我有一个带有子视图的活动,该视图将UnityPlayer加载到其中。这个UnityPlayer运作良好,符合预期。然后,我在该活动中打开一个新片段,该片段还具有一个将UnityPlayer加载到其中的视图。此UnityPlayer也可以正常运行,并且符合预期。

当我退出片段并返回活动时,就会出现问题。此处UnityPlayer被冻结,程序最终将崩溃。是否有人对在这样的不同视图中使用UnityPlayer有想法(或指针)?

例如:

当“活动”打开时,我使用它来初始化我的视图:

public void init(Context context) {
    inflate(context, R.layout.exercise_3d_animation_layout, this);

    mUnityPlayer = new UnityPlayer(context);

    FrameLayout frameLayout = findViewById(R.id.unity_player);
    frameLayout.addView(mUnityPlayer);
    mUnityPlayer.setOnTouchListener(this);
}


然后在我的活动中,所有正常的Unity-Android回调:

// Quit Unity
@Override protected void onDestroy ()
{
    mUnityPlayer.quit();
    super.onDestroy();
}

// Pause Unity
@Override protected void onPause()
{
    super.onPause();
    mUnityPlayer.pause();
}

// Resume Unity
@Override protected void onResume()
{
    super.onResume();
    mUnityPlayer.resume();
}

@Override protected void onStart()
{
    super.onStart();
    mUnityPlayer.start();
}

@Override protected void onStop()
{
    super.onStop();
    mUnityPlayer.stop();
}

// Low Memory Unity
@Override public void onLowMemory()
{
    super.onLowMemory();
    mUnityPlayer.lowMemory();
}

// Trim Memory Unity
@Override public void onTrimMemory(int level)
{
    super.onTrimMemory(level);
    if (level == TRIM_MEMORY_RUNNING_CRITICAL)
    {
        mUnityPlayer.lowMemory();
    }
}

// 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); }


当片段打开时,它将再次初始化视图(与之前相同)。我认为我的问题在于了解两者的生命周期以及如何相互作用

最佳答案

似乎unityPlayer.quit()实际上在被调用时确实杀死了它的进程。


  您可以通过指定其他进程名称来停止此行为
  (f.i.“:unity”)表示Android清单中的UnityPlayerActivity。
  似乎当调用quit()时UnityPlayer会杀死其进程,因此您
  只需确保您的应用程序和其他活动具有
  不同的进程名称(默认情况下是应用程序包名称)。


https://answers.unity.com/questions/587979/how-do-i-stop-munityplayerquit-closing-the-entire.html

关于android - 通过 fragment 保持UnityPlayer的生命,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52773712/

10-11 00:01