Unity在Android Activity中加载的时期测试
之前在研究Unity3D项目接入Android SDK中,发现在Unity项目生命周期与Android并不一样,无法直接管理Android的生命周期(需要导成Android项目才可以)。
而通过Unity版SDK接入Android,发现SDK的生命周期却没参与运行。在反编译后smali文件中加入SDK的生命周期后发现,Unity项目中的生命周期加载要在Android Activity onResume()之后。
Unity生命周期
Android Activity生命周期
对比
两者显然无法对比。
为确定Unity的生命加载周期在Android中的时期,为此我们准备一个测试Demo。
UnityDemo
1.新建一个Unity项目
点击Main Camara–>Add Component–>New Script–>UnityAndroidDemo.cs
在生命周期内添加打印信息,cs内容如下:
using UnityEngine;
using System.Collections;
public class UnityAndroidDemo : MonoBehaviour {
void Awake()
{
print("UnityAndroidDemo Unity is on Awake()");
}
// Use this for initialization
void Start () {
print("UnityAndroidDemo Unity is on Start()");
}
// Update is called once per frame
void Update () {
}
}
2.导出成Android项目
File–>BuildSettings–>设置包名–>Platform–>Android–>Google Android Project–>Export
3.用Eclipse打开导出的Android项目,打印生命周期信息
编辑后的UnityPlayerActivity的代码如下:
package com.yuchenfw.unityandroiddemo;
import com.unity3d.player.*;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class UnityPlayerActivity extends Activity
{
protected UnityPlayer mUnityPlayer; // don't change the name of this variable; referenced from native code
// Setup activity layout
@Override protected void onCreate (Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
Log.e("UnityAndroidDemo","UnityAndroidDemo Android is onCreate()");
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia play happy
mUnityPlayer = new UnityPlayer(this);
setContentView(mUnityPlayer);
mUnityPlayer.requestFocus();
}
// Quit Unity
@Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
Log.e("UnityAndroidDemo","UnityAndroidDemo Android is onDestroy()");
}
// Pause Unity
@Override protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
Log.e("UnityAndroidDemo","UnityAndroidDemo Android is onPause()");
}
// Resume Unity
@Override protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
Log.e("UnityAndroidDemo","UnityAndroidDemo Android is onResume()");
}
@Override protected void onStart()
{
super.onResume();
mUnityPlayer.resume();
Log.e("UnityAndroidDemo","UnityAndroidDemo Android is onStart()");
}
// This ensures the layout will be correct.
@Override public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
// Notify Unity of the focus change.
@Override public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { return mUnityPlayer.injectEvent(event); }
@Override public boolean onTouchEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
/*API12*/ public boolean onGenericMotionEvent(MotionEvent event) { return mUnityPlayer.injectEvent(event); }
}
4.运行
run as –>Android Application
日志如下:
红色是Android Activity的加载,绿色是Unity生命周期加载
5.结论
从日志可以看出,Unity的加载是在Android的onResume()之后进行的,因此在Unity中接入Android 的SDK时,涉及到的生命周期问题一定要在导成Android项目中进行,否则因SDK加载的过晚可能会导致功能的异常。