MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

MRTK instance added automatically to all scenes loaded in editor

Open provencher opened this issue 7 years ago • 22 comments

Overview

With the MRTK setup in a project, as soon as a scene without an MRTK instance is loaded in the editor, the MRTK creates an instance without an empty profile and forces the user to select a profile.

Expected Behavior

If no MRTK instance is loaded in a scene, then none should be added without the user's express consent.

Scenes without MRTK components should not have MRTK components automatically added to them.

Actual Behavior

If no MRTK instance exists in the scene, one is automatically added to the scene graph, without warning, with an empty profile, thus raising a popup prompting the user to select a profile.

If no profile is selected when the popup appears, the profile selection menu pops up again as soon as the MRTK instance is relesected, thus making it very difficult to delete the generated MRTK instance, as focus is constantly removed from the user.

If the user accepts any profile, then the scene is further modified, with the addition of many MRTK components, and the camera's hierarchy being changed.

Steps to reproduce

  1. Create empty unity project.
  2. Create basic scene with a camera.
  3. Import MRTK into project.
  4. Open basic scene created before MRTK import
  5. Exhibit behavior described above.

Unity Editor Version

Unity 2018.3.8f1

Mixed Reality Toolkit Release Version

v2 RC1

provencher avatar Apr 10 '19 14:04 provencher

I've just opened a PR for moving the "No MRTK" popup away from profiles and into a dedicated scene loaded/new handler: #3922

This still keeps the dialog (for the time being), but at least it should only happen on scene switching, and not when showing arbitrary profiles.

I'm curious though: From what i understand the code for creating a default MRTK object should only run when you actually confirm "Yes" in the dialog. Will try to repro with the steps above ...

ghost avatar Apr 10 '19 14:04 ghost

@lukastoenneMS I do not think that PR goes far enough to fix this issue.

I opened another PR which does resolve the issue completely for me.

The issue is that the scene check does not block rogue profiles or editor scripts for seeking the almighty Mrtk static Instance, which has a side effect of generating a new MRTK game object.

https://github.com/Microsoft/MixedRealityToolkit-Unity/pull/3925

provencher avatar Apr 10 '19 14:04 provencher

Is there any specific profile you selected to make the mrtk object be created ad-hoc? Do i need to be in play mode? if so, shouldn't the mrtk instance be temporary when in play mode?

ghost avatar Apr 10 '19 15:04 ghost

I believe the only difference from a blank scene my particular example has is that there is non-mrtk UI in it.

Perhaps that is an omission I should have mentioned in the issue at first. The MRTK adds a warning to all non-mrtk canvases, as follows image

In this situation, simply opening the scene adds the MRTK instance, and then requests a profile be selected, since none will be set by default. The Default profile modifies the camera hierarchy and adds components to the camera.

All this is done in edit mode, without ever once hitting play

provencher avatar Apr 10 '19 15:04 provencher

Alright, despite the fix I linked, I managed to enter a state where the play button doesn't work from there lacking an mrtk instance.

provencher avatar Apr 10 '19 17:04 provencher

Another issue worth investigating @luis-valverde-ms, is that the current Instance getter will create a new MRTK instance when the application is shutting down.

image

This leads to issues where the editor can hook onto an active instance created at runtime that doesn't actually exist in the scene when returning from playmode.

To reproduce this issue, simply start in a scene where the mrtk does not exist, then switch to a scene where it does, then end playmode. This causes many issues. @lukastoenneMS can you repro that?

provencher avatar Apr 10 '19 18:04 provencher

Adding this #if UNITY_EDITOR to the 0 cases fixes all my issues

image

provencher avatar Apr 10 '19 18:04 provencher

@Railboy What do you think of issues where the instance can be created when the app is quitting?

provencher avatar Apr 10 '19 18:04 provencher

is this issue fixed with Move MRTK scene check out of the profile inspector and into a utility class. #3922

Yoyozilla avatar Apr 23 '19 23:04 Yoyozilla

@Yoyozilla I don't think so. #3922 only changed the popup dialog, but in this case the MRTK instance is created automagically by the getter. I consider this bad behavior: we pretend the user is in control of creating MRTK, but then create the instance without consent anyway. Changing this might break existing code ...

ghost avatar Apr 24 '19 09:04 ghost

I've still run into this issue.

I've patched the fix in myself, which is simply to prevent the Instance from creating a new instance when the editor isn't in play mode, when there isn't already an instance in the scene.

provencher avatar Apr 24 '19 19:04 provencher

@davidkline-ms could you follow up with Eric on his fix

Yoyozilla avatar May 09 '19 21:05 Yoyozilla

@Yoyozilla @davidkline-ms I am completely removing the scene checker popup window in #4035

Railboy avatar May 09 '19 22:05 Railboy

I don't think just removing the scene checker is enough for this to be solved.

You need to also prevent the instance getter from creating instances if you are not in play mode.

provencher avatar May 09 '19 22:05 provencher

@provencher That is also taken care of. The instance getter doesn't create new instances at all, period. Would you like to take it for a spin?

Railboy avatar May 09 '19 22:05 Railboy

Cheers @Railboy, thanks for following up!

I think that's a much better behavior. I've been doing the fix I just listed, but I'm interested in seeing how your PR performs.

Do you have a PR that changes how Instances are created from the getter? I haven't seen the Instance getter modified in the pr you linked above.

provencher avatar May 10 '19 01:05 provencher

Do you have a PR that changes how Instances are created from the getter?

This is the new getter:

        public static MixedRealityToolkit Instance
        {
            get
            {
                if (activeInstance != null)
                {
                    return activeInstance;
                }

                var objects = FindObjectsOfType<MixedRealityToolkit>();

                switch (objects.Length)
                {
                    case 0:
                        break;

                    case 1:
                        RegisterInstance(objects[0]);
                        break;

                    default:
                        foreach (MixedRealityToolkit toolkit in objects)
                        {
                            RegisterInstance(toolkit);
                        }
                        break;
                }

                if (activeInstance == null)
                {
                    Debug.LogError("Didn't find / couldn't create an active instance of mixed reality toolkit. You can add one to your scene via 'Mixed Reality Toolkit->Add to Scene and Configure'");
                }

                return activeInstance;
            }
        }

Railboy avatar May 10 '19 02:05 Railboy

Awesome!

My only complaint is the error logging if the instance is null.

Also, if you're no longer adding instances, doing a findobject of type seems frivolous and expensive.

The registration also should happen outside the instance getter.

provencher avatar May 10 '19 03:05 provencher

@provencher Let's discuss these points in the PR.

Railboy avatar May 10 '19 03:05 Railboy

I'll follow up with Lars and Eric on this issue

Yoyozilla avatar Jun 05 '19 22:06 Yoyozilla