我正在开发一个与Unity3d集成的应用程序,在我的代码的某些部分中,我调用了一个启动Unity3d代码的UnityPlayerActivity。当用户使用完Unity3d后,他按下一个按钮,然后返回到安卓代码。我的问题是当我回到安卓代码时,在UnityPlayerActivity调用之后:
@Override protected void onDestroy ()
{
mUnityPlayer.quit();
super.onDestroy();
}应用程序已关闭,而不仅仅是Unity活动。我不能调用这个方法,但我认为那会浪费内存。
有没有办法只退出Unity3d活动,并保持应用程序正常运行,这样我就可以在另一个时间再次启动Unity3d部件?
Tks
发布于 2016-04-19 22:08:58
找到答案了。只需在清单上使用另一个进程即可。
基本上,您必须在AndroidManifest.xml中显式地命名您的Activity进程。
例如。
<activity
android:name=".UnityPreviewActivity"
android:process=".UnityPreviewActivity" />参考
http://answers.unity3d.com/questions/587979/how-do-i-stop-munityplayerquit-closing-the-entire.html
发布于 2018-04-10 11:57:06
1.覆盖UnityPlayer类的kill()方法
类MyUnityPlayer扩展了UnityPlayer {
public MyUnityPlayer(Context context) {
super(context);
}
@Override
protected void kill() {
}
}kill()在mUnityPlayer.quit()中调用,它将终止当前进程:
protected void kill() {
Process.killProcess(Process.myPid());
}因此,重写它以避免进程被杀死。
2.在UnityPlayerActivity中将mUnityPlayer的类型更改为MyUnityPlayer
3.当您想要退出activity时,调用UnityPlayerActivity的finish()方法。(在我的例子中,App在从UnityPlayerActivity启动一个新活动后,在fininshing UnityPlayerActivity之后崩溃。)
发布于 2017-10-04 19:21:47
您也可以只使用反射,从而避免调用kill()方法(在quit()中调用),因为它会杀死当前进程
即
try {
//1
Field v = unityPlayer.getClass().getDeclaredField("v");
v.setAccessible(true);
Method a = v.getType().getMethod("a");
a.invoke(v.get(unityPlayer));
//2
Field o = unityPlayer.getClass().getDeclaredField("o");
o.setAccessible(true);
o.set(unityPlayer, true);
//3
unityPlayer.pause();
Method unloadVR = unityPlayer.getClass().getDeclaredMethod("unloadGoogleVR");
unloadVR.setAccessible(true);
unloadVR.invoke(unityPlayer);
//4
unityPlayer.removeAllViews();
//5
Method h = unityPlayer.getClass().getDeclaredMethod("h");
h.setAccessible(true);
h.invoke(unityPlayer);
} catch (Exception e) {
e.printStackTrace();
}https://stackoverflow.com/questions/36718387
复制相似问题