DEV Community

KENTO⚽️XR Engineer😎
KENTO⚽️XR Engineer😎

Posted on

【Unity】A script for easier scenes debugging in Unity Editor

Intro

I recommend to create a script for debug at first on creating a new Unity project. It costs you at first, but the benefits will pay off over time.

This time I will write in this article how to display all scenes for debugging.

Demo

A script can easily display all scenes in a project, like this:
Image description

Sample Code


using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class DisplayDebugScene : MonoBehaviour
{
    private List<string> _sceneNameList;

    private static DisplayDebugScene _instance;

    private void Awake()
    {
        if (_instance != null)
        {
            Destroy(_instance);
            return;
        }

        _instance = this;
        DontDestroyOnLoad(_instance);

        _sceneNameList = new List<string>();
        for (int i = 1; i < SceneManager.sceneCountInBuildSettings; i++)
        {
            var path = SceneUtility.GetScenePathByBuildIndex(i);
            var sceneName = path.Substring(0, path.Length - 6).Substring(path.LastIndexOf('/') + 1);
            _sceneNameList.Add(sceneName);
            Debug.Log(sceneName);
        }
    }

    private void OnGUI()
    {
        foreach (var sceneName in _sceneNameList)
        {
            if (GUILayout.Button(sceneName))
            {
                SceneManager.LoadScene(sceneName);
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

As a point of caution the script retrieves scenes other than the first in "Scene in Build". So you can create a scene for debug and attach the script.

Additionally, scene folder path should fix as "Scene".

Top comments (0)