MRTK instance added automatically to all scenes loaded in editor
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
- Create empty unity project.
- Create basic scene with a camera.
- Import MRTK into project.
- Open basic scene created before MRTK import
- Exhibit behavior described above.
Unity Editor Version
Unity 2018.3.8f1
Mixed Reality Toolkit Release Version
v2 RC1
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 ...
@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
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?
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

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
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.
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.

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?
Adding this #if UNITY_EDITOR to the 0 cases fixes all my issues

@Railboy What do you think of issues where the instance can be created when the app is quitting?
is this issue fixed with Move MRTK scene check out of the profile inspector and into a utility class. #3922
@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 ...
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.
@davidkline-ms could you follow up with Eric on his fix
@Yoyozilla @davidkline-ms I am completely removing the scene checker popup window in #4035
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 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?
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.
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;
}
}
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 Let's discuss these points in the PR.
I'll follow up with Lars and Eric on this issue