0


Android与Unity交互注意事项

1.AndroidStudio主工程导入unityLibrary库并添加依赖

2.主工程build.gradle 加入

ndk {
    abiFilters 'armeabi-v7a'
}

3.添加launcher中的string.xml中的game_view_content_description主到主工程资源文件string.xml中

<string name="game_view_content_description">Game view</string>

4.unityLibrary中的Androidmanifest启动入口注释掉

<!-- <intent-filter> -->
<!-- <action android:name="android.intent.action.MAIN" /> -->
<!-- <category android:name="android.intent.category.LAUNCHER" /> -->
<!-- </intent-filter> -->

5.UnityPlayerActivity的横竖屏设置在Androidmanifest中修改无效,需要unity导出Android工程时就修改为竖屏

6.unity 场景作为Android的子view添加进activity布局中,mUnityPlayer表示的unity的view,Android视图嵌入unity场景,只需在布局中的所需位置将mUnityPlayer添加进布局中即可

public class WithUnityActivity extends UnityPlayerActivity {
    FrameLayout fl;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_with_unity);
        fl = (FrameLayout) findViewById(R.id.fl_content);
        fl.addView(mUnityPlayer);
    }
}

7.返回上一个activity时使用finish()或者mUnityPlayer.quit();都会出现不同情况的闪退,原因是

UnityPlayer在退出时会调用kill方法杀进程

解决:重写UnityPlayer中的kill方法

public class MyUnityPlayer extends UnityPlayer {

    public MyUnityPlayer(Context contex) {
        super(contex);
    }
    @Override
    protected void kill() {

    }
}

替换UnityPlayerActivity中UnityPlayer

public class UnityPlayerActivity extends Activity implements IUnityPlayerLifecycleEvents
{
//    protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
    protected MyUnityPlayer mUnityPlayer;

    protected String updateUnityCommandLineArguments(String cmdLine)
    {
        return cmdLine;
    }

    // Setup activity layout
    @Override protected void onCreate(Bundle savedInstanceState)
    {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);

        String cmdLine = updateUnityCommandLineArguments(getIntent().getStringExtra("unity"));
        getIntent().putExtra("unity", cmdLine);
//        mUnityPlayer = new UnityPlayer(this, this);
        mUnityPlayer = new MyUnityPlayer(this);
        setContentView(mUnityPlayer);
        mUnityPlayer.requestFocus();
    }

WithUnityActivity 监听返回键,finish()

public class WithUnityActivity extends UnityPlayerActivity {
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

8.继承UnityPlayerActivity时,若是在unityLibrary中则可以直接继承使用mUnityPlayer。若是在主工程中继承则会报错:找不到com.unity3d.player.UnityPlayer的类文件错误

解决方式:不依赖unityLibrary源码库,将unityLibrary编译成aar库放入主工程中使用。

9.unity 与Android相互调用的方法:(1)Android端在继承UnityPlayerActivity的activity中定义对应方法

public class UnityWithActivity extends UnityPlayerActivity {
    /**
    * unity 调用Android的方法
    */
    public void UnityCallAndroid(String btnType){
        Log.d("TAG", "UnityPlayerActivity: unity调用Android方法,btnType:"+btnType);
    }
/**
 * android调用unity的方法
 * UnitySendMessage  参数1:挂载脚本的物体GameObject  参数2:方法的名称  参数3:传递的内容
 */
public void AndroidCallUnity(String msg){
    UnityPlayer.UnitySendMessage("Main Camera","AndroidCallUnity",msg);
}

(2)unity 脚本中调用activity 定义的方法以及定义给Android调用的方法

public class ToAndroidScript : MonoBehaviour
{
AndroidJavaClass jc;
AndroidJavaObject jo;
private void Awake()
{
jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");

     jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

}
 public void ClickBtn1()
 {
     jo.Call("UnityCallAndroid", "1");
 }
public void AndroidCallUnity(string msg)
    {
        button.gameObject.GetComponentInChildren<Text>().text = msg;
    }
标签: android unity

本文转载自: https://blog.csdn.net/xuwei7746521/article/details/123480626
版权归原作者 xuwei7746521 所有, 如有侵权,请联系我们删除。

“Android与Unity交互注意事项”的评论:

还没有评论