123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #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<GameObject> gameObjects, ObjectGroup objectGroup)
- {
- var roots = Octave3DWorldBuilder.ActiveInstance.GetRoots(gameObjects);
- foreach(var root in roots)
- {
- UndoEx.SetTransformParent(root.transform, objectGroup.GroupObject.transform);
- }
- }
- public static List<GameObject> Duplicate(IEnumerable<GameObject> sourceObjects)
- {
- List<GameObject> sourceParents = Octave3DWorldBuilder.ActiveInstance.GetRoots(sourceObjects);
- var clonedObjects = new List<GameObject>();
- 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<GameObject> ReplaceGameObjectHierarchyCollectionPrefab(List<GameObject> gameObjectCollection, GameObject newPrefab)
- {
- var newObjects = new List<GameObject>();
- List<GameObject> 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<GameObject> gameObjects)
- {
- foreach(GameObject gameObject in gameObjects)
- {
- gameObject.SetActive(false);
- }
- }
- public static void ShowObjects(List<GameObject> gameObjects)
- {
- foreach(GameObject gameObject in gameObjects)
- {
- gameObject.SetActive(true);
- }
- }
- public static void MakeObjectsStatic(List<GameObject> gameObjects)
- {
- foreach (GameObject gameObject in gameObjects)
- {
- gameObject.isStatic = true;
- }
- }
- public static void MakeObjectsDynamic(List<GameObject> gameObjects)
- {
- foreach (GameObject gameObject in gameObjects)
- {
- gameObject.isStatic = false;
- }
- }
- public static void EraseGameObjectsInAllLayers()
- {
- List<GameObject> allGameObjectsInAllLayers = ObjectLayerDatabase.Get().GetAllGameObjectsInAllLayers();
- ObjectErase.EraseGameObjectCollection(allGameObjectsInAllLayers);
- }
- public static void EraseAllGameObjectsInLayer(int objectLayer)
- {
- List<GameObject> allGameObjectsInLayer = ObjectLayerDatabase.Get().GetAllGameObjectsInLayer(objectLayer);
- ObjectErase.EraseGameObjectCollection(allGameObjectsInLayer);
- }
- #endregion
- }
- }
- #endif
|