microsoft / MixedReality-SceneUnderstanding-Samples

MIT License
52 stars 20 forks source link

unable to access files referenced by SUSerializedScenePaths in a Threading.Task in SceneUnderstandingManager #23

Closed crystal-butler closed 2 years ago

crystal-butler commented 2 years ago

Unity version: 2020.3.2f1 Visual Studio version: 16.10.4 OS: Windows 10

I'm trying to modify the SceneUnderstandingManager code so that in addition to updating the scene geometry, some other processing of the resulting meshes occurs. Specifically, I want to generate OBJ files for particular scene object classes and automatically save them on some user-specified interval.

I've gotten this working on the HoloLens by modifying RetrieveDataContinuously to make a call to SaveObjsToDiskAsync (with the addition of a Thread.Sleep command to add a delay to the retrieval and storage cycle). However, when I test the changes using the Unity player and a serialized Scene on PC, any commands that require accessing the serialized scene contents (which are of type TextAsset and set in the public SUSerializedScenePaths variable) seem to silently fail, and no scene data is ever returned. Using the key command L, which I guess is running on the main Unity thread, saves the OBJs just fine. Moving the call to SaveObjsToDiskAsync from the separate thread to the Unity Update function also succeeds.

Is it expected behavior that referencing SUSerializedScenePaths files from a thread other than the main Unity thread fails? Is there any way to access them outside the main thread? I'm not actually displaying any of the Scene Understanding data, just exporting the geometry for other purposes. There is other data being displayed by Update, though, so I'd rather not add to the load on the main thread if I can avoid it.

jorgonz commented 2 years ago

Hi Crystal, at a quick glance, I'm unsure what could be happening here.

My initial guess is that 'File.WriteAllText' which is the call we use in the editor to save objs might be getting deadlocked silently in the background, since Unity is holding a reference to SUSerializedScenePaths, that might be causing some issue in the background thread you creating.

However i'm not entirely sure, nor 100% certain that's the issue.

I'm gonna take a look at it myself, and see if i can repro, and fix it on my side.

Sit tight!

jorgonz commented 2 years ago

From what i could gather, running Unity related APIs in background threads doesn't work. Since SUSerializedScenePaths has TextAssets, as you mentioned, it won't work since these are Unity Constructs. https://stackoverflow.com/questions/10959832/make-background-thread-in-unity3d

Before introducing async/task to Unity, developers used coroutines. coroutines will run in the main thread and will not freeze your UI. Don't know if that will help you. At least it might unblock you for now? https://docs.unity3d.com/Manual/Coroutines.html

crystal-butler commented 2 years ago

Great, thanks for checking into that @jorgonz! I should be able to find a workaround that uses the main Unity thread.