Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Last active September 11, 2022 23:21
Show Gist options
  • Save thebeardphantom/28ec29accea44af7dbd6a5354b36cdef to your computer and use it in GitHub Desktop.
Save thebeardphantom/28ec29accea44af7dbd6a5354b36cdef to your computer and use it in GitHub Desktop.
Unity doesn't provide an API to get all of the scenes in your build. This script will automatically (at script reload), or manually through the Assets menu, generate a text file that contains all of your enabled scenes in your build settings in a Resources folder. This can then be read at runtime and stored in an array.
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEngine;
public static class SceneListExporter
{
[DidReloadScripts]
[MenuItem("Assets/Generate Scene List")]
private static void GenerateSceneList()
{
if (Application.isPlaying)
{
return;
}
var sceneNames = (from s in EditorBuildSettings.scenes
where s.enabled
select Path.GetFileNameWithoutExtension(s.path)).ToArray();
var path = Path.Combine(Application.dataPath, "Resources");
Directory.CreateDirectory(path);
path = Path.Combine(path, "scenes.txt");
File.WriteAllLines(path, sceneNames);
AssetDatabase.Refresh();
/* Access scenes at runtime:
*
* var sceneList = Resources.Load<TextAsset>("scenes");
* var scenes = sceneList.text.Split('\n');
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment