예제 #1
0
        /// <summary>
        /// Selects tool for use within custom system.
        /// </summary>
        /// <param name="tool">Tool that is to be selected. Specify `null` to
        /// revert to previous Unity tool.</param>
        /// <returns>
        /// Instance of tool that was selected.
        /// </returns>
        public ToolBase SelectTool(ToolBase tool)
        {
            if (tool != null)
            {
                this.AutoActivateTileSystem();

                if (ToolUtility.ActiveTileSystem == null)
                {
                    // Deselect tool because no tile system is selected!
                    tool = null;
                }
                else if (this.currentTool == null)
                {
                    // Reveal active tile system in scene palette when tool is first activated.
                    // Let's not keep scrolling to the active tile system each time the user
                    // selects another tool since this can be quite annoying!
                    ToolUtility.RevealTileSystem(ToolUtility.ActiveTileSystem, false);

                    // Automatically show tool palette upon activating tool if specified
                    // in user preferences.
                    if (RtsPreferences.AutoShowToolPalette)
                    {
                        ToolUtility.ShowToolPalette(false);
                    }
                }
            }

            if (tool != null)
            {
                // Should current Unity tool be preserved whilst custom tool is used?
                if (UnityEditor.Tools.current != Tool.None)
                {
                    this.previousUnityTool    = UnityEditor.Tools.current;
                    UnityEditor.Tools.current = Tool.None;
                }
            }
            else if (UnityEditor.Tools.current == Tool.None)
            {
                // Revert back to former Unity tool because tool has been deselected!
                UnityEditor.Tools.current = this.previousUnityTool;
            }

            // Will tool selection actually change?
            if (tool == this.currentTool)
            {
                return(tool);
            }

            // Reset active plop reference to avoid issues when switching tools.
            ToolUtility.ActivePlop = null;

            if (this.currentTool != null)
            {
                this.previousTool = this.currentTool;
            }

            // Switch to specified tool.
            var oldTool = this.currentTool;

            this.currentTool = tool;

            if (oldTool != null)
            {
                oldTool.OnDisable();
            }

            if (tool != null)
            {
                if (oldTool == null)
                {
                    // No tool was active prior to using this method to select a tool.
                    this.BeginEditMode();
                }

                tool.OnEnable();
            }
            else if (oldTool != null)
            {
                // A tool was active prior to using this method to deselect tool.
                this.ExitEditMode();
            }

            // Raise event for tool change.
            if (this.ToolChanged != null)
            {
                try {
                    this.ToolChanged(oldTool, this.currentTool);
                }
                catch (Exception ex) {
                    Debug.LogException(ex);
                }
            }

            ToolUtility.RepaintToolPalette();

            // Brush palette only needs to be repainted when tool is first selected or deselected.
            if (oldTool == null || tool == null)
            {
                ToolUtility.RepaintBrushPalette();
            }

            SceneView.RepaintAll();

            return(tool);
        }