public static GameObject ReplaceGameObjectHierarchyPrefab(GameObject gameObject, GameObject newPrefab) { if (gameObject == null || PrefabUtility.GetPrefabType(newPrefab) != PrefabType.Prefab) { 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.SetSelectedHierarchyWireframeHidden(ObjectPlacementSettings.Get().HideWireframeWhenPlacingObjects); 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); }
private static ObjectGroup CreateNewObjectGroupWithUniqueName(string name, List <string> existingGroupNames) { GameObject groupParent = new GameObject(); UndoEx.RegisterCreatedGameObject(groupParent); ObjectGroup newObjectGroup = Octave3DWorldBuilder.ActiveInstance.CreateScriptableObject <ObjectGroup>(); newObjectGroup.Name = UniqueEntityNameGenerator.GenerateUniqueName(name, existingGroupNames); newObjectGroup.GroupObject = groupParent; return(newObjectGroup); }
public static GameObject InstantiateObjectHierarchyFromPrefab(GameObject prefab, Vector3 worldPosition, Quaternion worldRotation, Vector3 worldScale) { GameObject instantiatedObject = PrefabUtility.InstantiatePrefab(prefab) as GameObject; UndoEx.RegisterCreatedGameObject(instantiatedObject); instantiatedObject.name = prefab.name; Transform objectTransform = instantiatedObject.transform; objectTransform.position = worldPosition; objectTransform.rotation = worldRotation; objectTransform.localScale = worldScale; instantiatedObject.SetSelectedHierarchyWireframeHidden(ObjectPlacementSettings.Get().HideWireframeWhenPlacingObjects); SceneViewCamera.Instance.SetObjectVisibilityDirty(); return(instantiatedObject); }
private void Combine(List <MeshCombineMaterial> meshCombineMaterials, GameObject combinedMeshesParent) { List <GameObject> allCombinedMeshObjects = new List <GameObject>(100); if (combinedMeshesParent == null) { combinedMeshesParent = new GameObject(GetNameForCombinedMeshesParent()); UndoEx.RegisterCreatedGameObject(combinedMeshesParent); } for (int combMaterialIndex = 0; combMaterialIndex < meshCombineMaterials.Count; ++combMaterialIndex) { MeshCombineMaterial meshCombMaterial = meshCombineMaterials[combMaterialIndex]; List <GameObject> combineMeshdObjects = Combine(meshCombMaterial, combinedMeshesParent); if (_combineSettings.WeldVertexPositions) { if (_combineSettings.WeldVertexPositionsOnlyForCommonMaterial) { WeldVertexPositionsForMeshObjects(combineMeshdObjects); } else { allCombinedMeshObjects.AddRange(combineMeshdObjects); } } } if (_combineSettings.WeldVertexPositions && !_combineSettings.WeldVertexPositionsOnlyForCommonMaterial) { WeldVertexPositionsForMeshObjects(allCombinedMeshObjects); } if (_combineSettings.CollHandling == MeshCombineSettings.ColliderHandling.Preserve) { PreserveColliders(meshCombineMaterials, combinedMeshesParent); } }
private void PreserveColliders(List <MeshCombineMaterial> meshCombineMaterials, GameObject combinedMeshesParent) { if (meshCombineMaterials.Count == 0 || _combineSettings.CollHandling != MeshCombineSettings.ColliderHandling.Preserve) { return; } GameObject colliderParent = combinedMeshesParent; Transform colliderParentTransform = colliderParent.transform; if (_combineSettings.CollPreserveParent == MeshCombineSettings.ColliderPreserveParent.OneForAll) { colliderParent = new GameObject(_combineSettings.OneForAllColliderParentName); UndoEx.RegisterCreatedGameObject(colliderParent); colliderParentTransform = colliderParent.transform; colliderParentTransform.parent = combinedMeshesParent.transform; } HashSet <GameObject> processedObjects = new HashSet <GameObject>(); Vector3 positionSum = Vector3.zero; for (int cmbMaterialIndex = 0; cmbMaterialIndex < meshCombineMaterials.Count; ++cmbMaterialIndex) { MeshCombineMaterial meshCmbMaterial = meshCombineMaterials[cmbMaterialIndex]; EditorUtility.DisplayProgressBar("Collider Handling", "Preserving colliders...", (float)cmbMaterialIndex / meshCombineMaterials.Count); List <MeshInstance> meshInstances = meshCmbMaterial.MeshInstances; foreach (var meshInstance in meshInstances) { GameObject meshObject = meshInstance.MeshTransform.gameObject; if (processedObjects.Contains(meshObject)) { continue; } processedObjects.Add(meshObject); positionSum += meshObject.transform.position; BoxCollider[] boxColliders = meshObject.GetComponents <BoxCollider>(); foreach (var boxCollider in boxColliders) { string colliderName = _combineSettings.CollPreserveName == MeshCombineSettings.ColliderPreserveName.SameAsSource ? meshObject.name : GetDefaultBoxColliderObjectName(); GameObject colliderObject = boxCollider.CloneAsNewObject(colliderName).gameObject; UndoEx.RegisterCreatedGameObject(colliderObject); colliderObject.transform.parent = colliderParentTransform; } SphereCollider[] sphereColliders = meshObject.GetComponents <SphereCollider>(); foreach (var sphereCollider in sphereColliders) { string colliderName = _combineSettings.CollPreserveName == MeshCombineSettings.ColliderPreserveName.SameAsSource ? meshObject.name : GetDefaultSphereColliderObjectName(); GameObject colliderObject = sphereCollider.CloneAsNewObject(colliderName).gameObject; UndoEx.RegisterCreatedGameObject(colliderObject); colliderObject.transform.parent = colliderParentTransform; } CapsuleCollider[] capsuleColliders = meshObject.GetComponents <CapsuleCollider>(); foreach (var capsuleCollider in capsuleColliders) { string colliderName = _combineSettings.CollPreserveName == MeshCombineSettings.ColliderPreserveName.SameAsSource ? meshObject.name : GetDefaultCapsuleColliderObjectName(); GameObject colliderObject = capsuleCollider.CloneAsNewObject(colliderName).gameObject; UndoEx.RegisterCreatedGameObject(colliderObject); colliderObject.transform.parent = colliderParentTransform; } MeshCollider[] meshColliders = meshObject.GetComponents <MeshCollider>(); foreach (var meshCollider in meshColliders) { string colliderName = _combineSettings.CollPreserveName == MeshCombineSettings.ColliderPreserveName.SameAsSource ? meshObject.name : GetDefaultMeshColliderObjectName(); GameObject colliderObject = meshCollider.CloneAsNewObject(colliderName).gameObject; UndoEx.RegisterCreatedGameObject(colliderObject); colliderObject.transform.parent = colliderParentTransform; } } } if (_combineSettings.CollPreserveParent == MeshCombineSettings.ColliderPreserveParent.OneForAll) { colliderParent.SetWorldPosDontAffectChildren(positionSum *= (1.0f / processedObjects.Count)); } if (colliderParent.transform.childCount == 0) { GameObject.DestroyImmediate(colliderParent); } EditorUtility.ClearProgressBar(); }
private GameObject CreateCombinedMeshObject(CombinedMeshData combinedMeshData, Material meshMaterial, GameObject combinedMeshesParent) { Mesh combinedMesh = CreateCombinedMesh(combinedMeshData); GameObject combinedMeshObject = new GameObject(_combineSettings.CombinedObjectName); UndoEx.RegisterCreatedGameObject(combinedMeshObject); combinedMeshObject.transform.parent = combinedMeshesParent.transform; combinedMeshObject.isStatic = _combineSettings.MakeCombinedMeshesStatic ? true : false; MeshFilter meshFilter = combinedMeshObject.AddComponent <MeshFilter>(); meshFilter.sharedMesh = combinedMesh; MeshRenderer meshRenderer = combinedMeshObject.AddComponent <MeshRenderer>(); meshRenderer.sharedMaterial = meshMaterial; if (_combineSettings.CollHandling == MeshCombineSettings.ColliderHandling.CreateNew) { if (_combineSettings.NewCollidersType == MeshCombineSettings.ColliderType.Mesh) { MeshCollider meshCollider = combinedMeshObject.AddComponent <MeshCollider>(); if (_combineSettings.UseConvexMeshColliders) { meshCollider.convex = true; } else { meshCollider.convex = false; } if (_combineSettings.NewCollidersAreTriggers) { meshCollider.isTrigger = true; } else { meshCollider.isTrigger = false; } } else if (_combineSettings.NewCollidersType == MeshCombineSettings.ColliderType.Box) { BoxCollider boxCollider = combinedMeshObject.AddComponent <BoxCollider>(); if (_combineSettings.NewCollidersAreTriggers) { boxCollider.isTrigger = true; } else { boxCollider.isTrigger = false; } } } Vector3 meshPivotPt = MeshCombineSettings.MeshPivotToWorldPoint(combinedMeshObject, _combineSettings.CombinedMeshesPivot); combinedMeshObject.SetMeshPivotPoint(meshPivotPt); return(combinedMeshObject); }