Beispiel #1
0
        private void CreateContainerGameObject(CSGModel 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 gameObject = new GameObject(GeneratedContainerName);
                var transform  = gameObject.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 = gameObject;
                model.GeneratedDataTransform = transform;
                model.OnInitialize();
            }
            finally
            {
                if (modelScene != oldActiveScene)
                {
                    UnityEngine.SceneManagement.SceneManager.SetActiveScene(oldActiveScene);
                }
            }
        }
Beispiel #2
0
        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;
        }