google-ar / arcore-unity-sdk

ARCore SDK for Unity
https://developers.google.com/ar
Other
1.4k stars 402 forks source link

Preserving ARCore session between scene changes #747

Open uffeflarup opened 4 years ago

uffeflarup commented 4 years ago

Hello all,

I'm currently working on a new feature for our AR game, where it preserves the ARCore session between scene changes. So when the player switches to a new world, then the player wont have to rescan their floor/table in order continue playing in that world. The game will continue the game on the surface that already have been scanned, which is a smoother playing experience.

The overall approach is like this, when I load a new world:

1) Call LifecycleManager.Instance.DisableSession() 2) Mark main camera (which also holds ARCoreSession and Tracked Pose Driver components) as DontDestroyOnLoad 3) Load next scene 4) Call LifecycleManager.Instance.EnableSession()

It works perfectly in Unity Editor when using Instant Preview. However, when building for Android it frequently crashes (maybe 1/3 of the times) when reaching step 4. The crash is coming through here:

08-09 11:43:14.125 15897 32726 E AndroidRuntime:        at libil2cpp.0x728afc(Native Method)
08-09 11:43:14.125 15897 32726 E AndroidRuntime:        at libunity.ARCore::ARCoreManager::FireEarlyUpdate()(FireEarlyUpdate:108)
08-09 11:43:14.125 15897 32726 E AndroidRuntime:        at libunity.0x292d48(Native Method)
08-09 11:43:14.125 15897 32726 E AndroidRuntime:        at libunity.ExecutePlayerLoop(NativePlayerLoopSystem*)(ExecutePlayerLoop:80)
08-09 11:43:14.125 15897 32726 E AndroidRuntime:        at libunity.ExecutePlayerLoop(NativePlayerLoopSystem*)(ExecutePlayerLoop:168)
08-09 11:43:14.125 15897 32726 E AndroidRuntime:        at libunity.PlayerLoop()(PlayerLoop:368)
08-09 11:43:14.125 15897 32726 E AndroidRuntime:        at libunity.UnityPlayerLoop()(UnityPlayerLoop:708)

I'm unable to figure out why it frequently crashes. Any ideas why disabling/enabling the ARCore session shouldn't work across scene changes?

Thank you VERY much in advance for all help.

Kind regards, Uffe Flarup

uffeflarup commented 4 years ago

Made some progress on the issue. Turns out it's related to Cloud Anchors:

If a call to XPSession.CreateCloudAnchor or XPSession.ResolveCloudAnchor is running, then it crashes when that call finishes.

I have tried to mark the hosted anchor with DontDestroyOnLoad as well, but that didn't solve the problem. And it's difficult to debug since cloud anchors don't work in Unity Editor.

uffeflarup commented 4 years ago

After a bit more digging I can see that cancelling CreateCloudAnchor call requires a bit of modification in the Unity API (only ResolveCloudAnchor can be cancelled in the 1.18 API). But after making these changes it seems to work.

Would be nice if there was a way in the API to simply cancel all cloud anchor requests. But it works for now.