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

Commit

Permalink
Fixed XRTK auto-initialization for auto scene switcher (#772)
Browse files Browse the repository at this point in the history
* Fixed XRTK auto-initialization for auto scene switcher

* remove the start scene from the editor build scenes if reset or cleared

* clear scenePath

* use null or whitespace
  • Loading branch information
StephenHodgson committed Jan 26, 2021
1 parent 3018494 commit 378d18d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
28 changes: 25 additions & 3 deletions Editor/MixedRealityPreferences.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,29 @@ public static SceneAsset StartSceneAsset
}
set
{
sceneAsset = value != null ? GetSceneObject(value) : null;
var scenePath = value != null ? AssetDatabase.GetAssetOrScenePath(value) : string.Empty;
string scenePath;

if (value == null)
{
scenePath = EditorPreferences.Get(START_SCENE_KEY, string.Empty);

if (!string.IsNullOrWhiteSpace(scenePath))
{
var oldScenePath = AssetDatabase.GetAssetOrScenePath(GetSceneObject(scenePath));
var buildScenes = EditorBuildSettings.scenes.ToList();
buildScenes.Remove(buildScenes.FirstOrDefault(buildScene => buildScene.path.Equals(oldScenePath)));
EditorBuildSettings.scenes = buildScenes.ToArray();
}

scenePath = string.Empty;
sceneAsset = null;
}
else
{
sceneAsset = GetSceneObject(value);
scenePath = AssetDatabase.GetAssetOrScenePath(value);
}

EditorPreferences.Set(START_SCENE_KEY, scenePath);
}
}
Expand Down Expand Up @@ -392,7 +413,7 @@ private static SceneAsset GetSceneObject(SceneAsset asset)

private static SceneAsset GetSceneObject(string sceneName, SceneAsset asset = null)
{
if (string.IsNullOrEmpty(sceneName) ||
if (string.IsNullOrWhiteSpace(sceneName) ||
EditorBuildSettings.scenes == null)
{
return null;
Expand All @@ -411,6 +432,7 @@ private static SceneAsset GetSceneObject(string sceneName, SceneAsset asset = nu
editorScene = new EditorBuildSettingsScene
{
path = AssetDatabase.GetAssetOrScenePath(asset),
enabled = true
};

editorScene.guid = new GUID(AssetDatabase.AssetPathToGUID(editorScene.path));
Expand Down
21 changes: 7 additions & 14 deletions Editor/Utilities/AutoSceneSwitcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,21 @@
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;
using XRTK.Services;
using XRTK.Extensions;
using XRTK.Editor.Utilities.SymbolicLinks;
using XRTK.Services;

namespace XRTK.Editor.Utilities
{
/// <summary>
/// Ensures that the <see cref="MixedRealityPreferences.StartSceneAsset"/> is always loaded
/// so that the <see cref="XRTK.Services.MixedRealityToolkit"/> runs correctly in the editor.
/// so that the <see cref="MixedRealityToolkit"/> runs correctly in the editor.
/// </summary>
[InitializeOnLoad]
public static class AutoSceneSwitcher
{
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"))
{
MixedRealityPreferences.StartSceneAsset = AssetDatabase.LoadAssetAtPath<SceneAsset>(EditorBuildSettings.scenes[0].path);
}
}

private static void OnPlayModeStateChanged(PlayModeStateChange playModeState)
Expand Down Expand Up @@ -79,14 +68,15 @@ private static void OnPlayModeStateChanged(PlayModeStateChange playModeState)
// If the start scene was successfully loaded, we're good and can continue playing.
if (startSceneLoaded)
{
EditorPreferences.Set($"{nameof(AutoSceneSwitcher)}", true);
return;
}

// 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())
if (!MixedRealityToolkit.IsInitialized && EditorPreferences.Get($"{nameof(AutoSceneSwitcher)}", true))
{
var dialogResult = EditorUtility.DisplayDialogComplex(
title: "Missing the MixedRealityToolkit",
Expand All @@ -97,6 +87,9 @@ private static void OnPlayModeStateChanged(PlayModeStateChange playModeState)

switch (dialogResult)
{
case 0:
EditorPreferences.Set($"{nameof(AutoSceneSwitcher)}", false);
break;
case 1:
MixedRealityToolkitInspector.CreateMixedRealityToolkitGameObject();
break;
Expand Down

0 comments on commit 378d18d

Please sign in to comment.