GameObjectExtensions.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. 
  2. namespace GitMerge
  3. {
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEditor;
  7. public static class GameObjectExtensions
  8. {
  9. /// <summary>
  10. /// Adds the copy of a Component to a GameObject.
  11. /// </summary>
  12. /// <param name="go">The GameObject that will get the new Component</param>
  13. /// <param name="original">The original component to copy</param>
  14. /// <returns>The reference to the newly added Component copy</returns>
  15. public static Component AddComponent(this GameObject go, Component original)
  16. {
  17. var newComponent = go.AddComponent(original.GetType());
  18. var originalProperty = new SerializedObject(original).GetIterator();
  19. var newSerializedObject = new SerializedObject(newComponent);
  20. var newProperty = newSerializedObject.GetIterator();
  21. if (originalProperty.Next(true))
  22. {
  23. newProperty.Next(true);
  24. while (originalProperty.NextVisible(true))
  25. {
  26. newProperty.NextVisible(true);
  27. newProperty.SetValue(originalProperty.GetValue());
  28. }
  29. }
  30. newSerializedObject.ApplyModifiedProperties();
  31. return newComponent;
  32. }
  33. /// <summary>
  34. /// Activates/deactivates the GameObjct, and hides it when it is disabled.
  35. /// This is used for "their" objects to hide them while merging.
  36. /// </summary>
  37. /// <param name="go">The object do enable/disable</param>
  38. /// <param name="active">Enable or disable the object?</param>
  39. public static void SetActiveForMerging(this GameObject go, bool active)
  40. {
  41. go.SetActive(active);
  42. go.hideFlags = active ? HideFlags.None : HideFlags.HideAndDontSave;
  43. }
  44. /// <summary>
  45. /// Ping the GameObject in the hierarchy, select it, and center it in the scene view.
  46. /// </summary>
  47. /// <param name="go">The GameObject of interest</param>
  48. public static void Highlight(this GameObject go)
  49. {
  50. // Focussing on the same object twice, zooms in to the coordinate instead of the bounding box.
  51. if (Selection.activeObject == go) {
  52. return;
  53. }
  54. Selection.activeGameObject = go;
  55. EditorGUIUtility.PingObject(go);
  56. var view = SceneView.lastActiveSceneView;
  57. if (view)
  58. {
  59. view.FrameSelected();
  60. }
  61. }
  62. /// <summary>
  63. /// Gets the path of this GameObject in the hierarchy.
  64. /// </summary>
  65. public static string GetPath(this GameObject gameObject)
  66. {
  67. var t = gameObject.transform;
  68. var sb = new StringBuilder(RemovePostfix(t.name));
  69. while (t.parent != null)
  70. {
  71. t = t.parent;
  72. sb.Insert(0, RemovePostfix(t.name) + "/");
  73. }
  74. return sb.ToString();
  75. }
  76. /// <summary>
  77. /// Returns a child of this GameObject that has the same relative path to this GamObject as
  78. /// the given GameObject has to it's root GameObject.
  79. /// </summary>
  80. public static GameObject GetChildWithEqualPath(this GameObject gameObject, GameObject otherGameObject)
  81. {
  82. string fullHierarchyPath = otherGameObject.GetPath();
  83. string relativeHierarchyPath = fullHierarchyPath.Substring(fullHierarchyPath.IndexOf("/", 1) + 1);
  84. Transform result = gameObject.transform.Find(relativeHierarchyPath);
  85. return result != null ? result.gameObject : gameObject; // fallback to root if a GameObject with equal path doesn't exist
  86. }
  87. private static string RemovePostfix(string name)
  88. {
  89. if (name.EndsWith(MergeManagerBase.THEIR_FILE_POSTFIX))
  90. {
  91. return name.Substring(0, name.Length - MergeManagerBase.THEIR_FILE_POSTFIX.Length);
  92. }
  93. return name;
  94. }
  95. }
  96. }