Unity-Technologies / EditorXR

Author XR in XR
Other
925 stars 167 forks source link

Fix Context destroy bug #541

Closed mtschoen-unity closed 5 years ago

mtschoen-unity commented 5 years ago

Purpose of this PR

Fix an issue where the in-scene context manager object can get destroyed or appear multiple times. Fixes #530

Testing status

This issue is difficult to reproduce. What was happening is that starting EXR in edit-mode would set hideflags on all ContextManagers to DontSaveInEditor, meaning that the one from your open scene, as well as the one that EXR just created got these hideflags. Closing the window would destroy the new context manager object, but not restore the hideflags on the one in your scene. Then, when you change scenes, your original context would not be destroyed, resulting in two of these objects existing while only one showed up in the hierearchy. If you entered play mode, you would get hideflags of None set on all EditingContextManagers, causing the second object to appear in the hierarchy. It was possible to "build up" a number of context objects this way.

In the reverse, what would happen is simply that, once you got your in-scene editing context manager's hideflags set to DontSaveInEditor, the object will disappear once you enter Play Mode. For some reason this also causes it to get ignored in builds, and will not execute its scripts in play mode. The object still exists but it doesn't do anything.

I was able to figure this out by creating an editor window with the following code:

var managers = Resources.FindObjectsOfTypeAll<EditingContextManager>();
foreach (var manager in managers)
{
    GUILayout.Label(manager.name + ", " + manager.gameObject.hideFlags);
}

Technical risk

Medium -- I'm not sure there was a particularly good reason why we were setting hide flags on >1 EditingContextManager in the first place.

Comments to reviewers

So glad to have squashed this bug. It was a tricky one, so it there may be some edge cases I'm missing. For some reason this view https://github.com/Unity-Technologies/SuperScience#hiddenhierarchy-find-hidden-scene-objects didn't show those hidden editing context manager objects. I had to specifically use Resources.FindObjectsOfTypeAll<EditingContextManager>() for it to show up.