| | 1 | | // Copyright (c) 2020-2022 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Collections.Generic; |
| | 6 | | using GDX.Collections.Generic; |
| | 7 | | using UnityEngine; |
| | 8 | | using UnityEngine.SceneManagement; |
| | 9 | |
|
| | 10 | | namespace GDX.Developer.ConsoleCommands |
| | 11 | | { |
| | 12 | | #if UNITY_2022_2_OR_NEWER |
| | 13 | | public class SceneLoadConsoleCommand : ConsoleCommandBase |
| | 14 | | { |
| | 15 | | Scene m_TargetScene; |
| | 16 | | #if UNITY_EDITOR |
| | 17 | | string m_EditorScenePath; |
| | 18 | | #endif |
| | 19 | |
|
| | 20 | | /// <inheritdoc /> |
| | 21 | | public override bool Evaluate(float deltaTime) |
| 0 | 22 | | { |
| | 23 | | #if UNITY_EDITOR |
| 0 | 24 | | if (m_TargetScene.IsValid()) |
| 0 | 25 | | { |
| 0 | 26 | | SceneManager.LoadSceneAsync(m_TargetScene.buildIndex, LoadSceneMode.Single); |
| 0 | 27 | | } |
| 0 | 28 | | else if(!string.IsNullOrEmpty(m_EditorScenePath)) |
| 0 | 29 | | { |
| 0 | 30 | | if (UnityEditor.EditorApplication.isPlaying) |
| 0 | 31 | | { |
| 0 | 32 | | UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(m_EditorScenePath, |
| | 33 | | new LoadSceneParameters(LoadSceneMode.Single)); |
| 0 | 34 | | } |
| | 35 | | else |
| 0 | 36 | | { |
| 0 | 37 | | UnityEditor.SceneManagement.EditorSceneManager.OpenScene(m_EditorScenePath, |
| | 38 | | UnityEditor.SceneManagement.OpenSceneMode.Single); |
| 0 | 39 | | } |
| 0 | 40 | | } |
| | 41 | | #else |
| | 42 | | SceneManager.LoadSceneAsync(m_TargetScene.buildIndex, LoadSceneMode.Single); |
| | 43 | | #endif |
| 0 | 44 | | return true; |
| 0 | 45 | | } |
| | 46 | |
|
| | 47 | | /// <inheritdoc /> |
| | 48 | | public override string GetKeyword() |
| 7 | 49 | | { |
| 7 | 50 | | return "scene"; |
| 7 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <inheritdoc /> |
| | 54 | | public override string GetHelpUsage() |
| 0 | 55 | | { |
| 0 | 56 | | return "scene <scene name> OR <scene index>"; |
| 0 | 57 | | } |
| | 58 | |
|
| | 59 | | /// <inheritdoc /> |
| | 60 | | public override string GetHelpMessage() |
| 0 | 61 | | { |
| 0 | 62 | | return "Loads a given scene asynchronously."; |
| 0 | 63 | | } |
| | 64 | |
|
| | 65 | | /// <inheritdoc /> |
| | 66 | | public override ConsoleCommandBase GetInstance(string context) |
| 0 | 67 | | { |
| | 68 | | // Check our known scenes in the manager. |
| | 69 | | // TODO: Build config output of list of scenes for this? |
| 0 | 70 | | SceneLoadConsoleCommand command = |
| | 71 | | new SceneLoadConsoleCommand { m_TargetScene = SceneManager.GetSceneByName(context) }; |
| | 72 | |
|
| 0 | 73 | | if (command.m_TargetScene.IsValid()) |
| 0 | 74 | | { |
| 0 | 75 | | return command; |
| | 76 | | } |
| | 77 | |
|
| | 78 | | // Lets check if it was a numeric value |
| 0 | 79 | | if (int.TryParse(context, out int buildIndex)) |
| 0 | 80 | | { |
| 0 | 81 | | command.m_TargetScene = SceneManager.GetSceneByBuildIndex(buildIndex); |
| 0 | 82 | | if (command.m_TargetScene.IsValid()) |
| 0 | 83 | | { |
| 0 | 84 | | return command; |
| | 85 | | } |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | #if UNITY_EDITOR |
| | 89 | | // It has to be perfect, not partials |
| 0 | 90 | | string[] possibleGuids = UnityEditor.AssetDatabase.FindAssets($"t:SceneAsset {context}"); |
| 0 | 91 | | int foundGuids = possibleGuids.Length; |
| 0 | 92 | | if (foundGuids > 0) |
| 0 | 93 | | { |
| 0 | 94 | | List<string> possiblePaths = new List<string>(foundGuids); |
| 0 | 95 | | for (int i = 0; i < foundGuids; i++) |
| 0 | 96 | | { |
| 0 | 97 | | possiblePaths.Add(UnityEditor.AssetDatabase.GUIDToAssetPath(possibleGuids[i])); |
| 0 | 98 | | } |
| 0 | 99 | | possiblePaths.Sort(); |
| 0 | 100 | | command.m_EditorScenePath = possiblePaths[0]; |
| 0 | 101 | | return command; |
| | 102 | | } |
| | 103 | | #endif |
| | 104 | |
|
| | 105 | |
|
| 0 | 106 | | Debug.LogWarning($"Unable to find scene '{context}'."); |
| 0 | 107 | | return null; |
| 0 | 108 | | } |
| | 109 | |
|
| | 110 | | /// <inheritdoc /> |
| | 111 | | public override string[] GetArgumentAutoCompleteSuggestions(string hint, string[] existingSet = null) |
| 0 | 112 | | { |
| 0 | 113 | | int sceneCount = SceneManager.sceneCountInBuildSettings; |
| 0 | 114 | | SimpleList<string> potentialScenes = new SimpleList<string>(sceneCount); |
| 0 | 115 | | if (string.IsNullOrEmpty(hint)) |
| 0 | 116 | | { |
| 0 | 117 | | for (int i = 0; i < sceneCount; i++) |
| 0 | 118 | | { |
| 0 | 119 | | Scene scene = SceneManager.GetSceneByBuildIndex(i); |
| 0 | 120 | | if (scene.IsValid()) |
| 0 | 121 | | { |
| 0 | 122 | | potentialScenes.AddUnchecked(scene.name); |
| 0 | 123 | | } |
| 0 | 124 | | } |
| 0 | 125 | | } |
| | 126 | | else |
| 0 | 127 | | { |
| 0 | 128 | | for (int i = 0; i < sceneCount; i++) |
| 0 | 129 | | { |
| 0 | 130 | | Scene scene = SceneManager.GetSceneByBuildIndex(i); |
| 0 | 131 | | if (scene.IsValid() && scene.name.StartsWith(hint)) |
| 0 | 132 | | { |
| 0 | 133 | | potentialScenes.AddUnchecked(scene.name); |
| 0 | 134 | | } |
| 0 | 135 | | } |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | // TODO: Find in project? editor |
| | 139 | |
|
| 0 | 140 | | potentialScenes.Compact(); |
| 0 | 141 | | return potentialScenes.Array; |
| 0 | 142 | | } |
| | 143 | | } |
| | 144 | | #endif // UNITY_2022_2_OR_NEWER |
| | 145 | | } |