Skip to content

Instantly share code, notes, and snippets.

@kurtdekker
Created December 29, 2022 16:04
Show Gist options
  • Save kurtdekker/232f56112a204d574402bae9a7fe3ba3 to your computer and use it in GitHub Desktop.
Save kurtdekker/232f56112a204d574402bae9a7fe3ba3 to your computer and use it in GitHub Desktop.
Perform a change on ALL of your scenes - BE CAREFUL!
using UnityEngine;
using UnityEditor.Callbacks;
using UnityEditor;
using UnityEditor.SceneManagement;
// @kurtdekker - 8:01 AM 12/29/2022
public class PerformChangeOnAllScenes
{
[MenuItem( itemName: "Assets/Perform Change On ALL Scenes")]
public static void DoSomethingToAllScenes()
{
if (!EditorUtility.DisplayDialog(
"WARNING!",
"Doing something severe to ALL scenes!\n\n" +
"BACK UP OR USE SOURCE CONTROL BEFORE RUNNING THIS!!!\n\n" +
"This could destroy everything you have ever worked for. Are you sure you want to do it?",
ok: "DO THE THING!",
cancel: "NOPE!"))
{
return;
}
// - find all scenes
string[] sceneGUIDs = AssetDatabase.FindAssets( "t:scene");
int numScenes = sceneGUIDs.Length;
Debug.Log( System.String.Format( "We found {0} scenes in total.", numScenes));
if (!EditorUtility.DisplayDialog(
"FINAL WARNING!",
System.String.Format( "We found {0} scenes. Here we go...", numScenes) + "\n\n" +
"BACK UP OR USE SOURCE CONTROL BEFORE RUNNING THIS!!!\n\n" +
"This is your LAST CHANCE TO STOP!",
ok: "DO THE THING!",
cancel: "NOPE!"))
{
return;
}
int scenesConsidered = 0;
int scenesProcessed = 0;
int errorCount = 0;
// - iterate and open each Scene
for (int i = 0; i < numScenes; i++)
{
scenesConsidered++;
var sceneName = AssetDatabase.GUIDToAssetPath( sceneGUIDs[i]);
Debug.Log( System.String.Format( "Working on sceneName '{0}'...", sceneName));
var scene = EditorSceneManager.OpenScene( sceneName);
bool SuccessfullyChangedThisScene = false;
// TODO: decide if this scene should or should not be processed here!
if (true)
{
SuccessfullyChangedThisScene = true;
// TODO: put your change-something-in-this-scene code here!
// TODO: if you have some sort of an error...
if (false)
{
errorCount++;
SuccessfullyChangedThisScene = false;
Debug.LogError( System.String.Format( "We had a problem with scene {0}!!", sceneName));
}
}
if (SuccessfullyChangedThisScene)
{
scenesProcessed++;
EditorSceneManager.MarkSceneDirty(scene);
// - resave the scene
EditorSceneManager.SaveScene(scene);
EditorSceneManager.NewScene(NewSceneSetup.EmptyScene);
}
else
{
errorCount++;
}
}
{
string s = "FINAL REPORT ON CHANGING ALL SCENES:\n";
s += System.String.Format( "Considered {0} scenes.\n", scenesConsidered);
s += System.String.Format( "Processed {0} scenes successfully.\n", scenesProcessed);
if (errorCount > 0)
{
s += "<b><color=#ff8080>"; // red
}
else
{
s += "<b><color=#80ff80>"; // green
}
s += System.String.Format( "Unable to change {0} scenes.</color></b>\n", errorCount);
Debug.Log( s);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment