public void Rebuild(ChiselModel model)
        {
            if (!model.IsInitialized)
            {
                model.OnInitialize();
            }

            if (!ChiselModelGeneratedObjects.IsValid(model.generated))
            {
                if (model.generated != null)
                {
                    model.generated.Destroy();
                }
                model.generated = ChiselModelGeneratedObjects.Create(model);
            }

            UpdateModelFlags(model);
        }
        private void CreateContainerGameObject(ChiselModel model)
        {
            var modelScene     = model.gameObject.scene;
            var oldActiveScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene();

            if (modelScene != oldActiveScene)
            {
                UnityEngine.SceneManagement.SceneManager.SetActiveScene(modelScene);
            }

            try
            {
                CSGNodeHierarchyManager.ignoreNextChildrenChanged = true;
                var newGameObject = new GameObject(GeneratedContainerName);
                newGameObject.SetActive(false);
                try
                {
                    var transform = newGameObject.GetComponent <Transform>();
                    CSGNodeHierarchyManager.ignoreNextChildrenChanged = true;
                    transform.SetParent(model.transform, false);
                    CSGNodeHierarchyManager.ignoreNextChildrenChanged = false;
                    transform.localPosition      = Vector3.zero;
                    transform.localRotation      = Quaternion.identity;
                    transform.localScale         = Vector3.one;
                    model.GeneratedDataContainer = newGameObject;
                    model.GeneratedDataTransform = transform;
                }
                finally
                {
                    newGameObject.SetActive(true);
                }
                model.OnInitialize();
            }
            finally
            {
                if (modelScene != oldActiveScene)
                {
                    UnityEngine.SceneManagement.SceneManager.SetActiveScene(oldActiveScene);
                }
            }
        }
        static readonly ModelState __modelState = new ModelState(); // static to avoid allocations
        public void Rebuild(ChiselModel 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))
                    {
                        ChiselObjectUtility.SafeDestroy(child.gameObject);
                    }
                }
            }


            // to avoid dangling memory
            __modelState.modelGameObject     = null;
            __modelState.modelTransform      = null;
            __modelState.containerGameObject = null;
            __modelState.containerTransform  = null;
        }