Android与Unity3D交互。启动UnityActivity出现黑屏、第一次要加载、第二次则秒开

Android工程:新建两个类。MyUnityPlayerActivity、MyUnityPlay

import android.app.Activity;
import android.content.res.Resources;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Handler;
import android.view.Window;
import android.widget.ImageView;
import com.unity3d.player.UnityPlayer;
import com.unity3d.player.UnityPlayerActivity;
import static com.unity3d.player.UnityPlayer.currentActivity;
public class MyUnityPlayerActivity extends UnityPlayerActivity
{
    private static Activity mActivity;
    // 这是启动画面的图片,这里可以使任意一个View,可以根据自己的需要去自定义
    private ImageView bgView = null;

    //Unity3D 点击返回按钮事件
    public void Exit()
    {
        moveTaskToBack(true);
    }
    public void OnShowSplash()
    {
        if (bgView != null)
            return;
        try
        {
            bgView = new ImageView(currentActivity);
            bgView.setImageResource(R.drawable.splash);
            bgView.setScaleType(ImageView.ScaleType.FIT_XY);
            Resources r = currentActivity.getResources();
            mUnityPlayer.addView(bgView, r.getDisplayMetrics().widthPixels, r.getDisplayMetrics().heightPixels);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    // 在unity的Activity中暴露方法给unity,让unity在准备好内容后调用隐藏启动画面
    public void OnHideSplash() {
        try {
            if (bgView == null)
                return;
            currentActivity.runOnUiThread(new Runnable()
            {
                public void run()
                {
                    mUnityPlayer.removeView(bgView);
                    bgView = null;
                }
            });
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    @Override
    protected  void  onDestroy()
    {
        super.onDestroy();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        mActivity = this;
        getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
        mUnityPlayer = new MyUnityPlay(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
        OnShowSplash();
    }
    // Resume Unity
    @Override
    protected void onResume()
    {
        super.onResume();
        new Handler().post(new Runnable()
        {
            @Override
            public void run()
            {
                mUnityPlayer.resume();
                currentActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run()
                    {
                        UnityPlayer.UnitySendMessage("Root","CommodityID","1");
                        //Unity3D 随便写一个类,需要挂载到Root物体上。
                        //这个类里边有个CommodityID方法,该方法有参数string
                    }
                });
            }
        });
    }
}

import android.content.ContextWrapper;
import com.unity3d.player.UnityPlayer;
public class MyUnityPlay extends UnityPlayer
{
    public MyUnityPlay(ContextWrapper contextWrapper) {
        super(contextWrapper);
    }
}

 

AndroidMainfest.xml   文件

commodity.html  这里是用H5写的调用UnityActivity
function aaaaaaaaaaaaaaaaaaaa(){
   var Intent = plus.android.importClass("android.content.Intent");
   var MyUnityPlayerActivity = plus.android.importClass("com.包名.MyUnityPlayerActivity");
   var intent=new Intent();
   var main = plus.android.runtimeMainActivity();
      intent.setClass(main,MyUnityPlayerActivity.class);
             main.startActivity(intent);
             main.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}

Unity3D 工程:随便写一个类,需要挂载到Root物体上。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using MySql.Data.MySqlClient;
using System.Data;
using I18N;
using I18N.West;
using I18N.CJK;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.Linq;

public class MyTest :MonoBehaviour
{

   private AndroidJavaObject my_activity;
    void Awake()
    {
        AndroidJavaClass jc = new AndroidJavaClass("com.xywf.MyUnityPlayerActivity");
        my_activity = jc.GetStatic("mActivity");
    }
    ///


    /// 点击Unity3D里的返回按钮调用Android里的方法
    ///

    public void Return()
    {
        my_activity.Call("Exit");
    }
    ///
    /// 隐藏启动UnityActivity的启动图
    ///

    IEnumerator HideAndroidSplash()
    {
        yield return new WaitForSeconds(0f);
        my_activity.Call("OnHideSplash");
    }

    private void Start()
    {
        StartCoroutine(HideAndroidSplash());//在Start里重写Android写的启动图(非APP启动图,跳UnityActivity的黑屏图)
    }

    //android 要调用的方法
    public void CommodityID(string commodityID)
    {
    }

}

 

 
 

 

本人并不懂Android,瞎猫碰死耗子。如有不对的地方,请尽管指点。

你可能感兴趣的:(Android与Unity3D交互。启动UnityActivity出现黑屏、第一次要加载、第二次则秒开)