ObjectActions.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #if UNITY_EDITOR
  2. using UnityEngine;
  3. using UnityEditor;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6. namespace O3DWB
  7. {
  8. public static class ObjectActions
  9. {
  10. #region Public Static Functions
  11. public static void AssignObjectsToGroup(IEnumerable<GameObject> gameObjects, ObjectGroup objectGroup)
  12. {
  13. var roots = Octave3DWorldBuilder.ActiveInstance.GetRoots(gameObjects);
  14. foreach(var root in roots)
  15. {
  16. UndoEx.SetTransformParent(root.transform, objectGroup.GroupObject.transform);
  17. }
  18. }
  19. public static List<GameObject> Duplicate(IEnumerable<GameObject> sourceObjects)
  20. {
  21. List<GameObject> sourceParents = Octave3DWorldBuilder.ActiveInstance.GetRoots(sourceObjects);
  22. var clonedObjects = new List<GameObject>();
  23. foreach (GameObject parent in sourceParents)
  24. {
  25. GameObject prefab = parent.GetSourcePrefab();
  26. Transform parentTransform = parent.transform;
  27. if (prefab == null)
  28. {
  29. GameObject clonedParent = parent.CloneAsWorkingObject(parentTransform.parent);
  30. //clonedParent.transform.parent = parent.transform.parent;
  31. clonedObjects.AddRange(clonedParent.GetAllChildrenIncludingSelf());
  32. Octave3DScene.Get().RegisterObjectHierarchy(clonedParent);
  33. }
  34. else
  35. {
  36. GameObject clonedParent = ObjectInstantiation.InstantiateObjectHierarchyFromPrefab(prefab, parentTransform.position, parentTransform.rotation, parentTransform.lossyScale);
  37. clonedObjects.AddRange(clonedParent.GetAllChildrenIncludingSelf());
  38. Octave3DScene.Get().RegisterObjectHierarchy(clonedParent);
  39. }
  40. }
  41. SceneViewCamera.Instance.SetObjectVisibilityDirty();
  42. return clonedObjects;
  43. }
  44. public static List<GameObject> ReplaceGameObjectHierarchyCollectionPrefab(List<GameObject> gameObjectCollection, GameObject newPrefab)
  45. {
  46. var newObjects = new List<GameObject>();
  47. List<GameObject> roots = Octave3DWorldBuilder.ActiveInstance.GetRoots(gameObjectCollection);
  48. foreach (GameObject gameObject in roots)
  49. {
  50. GameObject newObject = ReplaceGameObjectHierarchyPrefab(gameObject, newPrefab);
  51. if (newObject != null) newObjects.Add(newObject);
  52. }
  53. return newObjects;
  54. }
  55. public static GameObject ReplaceGameObjectHierarchyPrefab(GameObject gameObject, GameObject newPrefab)
  56. {
  57. if (gameObject == null) return null;
  58. var prefabType = PrefabUtility.GetPrefabAssetType(newPrefab);
  59. if (prefabType == PrefabAssetType.NotAPrefab || prefabType == PrefabAssetType.Model) return null;
  60. // Store any needed object data
  61. OrientedBox originalWorldOrientedBox = gameObject.GetHierarchyWorldOrientedBox();
  62. if (originalWorldOrientedBox.IsInvalid()) return null;
  63. int originalObjectLayer = gameObject.layer;
  64. bool isObjectStatic = gameObject.isStatic;
  65. Transform originalObjectTransform = gameObject.transform;
  66. Vector3 worldScale = originalObjectTransform.lossyScale;
  67. Quaternion worldRotation = originalObjectTransform.rotation;
  68. // Create a new game object from the specified prefab
  69. GameObject newObject = PrefabUtility.InstantiatePrefab(newPrefab) as GameObject;
  70. if (newObject != null)
  71. {
  72. // Register the created object for Undo and set its transform data. Also store any significant
  73. // data that the original object had before it was destroyed.
  74. UndoEx.RegisterCreatedGameObject(newObject);
  75. Transform newObjectTransform = newObject.transform;
  76. newObjectTransform.localScale = worldScale;
  77. newObjectTransform.rotation = worldRotation;
  78. newObjectTransform.parent = originalObjectTransform.transform.parent;
  79. newObject.layer = originalObjectLayer;
  80. newObject.isStatic = isObjectStatic;
  81. // We will adjust the new object's position such that its center is the same as the
  82. // one the original object had. This produces better results especially when the new
  83. // object is a multi-level object hierarchy.
  84. OrientedBox newHierarchyWorldOrientedBox = newObject.GetHierarchyWorldOrientedBox();
  85. if(newHierarchyWorldOrientedBox.IsInvalid())
  86. {
  87. GameObject.DestroyImmediate(newObject);
  88. return null;
  89. }
  90. newObjectTransform.position = ObjectPositionCalculator.CalculateObjectHierarchyPosition(newPrefab, originalWorldOrientedBox.Center, worldScale, worldRotation);
  91. // We will also inform the scene that a new object was created so that all the necessary steps can be performed
  92. Octave3DScene.Get().RegisterObjectHierarchy(newObject);
  93. // Destroy the old object
  94. UndoEx.DestroyObjectImmediate(gameObject);
  95. return newObject;
  96. }
  97. return null;
  98. }
  99. public static void HideObjects(List<GameObject> gameObjects)
  100. {
  101. foreach(GameObject gameObject in gameObjects)
  102. {
  103. gameObject.SetActive(false);
  104. }
  105. }
  106. public static void ShowObjects(List<GameObject> gameObjects)
  107. {
  108. foreach(GameObject gameObject in gameObjects)
  109. {
  110. gameObject.SetActive(true);
  111. }
  112. }
  113. public static void MakeObjectsStatic(List<GameObject> gameObjects)
  114. {
  115. foreach (GameObject gameObject in gameObjects)
  116. {
  117. gameObject.isStatic = true;
  118. }
  119. }
  120. public static void MakeObjectsDynamic(List<GameObject> gameObjects)
  121. {
  122. foreach (GameObject gameObject in gameObjects)
  123. {
  124. gameObject.isStatic = false;
  125. }
  126. }
  127. public static void EraseGameObjectsInAllLayers()
  128. {
  129. List<GameObject> allGameObjectsInAllLayers = ObjectLayerDatabase.Get().GetAllGameObjectsInAllLayers();
  130. ObjectErase.EraseGameObjectCollection(allGameObjectsInAllLayers);
  131. }
  132. public static void EraseAllGameObjectsInLayer(int objectLayer)
  133. {
  134. List<GameObject> allGameObjectsInLayer = ObjectLayerDatabase.Get().GetAllGameObjectsInLayer(objectLayer);
  135. ObjectErase.EraseGameObjectCollection(allGameObjectsInLayer);
  136. }
  137. #endregion
  138. }
  139. }
  140. #endif