internal void DestroyAllRegisteredGeneratedComponentsInModel(CSGModel model) { GameObject modelGameObject = null; if (model) { modelGameObject = model.gameObject; } __uniqueGameObjects.Clear(); foreach (var components in model.generatedRenderComponents.Values) { foreach (var component in components) { if (component == null) { continue; } if (component.gameObject) { var gameObject = component.gameObject; if (gameObject != modelGameObject) { __uniqueGameObjects.Add(gameObject); } } } } foreach (var components in model.generatedMeshColliders.Values) { foreach (var component in components) { if (component == null) { continue; } if (component.gameObject) { var gameObject = component.gameObject; if (gameObject != modelGameObject) { __uniqueGameObjects.Add(gameObject); } } } } foreach (var gameObject in __uniqueGameObjects) { CSGObjectUtility.SafeDestroy(gameObject); } __uniqueGameObjects.Clear(); model.generatedRenderComponents.Clear(); model.generatedMeshColliders.Clear(); model.generatedComponents.Clear(); }
private void RemoveContainerGameObject(CSGModel model) { if (model.GeneratedDataTransform) { model.GeneratedDataContainer.hideFlags = HideFlags.None; model.GeneratedDataTransform.hideFlags = HideFlags.None; CSGNodeHierarchyManager.ignoreNextChildrenChanged = true; CSGObjectUtility.SafeDestroy(model.GeneratedDataContainer); CSGNodeHierarchyManager.ignoreNextChildrenChanged = false; model.GeneratedDataContainer = null; model.GeneratedDataTransform = null; } }
// Destroy all Unity Meshes that aren't used and haven't been recycled public void DestroyNonRecycledUnusedUnityMeshes() { // Remove our garbage for (int i = 0; i < garbage.Count; i++) { refCountedMeshes.Remove(garbage[i]); } garbage.Clear(); // Make sure we destroy the leftover meshes for (int i = 0; i < garbageMeshes.Count; i++) { CSGObjectUtility.SafeDestroy(garbageMeshes[i]); } garbageMeshes.Clear(); }
public static CSGBrushMeshAsset CreateBoxAsset(UnityEngine.Vector3 min, UnityEngine.Vector3 max, CSGSurfaceAsset[] surfaceAssets, SurfaceFlags surfaceFlags = SurfaceFlags.None) { if (min.x == max.x || min.y == max.y || min.z == max.z) { return(null); } if (surfaceAssets.Length != 6) { return(null); } var brushMeshAsset = UnityEngine.ScriptableObject.CreateInstance <CSGBrushMeshAsset>(); brushMeshAsset.name = "Box"; if (!GenerateBoxAsset(brushMeshAsset, min, max, surfaceAssets, surfaceFlags)) { CSGObjectUtility.SafeDestroy(brushMeshAsset); } return(brushMeshAsset); }
static readonly ModelState __modelState = new ModelState(); // static to avoid allocations public void Rebuild(CSGModel model) { if (model.generatedMeshes == null || model.generatedMeshes.Length == 0) { // Destroy leftover components in model lookups DestroyAllRegisteredGeneratedComponentsInModel(model); RemoveContainerGameObject(model); } else { if (!model.IsInitialized) { model.OnInitialize(); } if (!model.GeneratedDataTransform) { DestroyAllRegisteredGeneratedComponentsInModel(model); CreateContainerGameObject(model); } var modelGameObject = model.gameObject; #if UNITY_EDITOR __modelState.staticFlags = UnityEditor.GameObjectUtility.GetStaticEditorFlags(modelGameObject); #endif __modelState.modelGameObject = modelGameObject; __modelState.modelTransform = model.hierarchyItem.Transform; __modelState.layer = modelGameObject.layer; __modelState.containerGameObject = model.GeneratedDataContainer; __modelState.containerTransform = model.GeneratedDataTransform; __modelState.existingRenderComponents = model.generatedRenderComponents; __modelState.existingMeshColliders = model.generatedMeshColliders; UpdateModelFlags(model); UpdateContainerFlags(model, __modelState); // Build components for generatedMesh, re-use existing components if they're available (& remove from lookups) updateMeshRenderers.Clear(); updateMeshColliders.Clear(); for (int i = 0; i < model.generatedMeshes.Length; i++) { var generatedMesh = model.generatedMeshes[i]; BuildComponents(model, __modelState, generatedMesh); } UpdateComponents(model, updateMeshRenderers, updateMeshColliders); updateMeshRenderers.Clear(); updateMeshColliders.Clear(); // Destroy leftover components in model lookups DestroyAllRegisteredGeneratedComponentsInModel(model); // Rebuild component lookup tables used by generatedMeshes BuildGeneratedComponentLookupTables(model); var containerTransform = __modelState.containerTransform; for (int c = containerTransform.childCount - 1; c >= 0; c--) { var child = containerTransform.GetChild(c); if (!model.generatedComponents.Contains(child)) { CSGObjectUtility.SafeDestroy(child.gameObject); } } } // to avoid dangling memory __modelState.modelGameObject = null; __modelState.modelTransform = null; __modelState.containerGameObject = null; __modelState.containerTransform = null; }