Beispiel #1
0
        // TODO: put somewhere else
        public static bool ConvertToBrushes(ChiselGeneratorComponent chiselNode)
        {
            chiselNode.OnValidate();
            if (!chiselNode.TopTreeNode.Valid)
            {
                return(false);
            }

            var topGameObject      = chiselNode.gameObject;
            var gameObjectIsActive = topGameObject.activeSelf;
            var surfaceDefinition  = chiselNode.SurfaceDefinition;
            var nodeTypeName       = chiselNode.ChiselNodeTypeName;

            // Destroying this Generator Component will destroy the treeNode
            // So we need to copy the treeNode
            var topNode = chiselNode.TopTreeNode;

            // Set the treeNode to default in the component
            // (so when we destroy it the original treeNode doesn't get destroyed)
            chiselNode.ResetTreeNodes(doNotDestroy: true);
            // Destroy the component
            UnityEditor.Undo.DestroyObjectImmediate(chiselNode);
            // ... and then destroy the treeNode ourselves after we're done with it

            var topNodeType = topNode.Type;

            bool result = false;

            try
            {
                // We set the gameobject to not be active, this will prevent a lot of messages to
                // be send by Unity while we're still building the entire sub-hierarchy
                topGameObject.SetActive(false);
                var topComponent = ConvertTreeNodeToBrushes(topGameObject, in surfaceDefinition, topNode, chiselNode.PivotOffset);
                result = topComponent != null;
            }
            finally
            {
                // Activate the gameobject again (if it was active in the first place)
                topGameObject.SetActive(gameObjectIsActive);

                // Destroy the treeNode that was part of the original Generator Component
                if (topNode.Valid)
                {
                    topNode.Destroy();
                }
            }
            UnityEditor.Undo.SetCurrentGroupName((topNodeType == CSGNodeType.Brush) ? $"Converted {nodeTypeName} to brush" : $"Converted {nodeTypeName} to multiple brushes");
            return(result);
        }
Beispiel #2
0
        public void Commit(ChiselGeneratorComponent generatorComponent, bool createAsBrush)
        {
            var newGameObject = generatorComponent.gameObject;

            if (!newGameObject)
            {
                Cancel();
                return;
            }

            if (createAsBrush)
            {
                ConvertToBrushesButton.ConvertToBrushes(generatorComponent);
                newGameObject.name = ChiselBrushComponent.kNodeTypeName;
            }

            IsGenerating = false;
            UnityEditor.Selection.selectionChanged -= OnDelayedSelectionChanged;
            UnityEditor.Selection.selectionChanged += OnDelayedSelectionChanged;
            UnityEditor.Selection.activeGameObject  = newGameObject;
            Undo.IncrementCurrentGroup();
            Reset();
        }