DEV Community

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

Posted on

1

【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".

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay