< Summary

Class:GDX.Developer.ConsoleCommands.SceneLoadConsoleCommand
Assembly:GDX
File(s):./Packages/com.dotbunny.gdx/GDX/Developer/ConsoleCommands/SceneConsoleCommand.cs
Covered lines:3
Uncovered lines:77
Coverable lines:80
Total lines:145
Line coverage:3.7% (3 of 80)
Covered branches:0
Total branches:0
Covered methods:1
Total methods:6
Method coverage:16.6% (1 of 6)

Coverage History

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity NPath complexity Sequence coverage
Evaluate(...)0%20400%
GetKeyword()0%110100%
GetHelpUsage()0%2100%
GetHelpMessage()0%2100%
GetInstance(...)0%42600%
GetArgumentAutoCompleteSuggestions(...)0%56700%

File(s)

./Packages/com.dotbunny.gdx/GDX/Developer/ConsoleCommands/SceneConsoleCommand.cs

#LineLine coverage
 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
 5using System.Collections.Generic;
 6using GDX.Collections.Generic;
 7using UnityEngine;
 8using UnityEngine.SceneManagement;
 9
 10namespace 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)
 022        {
 23#if UNITY_EDITOR
 024            if (m_TargetScene.IsValid())
 025            {
 026                SceneManager.LoadSceneAsync(m_TargetScene.buildIndex, LoadSceneMode.Single);
 027            }
 028            else if(!string.IsNullOrEmpty(m_EditorScenePath))
 029            {
 030                if (UnityEditor.EditorApplication.isPlaying)
 031                {
 032                    UnityEditor.SceneManagement.EditorSceneManager.LoadSceneAsyncInPlayMode(m_EditorScenePath,
 33                        new LoadSceneParameters(LoadSceneMode.Single));
 034                }
 35                else
 036                {
 037                    UnityEditor.SceneManagement.EditorSceneManager.OpenScene(m_EditorScenePath,
 38                        UnityEditor.SceneManagement.OpenSceneMode.Single);
 039                }
 040            }
 41#else
 42            SceneManager.LoadSceneAsync(m_TargetScene.buildIndex, LoadSceneMode.Single);
 43#endif
 044            return true;
 045        }
 46
 47        /// <inheritdoc />
 48        public override string GetKeyword()
 749        {
 750            return "scene";
 751        }
 52
 53        /// <inheritdoc />
 54        public override string GetHelpUsage()
 055        {
 056            return "scene <scene name> OR <scene index>";
 057        }
 58
 59        /// <inheritdoc />
 60        public override string GetHelpMessage()
 061        {
 062            return "Loads a given scene asynchronously.";
 063        }
 64
 65        /// <inheritdoc />
 66        public override ConsoleCommandBase GetInstance(string context)
 067        {
 68            // Check our known scenes in the manager.
 69            // TODO: Build config output of list of scenes for this?
 070            SceneLoadConsoleCommand command =
 71                new SceneLoadConsoleCommand { m_TargetScene = SceneManager.GetSceneByName(context) };
 72
 073            if (command.m_TargetScene.IsValid())
 074            {
 075                return command;
 76            }
 77
 78            // Lets check if it was a numeric value
 079            if (int.TryParse(context, out int buildIndex))
 080            {
 081                command.m_TargetScene = SceneManager.GetSceneByBuildIndex(buildIndex);
 082                if (command.m_TargetScene.IsValid())
 083                {
 084                    return command;
 85                }
 086            }
 87
 88#if UNITY_EDITOR
 89            // It has to be perfect, not partials
 090            string[] possibleGuids = UnityEditor.AssetDatabase.FindAssets($"t:SceneAsset {context}");
 091            int foundGuids = possibleGuids.Length;
 092            if (foundGuids > 0)
 093            {
 094                List<string> possiblePaths = new List<string>(foundGuids);
 095                for (int i = 0; i < foundGuids; i++)
 096                {
 097                    possiblePaths.Add(UnityEditor.AssetDatabase.GUIDToAssetPath(possibleGuids[i]));
 098                }
 099                possiblePaths.Sort();
 0100                command.m_EditorScenePath = possiblePaths[0];
 0101                return command;
 102            }
 103#endif
 104
 105
 0106            Debug.LogWarning($"Unable to find scene '{context}'.");
 0107            return null;
 0108        }
 109
 110        /// <inheritdoc />
 111        public override string[] GetArgumentAutoCompleteSuggestions(string hint, string[] existingSet = null)
 0112        {
 0113            int sceneCount = SceneManager.sceneCountInBuildSettings;
 0114            SimpleList<string> potentialScenes = new SimpleList<string>(sceneCount);
 0115            if (string.IsNullOrEmpty(hint))
 0116            {
 0117                for (int i = 0; i < sceneCount; i++)
 0118                {
 0119                    Scene scene = SceneManager.GetSceneByBuildIndex(i);
 0120                    if (scene.IsValid())
 0121                    {
 0122                        potentialScenes.AddUnchecked(scene.name);
 0123                    }
 0124                }
 0125            }
 126            else
 0127            {
 0128                for (int i = 0; i < sceneCount; i++)
 0129                {
 0130                    Scene scene = SceneManager.GetSceneByBuildIndex(i);
 0131                    if (scene.IsValid() && scene.name.StartsWith(hint))
 0132                    {
 0133                        potentialScenes.AddUnchecked(scene.name);
 0134                    }
 0135                }
 0136            }
 137
 138            // TODO: Find in project? editor
 139
 0140            potentialScenes.Compact();
 0141            return potentialScenes.Array;
 0142        }
 143    }
 144#endif // UNITY_2022_2_OR_NEWER
 145}

Coverage by test methods







Methods/Properties

Evaluate(System.Single)
GetKeyword()
GetHelpUsage()
GetHelpMessage()
GetInstance(System.String)
GetArgumentAutoCompleteSuggestions(System.String, System.String[])