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布局中
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.
|