Advertisement
Guest User

Untitled

a guest
Sep 29th, 2016
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class ReplacePrefab : EditorWindow
  5. {
  6. public delegate void ApplyOrRevert(GameObject _goCurrentGo, Object _ObjPrefabParent, ReplacePrefabOptions _eReplaceOptions);
  7. [MenuItem("Tools/Apply all selected prefabs %#a")]
  8. static void ApplyPrefabs()
  9. {
  10. SearchPrefabConnections(ApplyToSelectedPrefabs);
  11. }
  12.  
  13. [MenuItem("Tools/Revert all selected prefabs %#r")]
  14. static void ResetPrefabs()
  15. {
  16. SearchPrefabConnections(RevertToSelectedPrefabs);
  17. }
  18.  
  19. //Look for connections
  20. static void SearchPrefabConnections(ApplyOrRevert _applyOrRevert)
  21. {
  22. GameObject[] tSelection = Selection.gameObjects;
  23.  
  24. if (tSelection.Length > 0)
  25. {
  26. GameObject goPrefabRoot;
  27. GameObject goParent;
  28. GameObject goCur;
  29. bool bTopHierarchyFound;
  30. int iCount = 0;
  31. PrefabType prefabType;
  32. bool bCanApply;
  33. //Iterate through all the selected gameobjects
  34. foreach (GameObject go in tSelection)
  35. {
  36. prefabType = PrefabUtility.GetPrefabType(go);
  37. //Is the selected gameobject a prefab?
  38. if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance)
  39. {
  40. //Prefab Root;
  41. goPrefabRoot = ((GameObject)PrefabUtility.GetPrefabParent(go)).transform.root.gameObject;
  42. goCur = go;
  43. bTopHierarchyFound = false;
  44. bCanApply = true;
  45. //We go up in the hierarchy to apply the root of the go to the prefab
  46. while (goCur.transform.parent != null && !bTopHierarchyFound)
  47. {
  48. //Are we still in the same prefab?
  49. if (PrefabUtility.GetPrefabParent(goCur.transform.parent.gameObject) != null && (goPrefabRoot == ((GameObject)PrefabUtility.GetPrefabParent(goCur.transform.parent.gameObject)).transform.root.gameObject))
  50. {
  51. goCur = goCur.transform.parent.gameObject;
  52. }
  53. else
  54. {
  55. //The gameobject parent is another prefab, we stop here
  56. bTopHierarchyFound = true;
  57. if (goPrefabRoot != ((GameObject)PrefabUtility.GetPrefabParent(goCur)))
  58. {
  59. //Gameobject is part of another prefab
  60. bCanApply = false;
  61. }
  62. }
  63. }
  64.  
  65. if (_applyOrRevert != null && bCanApply)
  66. {
  67. iCount++;
  68. _applyOrRevert(goCur, PrefabUtility.GetPrefabParent(goCur), ReplacePrefabOptions.ConnectToPrefab);
  69. }
  70. }
  71. }
  72. Debug.Log(iCount + " prefab" + (iCount > 1 ? "s" : "") + " updated");
  73. }
  74. }
  75.  
  76. //Apply
  77. static void ApplyToSelectedPrefabs(GameObject _goCurrentGo, Object _ObjPrefabParent, ReplacePrefabOptions _eReplaceOptions)
  78. {
  79. PrefabUtility.ReplacePrefab(_goCurrentGo, _ObjPrefabParent, _eReplaceOptions);
  80. }
  81.  
  82. //Revert
  83. static void RevertToSelectedPrefabs(GameObject _goCurrentGo, Object _ObjPrefabParent, ReplacePrefabOptions _eReplaceOptions)
  84. {
  85. PrefabUtility.ReconnectToLastPrefab(_goCurrentGo);
  86. PrefabUtility.RevertPrefabInstance(_goCurrentGo);
  87. }
  88.  
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement