Can I create array of Scenes unity?

Viewed 360

I want to create public Scene[] levels; and manually assign my levels to the array, then loop through it and generate level selection buttons, but it won't show up in the inspector.

Is there any workaround?

Unity inspector screenshot

True class implementation MonoBehavior

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

public class True : MonoBehaviour
{
    // Start is called before the first frame update
   
    public static int money;
    
    [SerializeField]
    public SceneManager[] scenes;
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public void nextscencefirst()
    {
        SceneManager.LoadScene("Level2");
        money++;
    }



}


1 Answers

SceneManager is a built-in static class to control scenes during runtime. We can go to any scene by calling LoadScene method from that class. Also SceneManager cannot be seen in Inspector since it cannot be serialized class.

A reminder: Your scenes should be listed on build settings for these to work.

There are three ways to do what you want to do:

Method 1. Create a string list that holds names of scenes

public List<string> sceneNameList = new List<string>;

Method 2. Create a list of indices of scenes that added in build settings.

public List<int> sceneBuildIndexList = new List<int>

If the names of scenes are long or somehow difficult or you want to use sceneBuildIndex value it would be good to create a class and reach scenes from it

[System.Serializable]
public class SceneData
{
    public int sceneBuildIndex;
    public string sceneKey;

    //public void LoadScene() //Maybe 
}

public List<SceneData> sceneDataList = new List<SceneData>();

public SceneData GetSceneData(string key)
{
    for (int i = 0; i < sceneDataList.Count; i++)
    {
        if (sceneDataList[i].sceneKey == key)
        {
            return sceneDataList[i];
        }
    }

    return null;
}

Method 3: In Unity most classes are created from UnityEngine.Object, so it may let you assign scene from the Inspector.

[System.Serializable]
public class SceneData
{
    public UnityEngine.Object scene;
    public string key;

    public void LoadScene()
    {
        if (scene == null)
        {
            Debug.LogError("Scene is not assigned");
            return;
        }

        string pathToScene = AssetDatabase.GetAssetPath(scene);
        SceneManager.LoadScene(pathToScene); //If the scene is not set in BuildSettings it will throw an error.
    }
}

public List<SceneData> sceneDataList = new List<SceneData>();

public SceneData GetSceneData(string key)
{
    for (int i = 0; i < sceneDataList.Count; i++)
    {
        if (sceneDataList[i].key == key)
        {
            return sceneDataList[i];
        }
    }

    return null;
}


private void Awake()
{
    SceneData data = GetSceneData("Level1");
    if (data != null)
    {
        data.LoadScene();
    }
}
Related