Advertisement
Bunny83

PrefabNameRevert

Mar 25th, 2018
1,734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.26 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4.  
  5. public class PrefabNameRevert
  6. {
  7.     [MenuItem("Prefab/RevertName")]
  8.     static void RevertPrefabNames()
  9.     {
  10.         RevertAllNames(Selection.GetFiltered(typeof(GameObject), SelectionMode.OnlyUserModifiable));
  11.     }
  12.  
  13.     public static void RemoveNameModification(UnityEngine.Object aObj)
  14.     {
  15.         var mods = new List<PropertyModification>(PrefabUtility.GetPropertyModifications(aObj));
  16.         for (int i = 0; i < mods.Count; i++)
  17.         {
  18.             if (mods[i].propertyPath == "m_Name")
  19.                 mods.RemoveAt(i);
  20.         }
  21.         PrefabUtility.SetPropertyModifications(aObj, mods.ToArray());
  22.     }
  23.     public static void RevertAllNames(Object[] aObjects)
  24.     {
  25.         var items = new List<Object>();
  26.         foreach (var item in aObjects)
  27.         {
  28.             var prefab = PrefabUtility.GetPrefabParent(item);
  29.             if (prefab != null)
  30.             {
  31.                 Undo.RecordObject(item, "Revert perfab name");
  32.                 item.name = prefab.name;
  33.                 items.Add(item);
  34.             }
  35.         }
  36.         Undo.FlushUndoRecordObjects();
  37.         foreach (var item in items)
  38.         {
  39.             RemoveNameModification(item);
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement