googlearchive / tango-examples-unity

Project Tango [deprecated] UnitySDK Example Projects
https://developers.google.com/ar
Apache License 2.0
470 stars 242 forks source link

Saving an area freezes Unity main loop, despite using new thread #106

Closed LudwikJaniuk closed 7 years ago

LudwikJaniuk commented 7 years ago

Your "AreaLearning" example uses a new thread when saving an area, in order not to freeze the UI and to be able to show progress updates (this is the code in question). However, I've noticed that even though a new thread is being used, the call to AreaDescription.SaveCurrent() still freezes the UI and stops Unity from executing Update calls for example. When saving large areas, this takes considerable amounts of time.

Why does AreaDescription.SaveCurrent() freeze the main thread even though it's being started from a new thread? And what can I do to provide updates to my users even through this process?

For the record, I tested this by inserting Debug.Log calls in the Update function, and observing the adb logs when pressing save. When I do something else that is time-consuming in the new thread (specifically for (int i = 0; i < 300000000; i++);), the update loop is still being run and I get my print calls. For some reason though, AreaDescription.SaveCurrent() freezes everything until it is done.

LudwikJaniuk commented 7 years ago

Additionally, for large enough areas, the saving process seems to never exit and I have to kill it, often resulting in an ADF called "unnamed" being saved, which doesn't want to relocalize. I would really appreciate some input in this issue.

LudwikJaniuk commented 7 years ago

Alright, after finding the C API function that gets called under the hood and reading its docs, I found out that

Since the Tango Service locks internally, other API calls (such as TangoService_getPoseAtTime()) will block while this method is running.

Indeed, I have other objects in my scene that use the Tango API in their Update() functions, and after writing code to disable them for the time of saving, Unity does not freeze anymore.

I just wish it would have been mentioned in the Tango docs about the Unity function I was using.