Azure / azure-object-anchors

Azure Object Anchors releases and samples
https://docs.microsoft.com/azure/object-anchors
MIT License
37 stars 17 forks source link

Azure Object Anchors scene does not work the second time it is loaded because resources are not cleaned up properly #10

Closed horatiu665 closed 3 years ago

horatiu665 commented 3 years ago

Description

AOA scene works fine the first time I run it, but when I unload and load the scene again (using the MRTK scene system / examples hub) the Start Search button does not work anymore.

Steps to reproduce the issue

I have successfully managed to track an object for which I have a CAD model. I used the conversion program to get the .ou file, then managed to build the quickstart/apps/unity/mrtk and got it to detect my model. Perhaps of note is that I upgraded the mrtk project to unity 2020.3 and then I imported variuos MRTK samples using the MixedRealityFeatureTool.exe, including the ExamplesHub.

I added a button to load the AOA scene and I used the MRTK scene system. I am able to enter and exit all the various demos and experiments, and I figured I'll add the AOA among them.

The first time I run the AOA scene it works, and tracks just like in the build with only that scene. But when I leave the scene, the tracked model does not disappear (if I quit after pressing Start Searching and before pressing Stop Searching). Another thing I noticed is the mesh of the scene tracking (that looks like my room) that is drawn around the model also doesn't disappear sometimes, and it has a pink (missing) material.

I discovered I can make the Start Searching button work again the second time by adding this code in the ObjectAnchorsService.cs file, line 43:


        public static IObjectAnchorsService GetService()
        { 
            {
                if (_service == null)
                {
                    _service = new Lazy<ObjectAnchorsService>(() => new ObjectAnchorsService(), true);
                }
                return _service.Value;
            } 
        }

because I noticed that _service is null after unloading, but is not initialized except in the field.

I'll try to figure out how this whole thing works and make my own version of the scene in which everything is cleaned up properly, but it would be nice to not have to worry about that for the future. Hope this post helps!

Expected behavior

the AOA scene should work every time like the first time I started it.

Screenshots


Development information (please complete the following information)

AR Device information (please complete the following information):

Additional context

I discovered what might cause the cleanup issues. The wireframe model of the detected object is created in the hub scene, because the AOA scene is additively loaded in my case. I suspect that upon cleanup, there is some assumption made about which scene it searches for objects to delete them, and it doesn't take into account that it might be in an additively loaded scene.

I didn't solve it yet, but I just have this interesting discovery that hopefully leads to solving it.

BTW: I used Runtime Inspector to discover this, it's an asset store tool that lets you add a hierarchy and inspector view directly in a build. and it works pretty well with MRTK, I just put it on a SlateUGUI object as it comes packaged as a regular unity UI object.

craigktreasure commented 3 years ago

Hmmmm...that's not really a scenario we had in mind for this particular sample, but it doesn't sound unreasonable. We'd be happy to accept a fix for the issue.

craigktreasure commented 3 years ago

Closing this in favor of #11.