<
font
color
=
"#000"
>
<
font
face
=
"Arial"
>
/
*
*
*
UnityPlayerActivity
,
UnityPlayerProxyActivity都继承自UnityPlayerNativeActivity
*
而UnityPlayerNativeActivity继承自NativeActivity
*
在该类里定义了一些和ANDROID生命周期相同的回调方法,留给自定义的Activity子类重写。
*
/
public
class
UnityPlayerNativeActivity extends NativeActivity
{
/
/
UnityPlayer的引用,并且我们不能改变这个引用变量的名字,它被native code所引用
protected UnityPlayer mUnityPlayer;
protected void onCreate
(
Bundle savedInstanceState
)
{
requestWindowFeature
(
Window.FEATURE_NO_TITLE
)
;
super.onCreate
(
savedInstanceState
)
;
/
/
设置显示窗口参数
getWindow
(
)
.takeSurface
(
null
)
;
setTheme
(
android.R.style.Theme_NoTitleBar_Fullscreen
)
;
getWindow
(
)
.setFormat
(
PixelFormat.RGB_
565
)
;
/
/
创建一个UnityPlayer对象,并赋值给全局的引用变量
mUnityPlayer
=
new
UnityPlayer
(
this
)
;
/
/
为UnityPlayer设置一些参数
if
(
mUnityPlayer.getSettings
(
)
.getBoolean
(
"hide_status_bar"
,
true
)
)
getWindow
(
)
.setFlags
(
WindowManager.LayoutParams.FLAG_FULLSCREEN
,
WindowManager.LayoutParams.FLAG_FULLSCREEN
)
;
int glesMode
=
mUnityPlayer.getSettings
(
)
.getInt
(
"gles_mode"
,
1
)
;
boolean
trueColor
8888
=
false
;
/
/
UnityPlayer.init
(
)
方法需要在将
view
附加到layout之前调用。它将会调用native code
mUnityPlayer.init
(
glesMode
,
trueColor
8888
)
;
/
/
从UnityPlayer中获取到Unity的View视图
View playerView
=
mUnityPlayer.getView
(
)
;
/
/
将Unity视图加载到根视图上
setContentView
(
playerView
)
;
/
/
使Unity视图获取焦点
playerView.requestFocus
(
)
;
}
protected void onDestroy
(
)
{
/
/
当Activity结束的时候调用UnityPlayer.
quit
(
)
方法,它会卸载之前调用的native code
mUnityPlayer.
quit
(
)
;
super.onDestroy
(
)
;
}
/
/
下面几个方法都是ANDROID相关回调方法,确保在ANDROID执行相应方法时UnityPlayer也需调用相应方法
protected void onPause
(
)
{
super.onPause
(
)
;
mUnityPlayer.pause
(
)
;
}
protected void onResume
(
)
{
super.onResume
(
)
;
mUnityPlayer.resume
(
)
;
}
public void onConfigurationChanged
(
Configuration newConfig
)
{
super.onConfigurationChanged
(
newConfig
)
;
mUnityPlayer.configurationChanged
(
newConfig
)
;
}
public void onWindowFocusChanged
(
boolean
hasFocus
)
{
super.onWindowFocusChanged
(
hasFocus
)
;
mUnityPlayer.windowFocusChanged
(
hasFocus
)
;
}
public
boolean
dispatchKeyEvent
(
KeyEvent
event
)
{
if
(
event
.getAction
(
)
=
=
KeyEvent.ACTION_MULTIPLE
)
return
mUnityPlayer.onKeyMultiple
(
event
.getKeyCode
(
)
,
event
.getRepeatCount
(
)
,
event
)
;
return
super.dispatchKeyEvent
(
event
)
;
}
}
<
/
font
>
<
/
font
>