AppyPrefabs.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using UnityEditor;
  2. using UnityEngine;
  3. public class AppyPrefabs : Editor
  4. {
  5. [@MenuItem("Tools/BatchExportPrefab")]
  6. static void BatchExportPrefab()
  7. {
  8. GameObject[] objs = Selection.gameObjects;
  9. foreach (GameObject item in objs)
  10. {
  11. if (item == null) continue;
  12. GameObject currobj = PrefabUtility.FindRootGameObjectWithSameParentPrefab(item);
  13. if (currobj == null) continue;
  14. PrefabType pt = PrefabUtility.GetPrefabType(currobj);
  15. if (pt == PrefabType.None) continue;
  16. #if UNITY_2018_2_OR_NEWER
  17. Object tg = PrefabUtility.GetCorrespondingObjectFromSource(currobj);
  18. #else
  19. Object tg = PrefabUtility.GetPrefabParent(currobj);
  20. #endif
  21. if (tg == null) continue;
  22. //Debug.Log(currobj.name + "====" + tg.name);
  23. PrefabUtility.ReplacePrefab(currobj, tg, ReplacePrefabOptions.ConnectToPrefab);
  24. PrefabUtility.ResetToPrefabState(currobj);
  25. //重设后有子节点,去掉,在导出后不会影响已经加过子节的Hierarchy Object
  26. //PrefabUtility.RevertPrefabInstance(currobj);
  27. }
  28. AssetDatabase.Refresh();
  29. }
  30. [@MenuItem("Tools/BatchRestPrefab")]
  31. static void BatchRestPrefab()
  32. {
  33. GameObject[] objs = Selection.gameObjects;
  34. foreach (GameObject item in objs)
  35. {
  36. if (item == null) continue;
  37. GameObject currobj = PrefabUtility.FindRootGameObjectWithSameParentPrefab(item);
  38. if (currobj == null) continue;
  39. Debug.Log(currobj.name);
  40. PrefabUtility.ReconnectToLastPrefab(currobj);//must reconnect
  41. PrefabUtility.ResetToPrefabState(currobj);
  42. PrefabUtility.RevertPrefabInstance(currobj);//会全部重设
  43. }
  44. AssetDatabase.Refresh();
  45. }
  46. }