kotlin ARCore会话未重新启动

clj7thdc  于 2023-02-19  发布在  Kotlin
关注(0)|答案(2)|浏览(100)

使用谷歌Arcore,当我在我已经建立的游戏中,我有一个重启按钮来清除场景和重启关卡。我是通过使用Scenemanager.LoadScene()重新加载场景来做到这一点的,但是当场景重新加载时,相机没有初始化,只是显示一个蓝色屏幕,我的UI在顶部。
有什么我可以在代码中使用,以确保这不会发生时,重新加载场景?

fafcakar

fafcakar1#

在ARCore NDK中,您可以使用以下方法销毁会话并释放其资源:

void ArSession_destroy(
    ArSession *session
)

此方法释放ARCore会话使用的资源。完成此操作需要几秒钟。若要防止阻塞主线程,请在主线程上调用ArSession_pause(),然后在后台线程上调用ArSession_destroy()

然后,您必须创建一个新会话:

ArSession_create()

此外,在ARCore Android中,当前会话通常有onPause()onResume()方法。但我使用另外两个方法:pause()用于暂停当前会话,resume()用于启动或恢复ARCore当前会话。

在Unity中,您应该根据需要尝试DestroyImmediate(session)Destroy(session)

ARCoreSession session = goARCoreDevice.GetComponent<ARCoreSession>();
ARCoreSessionConfig myConfig = session.SessionConfig;
DestroyImmediate(session);
// Destroy(session);
yield return null;
session = goARCoreDevice.AddComponent<ARCoreSession>();
session.SessionConfig = myConfig;
session.enabled = true;
46scxncf

46scxncf2#

对于那些在android studio中使用ARCore有问题的人,为了解决这个问题,我使用了简单的变通方案,即替换片段,而不是重新启动整个Activity。

**步骤:**我动态添加ArFragment,当用户按下刷新按钮时,我替换整个片段。

1-动态添加片段:

getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.container, new MainARFragment())
                .addToBackStack(null)
                .commit();

2-下面是我的刷新按钮代码:

refreshButton.setOnClickListener(view -> {
            //refresh everything remove models and detect from start
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.container, new MainARFragment()).commit();
        });

一切正常,但当我尝试在onCreate方法中添加findFragmentById后,我得到了空指针异常。为了避免这种情况,使用回调从ArFragment和在onAttach的ArFragment调用onComplete方法,然后用途:

arFragment = (MainARFragment) getSupportFragmentManager().findFragmentById(R.id.container);

希望这会有帮助。

相关问题