Skip to content

Instantly share code, notes, and snippets.

@marinho
Created February 17, 2022 06:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marinho/b5e7dd912f0ef695d82ca57483ee6cdd to your computer and use it in GitHub Desktop.
Save marinho/b5e7dd912f0ef695d82ca57483ee6cdd to your computer and use it in GitHub Desktop.
Class to add all scenes in a Unity project into build settings. This is simpler than adding them manually one by one using UI.
#if UNITY_EDITOR
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using UnityEngine;
using UnityEditor;
namespace Twelve
{
public class UniyGameScenesInBulk : EditorWindow
{
[MenuItem("Tools/Twelve/Add Game Scenes", false, 0)]
public static void AddTwelveScenes()
{
List<EditorBuildSettingsScene> editorBuildSettingsScenes = new List<EditorBuildSettingsScene>(EditorBuildSettings.scenes);
var sceneFiles = GetScenesInFolder("Game");
foreach (var sceneFile in sceneFiles) {
AddOrEnableSceneToSettings(editorBuildSettingsScenes, sceneFile);
}
EditorBuildSettings.scenes = editorBuildSettingsScenes.ToArray();
}
static void AddOrEnableSceneToSettings(List<EditorBuildSettingsScene> scenes, string path) {
var existing = scenes.Find(s => s.path == path);
if (existing == null) {
EditorBuildSettingsScene sceneToAdd = new EditorBuildSettingsScene(path, true);
scenes.Add(sceneToAdd);
UnityEngine.Debug.Log($"Scene '{path}' was added.");
}
else if (!existing.enabled) {
existing.enabled = true;
UnityEngine.Debug.Log($"Scene '{path}' was enabled.");
}
}
static List<string> GetScenesInFolder(string scenesFolder) {
string assetsFolder = Application.dataPath;
string fullPath = $"{assetsFolder}/{scenesFolder}";
string findCmd = $"find {fullPath} -name '*.unity'";
var outputLines = new List<string>(ExecuteProcessTerminal(findCmd).Split('\n'));
var sceneFiles = outputLines.Select(s => s.Replace($"{assetsFolder}", "Assets")).ToList();
var notEmptySceneFiles = sceneFiles.FindAll(f => f.Trim() != "");
return notEmptySceneFiles;
}
static string ExecuteProcessTerminal(string argument)
{
try
{
// UnityEngine.Debug.Log("============== Start Executing [" + argument + "] ===============");
ProcessStartInfo startInfo = new ProcessStartInfo()
{
FileName = "/bin/bash",
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow = true,
Arguments = " -c \"" + argument + " \""
};
Process myProcess = new Process
{
StartInfo = startInfo
};
myProcess.Start();
string output = myProcess.StandardOutput.ReadToEnd();
// UnityEngine.Debug.Log(output); // GetStringResult
myProcess.WaitForExit();
// UnityEngine.Debug.Log("============== End ===============");
return output;
}
catch (Exception e)
{
UnityEngine.Debug.Log(e);
return "";
}
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment