有些时候,用于对接得Android SDK要求主Activity必须继承其接口提供得activity,这时就不能继承unity的activity,我们只能手工创建unity player。
import com.unity3d.player.UnityPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.res.Configuration;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
private UnityPlayer mUnityPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//创建unityplayer对象
mUnityPlayer = new UnityPlayer(this);
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 trueColor8888 = false;
mUnityPlayer.init(glesMode, trueColor8888);
//设置Unity view
View playerView = mUnityPlayer.getView();
setContentView(playerView);
playerView.requestFocus();
}
//调用unity的onDestroy
protected void onDestroy()
{
super.onDestroy();
mUnityPlayer.quit();
}
//调用Unity的onPause
protected void onPause()
{
super.onPause();
mUnityPlayer.pause();
}
//调用Unity的onResume
protected void onResume()
{
super.onResume();
mUnityPlayer.resume();
}
//调用Unity的ConfigurationChanged
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mUnityPlayer.configurationChanged(newConfig);
}
//调用Unity的onWindowFocusChanged
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
mUnityPlayer.windowFocusChanged(hasFocus);
}
//调用Unity的onKeyDown
public boolean onKeyDown(int keyCode, KeyEvent event)
{
return mUnityPlayer.onKeyDown(keyCode, event);
}
//调用Unity的
public boolean onKeyUp(int keyCode, KeyEvent event)
{
return mUnityPlayer.onKeyUp(keyCode, event);
}
}