简体   繁体   English

ARCore 会话未重新启动

[英]ARCore session is not restarting

Using Google ARCore, when I am in the game I have built I have a restart button to clear the scene and restart the level.使用 Google ARCore,当我在我构建的游戏中时,我有一个重新启动按钮来清除场景并重新启动关卡。 I was doing this by reloading the scene using Scenemanager.LoadScene() , however when the scene reloads the camera is not initialised and just shows a blue screen with my UI over the top.我是通过使用Scenemanager.LoadScene()重新加载场景来做到这一点的,但是当场景重新加载时,相机没有初始化,只是显示一个蓝屏,我的 UI 位于顶部。

Is there something I can use in code to ensure this doesn't happen when reloading the scene?我可以在代码中使用什么来确保在重新加载场景时不会发生这种情况?

In ARCore NDK you'd use the following method for destroying a session and releasing its resources: 在ARCore NDK中,您将使用以下方法破坏会话并释放其资源:

void ArSession_destroy(
    ArSession *session
)

This method releases resources used by an ARCore session. 此方法释放ARCore会话使用的资源。 It'll take several seconds to complete. 这将需要几秒钟才能完成。 To prevent blocking the main thread, call ArSession_pause() on the main thread , and then call ArSession_destroy() on a background thread . 为了防止阻塞主线程,调用ArSession_pause()主线程上,然后呼叫ArSession_destroy()后台线程

Then, you have to create a new session with : 然后,您必须使用创建一个新会话:

ArSession_create()

Also, in ARCore Android , there's typically onPause() and onResume() methods for current session . 另外, 在ARCore Android中当前会话通常具有onPause()onResume()方法。 But I use 2 other ones: pause() for pausing the current session and resume() for starting or resuming the ARCore current session. 但是我使用了另外两个: pause()用于暂停当前会话, resume()用于启动或恢复ARCore当前会话。

In Unity you should try DestroyImmediate(session) or Destroy(session) for your purpose. 在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;

Hope this helps. 希望这可以帮助。

For those who have an issue with ARCore in android studio, to solve this I used the simple workaround of replacing fragment instead of restarting entire Activity. 对于那些在android studio中遇到与ARCore有关的问题,为了解决此问题,我使用了替换片段而不是重新启动整个Activity的简单解决方法。

Procedure: I'm adding ArFragment dynamically and when the user press on refresh button I'm replacing the entire fragment. 过程:我正在动态添加ArFragment,当用户按下刷新按钮时,我将替换整个片段。

1-Adding Fragment dynamically: 1-动态添加片段:

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

2-Here is my refresh button code: 2-这是我的刷新按钮代码:

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

Everything worked fine but when I tried to findFragmentById after adding it inside onCreate method I got null pointer exception. 一切正常,但是当我在将其添加到onCreate方法中后尝试查找FragmentById时,出现了空指针异常。 To avoid this use Callback from ArFragment and in onAttach of ArFragment call the onComplete method and then use : 为了避免这种情况,请使用ArFragment中的Callback,并在ArFragment的onAttach中调用onComplete方法,然后使用:

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

Hope this will help. 希望这会有所帮助。

@Nouman Ch @Nouman Ch
According to your method, the fragment was successfully replaced, but I encountered a new problem. 根据您的方法,该片段已成功替换,但是我遇到了一个新问题。 the onupdatelistener was not updated, and the camera posture could not be obtained after restarting the session. onupdatelistener未更新,并且重新启动会话后无法获取相机姿态。 How do you understand the sentence "please use callback in arfragment" in your answer? 您如何理解答案中的“请在配置中使用回调”一句? I don't know how to use it. 我不知道该怎么用。 i asked the questin in stackflow ,but nobody answered to me . 我在stackflow中询问问题,但没人回答我。 click here to get the detail 点击这里获取详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM