#if UNITY_EDITOR using UnityEngine; using UnityEditor; using System.Linq; using System.Collections.Generic; namespace O3DWB { public static class ObjectActions { #region Public Static Functions public static void AssignObjectsToGroup(IEnumerable gameObjects, ObjectGroup objectGroup) { var roots = Octave3DWorldBuilder.ActiveInstance.GetRoots(gameObjects); foreach(var root in roots) { UndoEx.SetTransformParent(root.transform, objectGroup.GroupObject.transform); } } public static List Duplicate(IEnumerable sourceObjects) { List sourceParents = Octave3DWorldBuilder.ActiveInstance.GetRoots(sourceObjects); var clonedObjects = new List(); foreach (GameObject parent in sourceParents) { GameObject prefab = parent.GetSourcePrefab(); Transform parentTransform = parent.transform; if (prefab == null) { GameObject clonedParent = parent.CloneAsWorkingObject(parentTransform.parent); //clonedParent.transform.parent = parent.transform.parent; clonedObjects.AddRange(clonedParent.GetAllChildrenIncludingSelf()); Octave3DScene.Get().RegisterObjectHierarchy(clonedParent); } else { GameObject clonedParent = ObjectInstantiation.InstantiateObjectHierarchyFromPrefab(prefab, parentTransform.position, parentTransform.rotation, parentTransform.lossyScale); clonedObjects.AddRange(clonedParent.GetAllChildrenIncludingSelf()); Octave3DScene.Get().RegisterObjectHierarchy(clonedParent); } } SceneViewCamera.Instance.SetObjectVisibilityDirty(); return clonedObjects; } public static List ReplaceGameObjectHierarchyCollectionPrefab(List gameObjectCollection, GameObject newPrefab) { var newObjects = new List(); List roots = Octave3DWorldBuilder.ActiveInstance.GetRoots(gameObjectCollection); foreach (GameObject gameObject in roots) { GameObject newObject = ReplaceGameObjectHierarchyPrefab(gameObject, newPrefab); if (newObject != null) newObjects.Add(newObject); } return newObjects; } public static GameObject ReplaceGameObjectHierarchyPrefab(GameObject gameObject, GameObject newPrefab) { if (gameObject == null) return null; var prefabType = PrefabUtility.GetPrefabAssetType(newPrefab); if (prefabType == PrefabAssetType.NotAPrefab || prefabType == PrefabAssetType.Model) return null; // Store any needed object data OrientedBox originalWorldOrientedBox = gameObject.GetHierarchyWorldOrientedBox(); if (originalWorldOrientedBox.IsInvalid()) return null; int originalObjectLayer = gameObject.layer; bool isObjectStatic = gameObject.isStatic; Transform originalObjectTransform = gameObject.transform; Vector3 worldScale = originalObjectTransform.lossyScale; Quaternion worldRotation = originalObjectTransform.rotation; // Create a new game object from the specified prefab GameObject newObject = PrefabUtility.InstantiatePrefab(newPrefab) as GameObject; if (newObject != null) { // Register the created object for Undo and set its transform data. Also store any significant // data that the original object had before it was destroyed. UndoEx.RegisterCreatedGameObject(newObject); Transform newObjectTransform = newObject.transform; newObjectTransform.localScale = worldScale; newObjectTransform.rotation = worldRotation; newObjectTransform.parent = originalObjectTransform.transform.parent; newObject.layer = originalObjectLayer; newObject.isStatic = isObjectStatic; // We will adjust the new object's position such that its center is the same as the // one the original object had. This produces better results especially when the new // object is a multi-level object hierarchy. OrientedBox newHierarchyWorldOrientedBox = newObject.GetHierarchyWorldOrientedBox(); if(newHierarchyWorldOrientedBox.IsInvalid()) { GameObject.DestroyImmediate(newObject); return null; } newObjectTransform.position = ObjectPositionCalculator.CalculateObjectHierarchyPosition(newPrefab, originalWorldOrientedBox.Center, worldScale, worldRotation); // We will also inform the scene that a new object was created so that all the necessary steps can be performed Octave3DScene.Get().RegisterObjectHierarchy(newObject); // Destroy the old object UndoEx.DestroyObjectImmediate(gameObject); return newObject; } return null; } public static void HideObjects(List gameObjects) { foreach(GameObject gameObject in gameObjects) { gameObject.SetActive(false); } } public static void ShowObjects(List gameObjects) { foreach(GameObject gameObject in gameObjects) { gameObject.SetActive(true); } } public static void MakeObjectsStatic(List gameObjects) { foreach (GameObject gameObject in gameObjects) { gameObject.isStatic = true; } } public static void MakeObjectsDynamic(List gameObjects) { foreach (GameObject gameObject in gameObjects) { gameObject.isStatic = false; } } public static void EraseGameObjectsInAllLayers() { List allGameObjectsInAllLayers = ObjectLayerDatabase.Get().GetAllGameObjectsInAllLayers(); ObjectErase.EraseGameObjectCollection(allGameObjectsInAllLayers); } public static void EraseAllGameObjectsInLayer(int objectLayer) { List allGameObjectsInLayer = ObjectLayerDatabase.Get().GetAllGameObjectsInLayer(objectLayer); ObjectErase.EraseGameObjectCollection(allGameObjectsInLayer); } #endregion } } #endif