/// <summary>
        /// Automatically activate tile system from user selection if appropriate so that
        /// tool can be selected.
        /// </summary>
        private void AutoActivateTileSystem()
        {
            ToolUtility.SelectActiveOrParentTileSystem();

            if (ToolUtility.ActiveTileSystem == null)
            {
                // Okay, no tile system is actually selected.
                // Select first non-locked if possible; otherwise select first if possible.
                var tileSystems     = ToolUtility.GetAllTileSystemsInScene();
                var firstTileSystem = tileSystems.FirstOrDefault(s => s != null && !s.Locked);
                if (firstTileSystem == null)
                {
                    firstTileSystem = tileSystems.FirstOrDefault(s => s != null);
                }

                if (firstTileSystem != null)
                {
                    ToolUtility.SelectTileSystem(firstTileSystem);
                }
                else
                {
                    return;
                }
            }

            // When using a tool it only makes sense to have one selected tile system.
            if (Selection.objects.Length > 1)
            {
                Selection.objects = new Object[] { ToolUtility.ActiveTileSystem.gameObject };
            }
        }
        /// <summary>
        /// Display user interface to replace tiles by brush in a tile system.
        /// </summary>
        /// <exclude/>
        public static void Command_ReplaceByBrush()
        {
            ReplaceByBrushWindow.ShowWindow((target, source, replacement) => {
                // Perform find and replace.
                int replaced = 0;
                switch (target)
                {
                default:
                case ReplaceByBrushTarget.ActiveTileSystem:
                    if (ToolUtility.ActiveTileSystem != null && ToolUtility.ActiveTileSystem.IsEditable && !ToolUtility.ActiveTileSystem.Locked)
                    {
                        replaced = ReplaceByBrush(ToolUtility.ActiveTileSystem, source, replacement);
                    }
                    break;

                case ReplaceByBrushTarget.SelectedTileSystems:
                    foreach (Object ob in Selection.GetFiltered(typeof(TileSystem), SelectionMode.ExcludePrefab))
                    {
                        TileSystem system = ob as TileSystem;
                        if (system.IsEditable && !system.Locked)
                        {
                            replaced += ReplaceByBrush(system, source, replacement);
                        }
                    }
                    break;

                case ReplaceByBrushTarget.All:
                    foreach (var tileSystem in ToolUtility.GetAllTileSystemsInScene())
                    {
                        if (tileSystem.IsEditable && !tileSystem.Locked)
                        {
                            replaced += ReplaceByBrush(tileSystem, source, replacement);
                        }
                    }
                    break;
                }

                string message = string.Format(
                    /* 0: quantity of tiles replaced */
                    TileLang.Text("Replace by Brush :: Matched and replaced {0} tile(s)."),
                    replaced
                    );
                Debug.Log(message + "\n");
            });
        }
        private void AutoAddPostfixToName()
        {
            var tileSystems = ToolUtility.GetAllTileSystemsInScene();

            string baseName  = Regex.Replace(this.currentPreset.SystemName, "#\\d+$", "");
            string nextName  = baseName;
            int    increment = 1;

            while (true)
            {
next:
                foreach (var tileSystem in tileSystems)
                {
                    if (tileSystem.name == nextName)
                    {
                        nextName = string.Format("{0} #{1}", baseName, ++increment);
                        goto next;
                    }
                }
                break;
            }

            this.currentPreset.SystemName = nextName;
        }