Unity与Android融合重写UnitySplash

吃水不忘挖坑人

        感谢寻梦的原作,让我这个Android小白能成功的完成这次操作.

写在这里的话

        大体的都是来自原作者的,所以就不做更多的解释,这里只是对于其中gif图片的加载做一个记录
Android部分
        重写UnitySplash

public class UnitySplashSDK
{
    private ImageView bgView = null;
    private GifImageView gv = null;
    private UnityPlayer mUnityPlayer = null;
    private TextView tv = null;

    private static UnitySplashSDK mInstance = null;
    public static UnitySplashSDK getInstance()
    {
        if(null == mInstance)
        {
            synchronized (UnitySplashSDK.class)
            {
                if(null == mInstance)
                    mInstance = new UnitySplashSDK();
            }
        }
        return mInstance;
    }

    public void onCreate(Activity activity, Bundle savedInstanceState)
    {
        mUnityPlayer = ((UnityPlayerActivity)activity).getUnityPlayer();
        onShowSplash();
    }

    public void onShowSplash()
    {
        if(bgView != null)
            return;
        try {

            // 设置启动内容,这里的内容可以自定义区修改,这里我加载了一个gif作为启动图标↓
            bgView = new ImageView(UnityPlayer.currentActivity);

            bgView.setBackgroundColor(Color.parseColor("#36c3e5"));

            InputStream is = UnityPlayer.currentActivity.getAssets().open("loadBackground.png");
            Bitmap bm = BitmapFactory.decodeStream(is);
            bgView.setImageBitmap(bm);
            bgView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            Resources r = mUnityPlayer.currentActivity.getResources();

            mUnityPlayer.addView(bgView, r.getDisplayMetrics().widthPixels, r.getDisplayMetrics().heightPixels);

            gv = new GifImageView(UnityPlayer.currentActivity);
            Point p = new Point();
            WindowManager wm = (WindowManager) UnityPlayer.currentActivity.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getSize(p);
            gv.setMovie(UnityPlayer.currentActivity.getAssets().open("loadAnimation.gif"),p.x / 2 - 150,p.y / 2 - 300);
            gv.setScaleType(ImageView.ScaleType.CENTER);
            mUnityPlayer.addView(gv, r.getDisplayMetrics().widthPixels, r.getDisplayMetrics().heightPixels);

            tv = new TextView(UnityPlayer.currentActivity);
            tv.setTextColor(Color.WHITE);
            tv.setTextSize(16);
            tv.setText("请稍后,正在努力加载中...");
            Log.v("test",p.x+"");
            mUnityPlayer.addView(tv, r.getDisplayMetrics().widthPixels, r.getDisplayMetrics().heightPixels);
            tv.setPadding(p.x / 2 - 240,p.y / 2 + 20,0, 0);

        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public void onHideSplash()
    {
        try {
            if(bgView == null)
                return;
            UnityPlayer.currentActivity.runOnUiThread(new Runnable() {
                @Override
                public void run()
                {
                    mUnityPlayer.removeView(bgView);
                    bgView = null;
                    mUnityPlayer.removeView(gv);
                    gv = null;
                    mUnityPlayer.removeView(tv);
                    tv = null;
                }
            });
        }catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

        Gif图调用,没有获取图片的大小,和锚点的控制,需要自己计算一下位置

package com.jx.shoe;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;

import java.io.InputStream;

/**
 * Created by Administrator on 2018/7/25.
 */

public class GifImageView extends ImageView
{
    private Movie mMovie;
    private long mMovieStart = 0;

    private int x,y;

    public GifImageView(Context context)
    {
        super(context);
    }

    public GifImageView(Context context, AttributeSet attrs)
    {
        super(context,attrs);
    }

    public void setMovie(InputStream is ,int dx,int dy)
    {
        mMovie = Movie.decodeStream(is);
        x = dx;
        y = dy;
    }

    @Override
    public void onDraw(Canvas canvas) {

        long now = android.os.SystemClock.uptimeMillis();

        if (mMovieStart == 0) {
            mMovieStart = now;
        }

        if (mMovie != null) {
            int dur = mMovie.duration();

            if (dur == 0) {
                dur = 1000;
            }

            int relTime = (int) ((now - mMovieStart) % dur);
            mMovie.setTime(relTime);
            mMovie.draw(canvas, x, y);

            invalidate();
        }
    }

}

        在UnityPlayerActivity脚本的onCreate中添加如下一句

UnitySplashSDK.getInstance().onCreate(this,savedInstanceState);

        还需要在这个脚本添加一个函数

 public void hideSplash()
    {
        UnitySplashSDK.getInstance().onHideSplash();
    }

Unity部分

private AndroidJavaObject m_activity;

    void Awake()
    {
        AndroidJavaClass ajc = new AndroidJavaClass("com.jx.shoe.UnityPlayerActivity");
    }

    void Start () {
        HideAndroidSplash();
    }
    public void HideAndroidSplash() {
       ajc.Call("hideSplash");
    }

        在程序一开运行的时候就隐藏界面
ps
        我这里是直接在Andorid中写了加载界面,没有使用unity的加载界面,相当于在Andorid中给加载的那一段黑屏时期给了一块遮羞布.
        顺带在说一下,图片资源放在unity导出的工程的assets文件夹下,据说这种对于gif的操作在gif文件比较大的时候会报错,奈何我使用android-gif-drawable这个开源框架的时候,真机调试提示我硬件不支持该App,对于我这个小白,这个问题,我实在是不知道从何查起,只能换一种方法了,如果有大神愿意指点一下迷津,不甚感激.

        不知道是不是我打开方式不对,在给unity调用隐藏的时候,需要单独写一个类供unity调用,而不能直接使用unity打包工程生成的那个UnityPlayerAcitivity,我尝试直接将这个hideSplash写在生成的这个UniytPlayerActivity里面,没法完成调用,至于为什么,当前没有明确的答案,如果有大神知道,烦请告知.

你可能感兴趣的:(Unity与Android融合重写UnitySplash)