Search Unity

Script to replace gameobjects with prefab

Discussion in 'Assets and Asset Store' started by dubiduboni_unity, Feb 23, 2019.

  1. dubiduboni_unity

    dubiduboni_unity

    Joined:
    Feb 11, 2019
    Posts:
    116
    The script must be inside the Assets\Editor it does not matter where you create the Editor folder but it must be inside the Editor folder to work !

    Added a search box you can search for multiple objects and then replace all of them at once.
    Or replacing selected objects from the hierarchy.

    I added also a undo option also option to keep the names of the objects.
    It will keep the index place and order in the hierarchy.
    It will keep the order and place and also the scene it was replaced at if you have more then one scenes and you selected objects from some scenes.
    Keep position,scaling,rotation.



    Code (csharp):
    1.  
    2. using System;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using System.IO;
    6. using System.Linq;
    7. using UnityEditor;
    8. using UnityEngine;
    9. using UnityEngine.SceneManagement;
    10. using UnityEngine.UI;
    11.  
    12. public class PrefabReplace : EditorWindow
    13. {
    14.     [SerializeField] private GameObject prefab;
    15.     private bool selectionChanged;
    16.     private string objectsToSearch = "";
    17.     private List<GameObject> foundObjects = new List<GameObject>();
    18.     private GUIStyle guiStyle = new GUIStyle(); //create a new variable
    19.     private int count = 0;
    20.     private bool addFoundObjects;
    21.     private bool keepNames = true;
    22.  
    23.     [MenuItem("Tools/Prefab Replace")]
    24.     static void CreateReplaceWithPrefab()
    25.     {
    26.         int width = 340;
    27.         int height = 370;
    28.  
    29.         int x = (Screen.currentResolution.width - width) / 2;
    30.         int y = (Screen.currentResolution.height - height) / 2;
    31.        
    32.         GetWindow<PrefabReplace>().position = new Rect(x, y, width, height);
    33.     }
    34.  
    35.     private void OnGUI()
    36.     {
    37.         guiStyle.fontSize = 15; //change the font size
    38.         Searching();
    39.         GUILayout.Space(10);
    40.         Replacing();
    41.         GUILayout.Space(50);
    42.         Settings();
    43.     }
    44.  
    45.     private void Searching()
    46.     {
    47.         //GUI.Label(new Rect(10, 15, 150, 20), "Search by name", guiStyle);
    48.         objectsToSearch = GUI.TextField(new Rect(90, 35, 150, 20), objectsToSearch, 25);
    49.  
    50.         if (objectsToSearch != "")
    51.         {
    52.             GUI.enabled = true;
    53.         }
    54.         else
    55.         {
    56.             GUI.enabled = false;
    57.             count = 0;
    58.         }
    59.         GUILayout.Space(15);
    60.         if (GUILayout.Button("Search"))
    61.         {
    62.             foundObjects = new List<GameObject>();
    63.             count = 0;
    64.  
    65.             foreach (GameObject gameObj in GameObject.FindObjectsOfType<GameObject>())
    66.             {
    67.                 if (gameObj.name == objectsToSearch)//.Contains(objectsToSearch))// == objectsToSearch)
    68.                 {
    69.                     count += 1;
    70.                     foundObjects.Add(gameObj);
    71.                     foreach (Transform child in gameObj.transform)
    72.                     {
    73.                         count += 1;
    74.                         foundObjects.Add(child.gameObject);
    75.                     }
    76.                 }
    77.             }
    78.  
    79.             if(foundObjects.Count == 0)
    80.             {
    81.                 count = 0;
    82.             }
    83.         }
    84.  
    85.         GUI.enabled = true;
    86.         EditorGUI.LabelField(new Rect(90, 65, 210, 15), "Number of found objects and childs");
    87.         GUI.TextField(new Rect(90, 80, 60, 15), count.ToString(), 25);
    88.  
    89.         GUILayout.Space(100);
    90.         if (count > 0)
    91.         {
    92.             GUI.enabled = true;
    93.         }
    94.         else
    95.         {
    96.             GUI.enabled = false;
    97.         }
    98.         if (GUILayout.Button("Replace found objects"))
    99.         {
    100.             if (prefab != null)      
    101.             {
    102.                 InstantiatePrefab(foundObjects);
    103.             }
    104.         }
    105.  
    106.         GUI.enabled = true;
    107.     }
    108.  
    109.     private void Replacing()
    110.     {
    111.         GUILayout.Space(20);
    112.         GUILayout.BeginVertical(GUI.skin.box);
    113.         GUILayout.Label("Replacing");
    114.         GUILayout.Space(20);
    115.  
    116.         prefab = (GameObject)EditorGUILayout.ObjectField("Prefab", prefab, typeof(GameObject), false);
    117.  
    118.         var selection = Selection.objects.OfType<GameObject>().ToList();
    119.  
    120.         if (selectionChanged)
    121.         {
    122.             if (selection.Count == 0)
    123.                 GUI.enabled = false;
    124.  
    125.             for (var i = selection.Count - 1; i >= 0; --i)
    126.             {
    127.                 var selectedObject = selection[i];
    128.                 if (prefab != null && selection.Count > 0 &&
    129.                     selectedObject.scene.name != null
    130.                     && prefab != PrefabUtility
    131.                     .GetCorrespondingObjectFromSource(selectedObject))
    132.                 {
    133.                     GUI.enabled = true;
    134.                 }
    135.                 else
    136.                 {
    137.                     GUI.enabled = false;
    138.                 }
    139.             }
    140.         }
    141.         else
    142.         {
    143.             GUI.enabled = false;
    144.         }
    145.  
    146.         if (GUILayout.Button("Replace"))
    147.         {
    148.             InstantiatePrefab(selection);
    149.             selectionChanged = false;
    150.         }
    151.  
    152.         GUILayout.Space(10);
    153.         GUI.enabled = true;
    154.         EditorGUILayout.LabelField("Selection count: " + Selection.objects.OfType<GameObject>().Count());
    155.  
    156.         GUILayout.EndVertical();
    157.     }
    158.  
    159.     private void Settings()
    160.     {
    161.         keepNames = GUILayout.Toggle(keepNames, "Keep Names");
    162.     }
    163.  
    164.     private void OnInspectorUpdate()
    165.     {
    166.         Repaint();
    167.     }
    168.  
    169.     private void OnSelectionChange()
    170.     {
    171.         selectionChanged = true;
    172.     }
    173.  
    174.     private void InstantiatePrefab(List<GameObject> selection)
    175.     {
    176.         if (prefab != null && selection.Count > 0)
    177.         {
    178.             for (var i = selection.Count - 1; i >= 0; --i)
    179.             {
    180.                 var selected = selection[i];
    181.                 Component[] components = selected.GetComponents(typeof(MonoBehaviour));
    182.                 if (components.Length == 0)
    183.                 {
    184.                     SceneManager.SetActiveScene(SceneManager.GetSceneByName(selected.scene.name));
    185.  
    186.                     var prefabType = PrefabUtility.GetPrefabAssetType(prefab);
    187.                     GameObject newObject;
    188.  
    189.                     if (prefabType == PrefabAssetType.Regular)
    190.                     {
    191.                         newObject = (GameObject)PrefabUtility.InstantiatePrefab(prefab);
    192.                     }
    193.                     else
    194.                     {
    195.                         newObject = Instantiate(prefab);
    196.  
    197.                         if (keepNames == false)
    198.                         {
    199.                             newObject.name = prefab.name;
    200.                         }
    201.                     }
    202.                     if (newObject == null)
    203.                     {
    204.                         Debug.LogError("Error instantiating prefab");
    205.                         break;
    206.                     }
    207.  
    208.                     Undo.RegisterCreatedObjectUndo(newObject, "Replace With Prefabs");
    209.                     newObject.transform.parent = selected.transform.parent;
    210.                     newObject.transform.localPosition = selected.transform.localPosition;
    211.                     newObject.transform.localRotation = selected.transform.localRotation;
    212.                     newObject.transform.localScale = selected.transform.localScale;
    213.                     newObject.transform.SetSiblingIndex(selected.transform.GetSiblingIndex());
    214.                     if (keepNames == true)
    215.                     {
    216.                         newObject.name = selected.name;
    217.                     }
    218.                     Undo.DestroyObjectImmediate(selected);
    219.                 }
    220.             }
    221.         }
    222.     }
    223. }
    224.  
    225.  
     
  2. namdo

    namdo

    Joined:
    Feb 23, 2015
    Posts:
    200
    Thanks for this.
     
  3. notaspyaraceku

    notaspyaraceku

    Joined:
    Dec 18, 2018
    Posts:
    2
    thank you it's great <3