- Home /
Add/Remove object to all scenes in editor
Hi, I want to destroy a specific Gameobject(finding by name) in all my 100 scenes and add another one. Does anyone know editor code that sweeps through the added scenes so I can create this functionality? I couldn't find such document
Thanks
Answer by fafase · Jan 24, 2015 at 06:21 PM
I usually don't provide full scripts but I wanted to try that one so here it is. You still get an error for YAML parsing but I think it gets fixed while running the scenes...not sure though you tell me. This is meant to be in the Editor folder under Assets. It will show a button in the menu bar on top under Object. Then it opens a GUI window and asks for the name of the object to
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
public class RemoveObject : EditorWindow
{
[MenuItem("Object/Remove object")]
static void Init()
{
// Get existing open window or if none, make a new one:
ChangeCustomerHelper window = (ChangeCustomerHelper)EditorWindow.GetWindow(typeof(ChangeCustomerHelper));
}
string objectToRemove = "";
string prefab = "";
void OnGUI()
{
prefab = EditorGUILayout.TextField("Prefab:", prefab);
objectToRemove = EditorGUILayout.TextField("Object to remove:", objectToRemove);
if (objectToRemove.Length > 0 && prefab.Length > 0)
{
if (GUILayout.Button("Remove " + objectToRemove))
{
RemoveAll();
this.Close();
}
}
}
private void RemoveAll()
{
string[] fileEntries = Directory.GetFiles("C:/Users/Lucas/Documents/New Unity Project 2/Assets/Scene");
List<string> scenes = new List<string>();
foreach (string str in fileEntries)
{
if (str.Contains("Level") && str.Contains(".unity"))
{
scenes.Add(str);
}
}
foreach (string str in scenes)
{
EditorApplication.OpenScene(str);
GameObject objs = GameObject.Find(objectToRemove);
if (objs != null)
{
DestroyImmediate(objs);
GameObject obj = Resources.Load("Prefab") as GameObject;
Instantiate(obj);
EditorApplication.SaveScene(str);
}
}
}
}
Now what it does (and I hope you will read that as well...)
First you need to change the path where there is my own and put the one for your Scene folder. On press, it goes down that folder and collect all file with Level and .unity. If you have a different naming you can change Level or even add an extra variable to the GUI.
Then it goes through all of those string it found matching the two and those are the scene you want to modify.
It opens the scene (the current scene wont be saved so make sure about that or add a save first). Then it looks for a game object named as you provided, then if it found one, it deletes it and go into the Resource folder to find the prefab as provided and add it to the scene.
Now you should be able to modify it, in case the object is not found and you want to add the prefab. Finally, it saves that scene before getting to the next one.
All in all, I guess you get the idea. Up to you now.
Thanks a lot for your help, After many search, I came out with the same method, and well, even inspired from the same method and changed it to my liking :) It removed the relevant objects from the 220 busy scenes in 3 $$anonymous$$utes or so.
using UnityEngine;
using System.Collections.Generic;
using UnityEditor;
public class ReadSceneNames : $$anonymous$$onoBehaviour
{
[$$anonymous$$enuItem("Debug/ReadSceneNames")]
public static void ReadNames()
{
foreach (EditorBuildSettingsScene S in EditorBuildSettings.scenes)
{
EditorApplication.OpenScene(S.path);
object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject));
foreach (object o in obj)
{
if (o != null)
{
GameObject g = (GameObject) o;
if (g != null && (g.name == "LoadingScreen System" || g.name == "Purchase System" || g.name == "GameCenter System" || g.name == "Registration System"))
{
DestroyImmediate(g);
}
}
}
EditorApplication.SaveScene(S.path);
}
}
Answer by sfilo · Oct 17, 2016 at 10:50 AM
I used the code samples in this thread to create an editor script that allows to remove and add objects from scenes. You select the scenes in the project view and drag them to the dialog, you select the objects to delete and objects to add and you run the script. This thread was very helpful to me so here's the script for anyone who needs it.
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.Collections;
public class AddRemoveObjectsInScenes: ScriptableWizard
{
// public EditorBuildSettingsScene[] scenes;
public GameObject[] outObjects;
public GameObject[] inObjects;
public Object[] scenes;
[MenuItem("Tools/Selection/Bulk Edit Scenes")]
static void CreateWizard()
{
ScriptableWizard.DisplayWizard("Edit Scenes", typeof(AddRemoveObjectsInScenes), "Run");
}
void OnWizardCreate()
{
foreach (Object s in scenes)
{
string path = AssetDatabase.GetAssetPath(s);
EditorApplication.OpenScene(path);
Object[] obj = GameObject.FindSceneObjectsOfType(typeof (GameObject));
foreach (Object o in obj)
{
if (o != null)
{
GameObject g = (GameObject) o;
Object foundPrefab = PrefabUtility.GetPrefabObject(o);
if (g != null && PrefabUtility.GetPrefabObject(o) != null)
{
// remove old objects
foreach (GameObject oldObject in outObjects)
{
Object foundPrefabRoot = PrefabUtility.FindPrefabRoot(g);
Object foundPrefabParent = PrefabUtility.GetPrefabParent(foundPrefabRoot);
Object oldPrefabRoot = PrefabUtility.FindPrefabRoot(oldObject);
if (foundPrefabParent == oldPrefabRoot)
{
Debug.Log("Deleted:"+ foundPrefabParent);
GameObject.DestroyImmediate(foundPrefabRoot);
}
}
}
}
}
// add new objects
foreach (GameObject newObject in inObjects)
{
PrefabUtility.InstantiatePrefab(newObject);
}
EditorApplication.SaveScene(path);
}
}
}
Your answer
Follow this Question
Related Questions
Drawing Handles 1 Answer
cannot open a unity scene 0 Answers
'Resource file has already been unloaded' error when exiting application in Editor 1 Answer
Editor scene preview window 0 Answers
Why does object movement get saved to scene editor? 2 Answers