Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Fix auto scene switcher dialog appearing when toolkit is already active and configured #754

Merged
merged 2 commits into from
Jan 14, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) XRTK. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using UnityEditor;
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using XRTK.Services;
using XRTK.Extensions;
using XRTK.Editor.Utilities.SymbolicLinks;

namespace XRTK.Editor.Utilities
Expand All @@ -20,6 +22,9 @@ static AutoSceneSwitcher()
{
EditorApplication.playModeStateChanged += OnPlayModeStateChanged;

// If a start scene hasn't been configured yet for XRTK but the project
// has build scenes configured, we assume the scene at index 0 of the build scenes
// is our start scene and save it to preferences, unless it's the Unity default sample scene.
if (MixedRealityPreferences.StartSceneAsset == null &&
EditorBuildSettings.scenes.Length > 0 &&
!EditorBuildSettings.scenes[0].path.Contains("SampleScene"))
Expand All @@ -41,47 +46,65 @@ private static void OnPlayModeStateChanged(PlayModeStateChange playModeState)
var startSceneAsset = MixedRealityPreferences.StartSceneAsset;
var startSceneLoaded = false;

// If we have a start scene configured in the XRTK preferences
// make sure it's loaded.
if (startSceneAsset != null)
{
for (int i = 0; i < SceneManager.sceneCount; i++)
{
var scene = SceneManager.GetSceneAt(i);

if (scene.name == startSceneAsset.name &&
SceneManager.GetActiveScene().name != startSceneAsset.name)
var loadedScene = SceneManager.GetSceneAt(i);
if (string.Equals(loadedScene.name, startSceneAsset.name))
{
SceneManager.SetActiveScene(scene);
startSceneLoaded = true;
break;
}
}

// A start scene was configured in preferences, but it's not loaded
// into hierarchy yet, so we force load it for the user now.
if (!startSceneLoaded && startSceneAsset != null)
{
var startScene = EditorSceneManager.OpenScene(AssetDatabase.GetAssetOrScenePath(startSceneAsset), OpenSceneMode.Additive);
SceneManager.SetActiveScene(startScene);

// Move the start scene to be first in hierarchy, if multiples scenes are loaded.
if (SceneManager.sceneCount > 1)
{
EditorSceneManager.MoveSceneBefore(startScene, SceneManager.GetSceneAt(1));
}

startSceneLoaded = true;
}
}

if (startSceneAsset != null && SceneManager.GetActiveScene().name == startSceneAsset.name) { return; }

var dialogResult = EditorUtility.DisplayDialogComplex(
title: "Missing the MixedRealityToolkit",
message: "Would you like to configure the Mixed Reality Toolkit now?",
"No, play anyway",
"Configure, then play",
"Cancel");
// If the start scene was successfully loaded, we're good and can continue playing.
if (startSceneLoaded)
{
return;
}

switch (dialogResult)
// We couldn't auto setup the XRTK start scene for the user. Thus, the currently
// loaded scene(s) may not contain a proper XRTK service locator game object configuration.
// Least we can do now is offer the user to auto configure the current scene, if none of the
// loaded scenes already contains a XRTK configuration.
if (MixedRealityToolkit.Instance.IsNull())
{
case 1:
MixedRealityToolkitInspector.CreateMixedRealityToolkitGameObject();
EditorApplication.isPlaying = true;
break;
case 2:
EditorApplication.isPlaying = false;
break;
var dialogResult = EditorUtility.DisplayDialogComplex(
title: "Missing the MixedRealityToolkit",
message: "Would you like to configure the Mixed Reality Toolkit now?",
"No, play anyway",
"Configure, then play",
"Cancel");

switch (dialogResult)
{
case 1:
MixedRealityToolkitInspector.CreateMixedRealityToolkitGameObject();
break;
case 2:
EditorApplication.isPlaying = false;
break;
}
}
}
}
}
}