Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by byerdelen · Jan 24, 2015 at 02:40 PM · editorscene

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image byerdelen · Jan 24, 2015 at 06:28 PM 1
Share

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);
         }
 
     }
avatar image fafase · Jan 24, 2015 at 06:33 PM 0
Share

Good to see you tried.

avatar image
3

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);
         }
 
     }
 }




Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges