예제 #1
0
        void BuildPath(int clickedCellIndex)
        {
            DestroyLOSMarkers();

            if (isSelectingStart)
            {
                // Selects start cell
                cellStartIndex = clickedCellIndex;
                grid.CellToggle(cellStartIndex, true, Color.yellow);
            }
            else
            {
                // Clicked on the end cell, then show the path
                // First clear color of start cell
                grid.CellToggle(cellStartIndex, false, Color.white);
                // Get Path
                List <int> path = grid.FindPath(cellStartIndex, clickedCellIndex, cellGroupMask: CELLS_ALL_NAVIGATABLE);
                // Color the path
                if (path != null)
                {
                    for (int k = 0; k < path.Count; k++)
                    {
                        grid.CellFadeOut(path [k], Color.green, 1f);
                    }
                }
            }
            isSelectingStart = !isSelectingStart;
        }
예제 #2
0
        /// <summary>
        /// Creates a classic black and white checkers board
        /// </summary>
        void CreateBoard()
        {
            Texture2D whiteCell = Resources.Load <Texture2D> ("Textures/cellWhite");
            Texture2D blackCell = Resources.Load <Texture2D> ("Textures/cellBlack");

            grid.gridTopology = GRID_TOPOLOGY.Box;
            grid.rowCount     = 8;
            grid.columnCount  = 8;

            bool even = false;

            for (int row = 0; row < grid.rowCount; row++)
            {
                even = !even;
                for (int col = 0; col < grid.columnCount; col++)
                {
                    even = !even;
                    Cell cell      = grid.CellGetAtPosition(col, row);
                    int  cellIndex = grid.CellGetIndex(cell);
                    if (even)
                    {
                        grid.CellToggle(cellIndex, true, whiteCell);
                    }
                    else
                    {
                        grid.CellToggle(cellIndex, true, blackCell);
                    }
                }
            }
        }
예제 #3
0
        void Start()
        {
            // Get a reference to Grids 2D System's API
            grid = Grid2D.instance;

            Color[] noise     = tex.GetPixels();
            Color[] ramp      = colorRamp.GetPixels();
            int     width     = tex.width;
            int     height    = tex.height;
            int     cellCount = grid.cells.Count;

            for (int k = 0; k < cellCount; k++)
            {
                Vector2 center    = grid.cells[k].center;
                int     tw        = (int)((center.x + 0.5f) * width);
                int     th        = (int)((center.y + 0.5f) * height);
                float   elevation = noise[th * width + tw].r;
                if (elevation < waterLevel)
                {
                    // water
                    grid.cells[k].visible = false;
                }
                else
                {
                    int   pos   = (int)(colorRamp.width * (elevation - waterLevel) / (1f - waterLevel));
                    Color color = ramp[pos];
                    grid.CellToggle(k, true, color);
                }
            }
            grid.Redraw();
        }
예제 #4
0
        void DrawFruit(int cellIndex, Texture2D fruitTexture)
        {
            Vector2 textureOffset   = Vector2.zero;
            Vector2 textureScale    = new Vector2(1f, 0.7f);           // to keep some aspect ratio
            float   rotationDegrees = 0f;

            grid.CellToggle(cellIndex, true, Color.white, false, fruitTexture, textureScale, textureOffset, rotationDegrees, true);
        }
예제 #5
0
        void Start()
        {
            // setup GUI - only for the demo
            GUIResizer.Init(800, 500);
            labelStyle                  = new GUIStyle();
            labelStyle.alignment        = TextAnchor.MiddleLeft;
            labelStyle.normal.textColor = Color.white;

            // Get a reference to Grids 2D System's API
            grid = Grid2D.instance;
            for (int k = 0; k < grid.numCells; k++)
            {
                Texture2D fruitTexture = fruits [Random.Range(0, fruits.Length)];

                Vector2 textureOffset   = Vector2.zero;
                Vector2 textureScale    = new Vector2(1f, 0.6f);               // to keep some aspect ratio
                float   rotationDegrees = -90f;

                grid.CellToggle(k, true, Color.white, false, fruitTexture, textureScale, textureOffset, rotationDegrees, true);
            }
        }
예제 #6
0
 void toggleCellVisible(int cellIndex)
 {
     grid.CellToggle(cellIndex, true, Color.white, false, grid.canvasTexture);
 }
예제 #7
0
        public override void OnInspectorGUI()
        {
            EditorGUILayout.Separator();
            GUI.skin.label.alignment = TextAnchor.MiddleCenter;
            GUILayout.Label(_headerTexture, GUILayout.ExpandWidth(true));
            GUI.skin.label.alignment = TextAnchor.MiddleLeft;

            EditorGUILayout.BeginVertical(blackStyle);

            EditorGUILayout.BeginHorizontal();
            DrawTitleLabel("Grid Configuration");
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Help"))
            {
                EditorUtility.DisplayDialog("Grids 2D System", "Grids 2D is an advanced grid generator for Unity.\n\nFor a complete description of the options, please refer to the documentation guide (PDF) included in the asset.\nWe also invite you to visit and sign up on our support forum on kronnect.com where you can post your questions/requests.\n\nThanks for purchasing! Please rate Grids 2D on the Asset Store! Thanks.", "Close");
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Topology", GUILayout.Width(120));
            grid.gridTopology = (GRID_TOPOLOGY)EditorGUILayout.IntPopup((int)grid.gridTopology, topologyOptions, topologyOptionsValues);
            EditorGUILayout.EndHorizontal();

            if (grid.gridTopology == GRID_TOPOLOGY.Irregular)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Cells (aprox.)", GUILayout.Width(120));
                grid.numCells = EditorGUILayout.IntSlider(grid.numCells, 2, Grid2D.MAX_CELLS);
                EditorGUILayout.EndHorizontal();
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Columns", GUILayout.Width(120));
                grid.columnCount = EditorGUILayout.IntSlider(grid.columnCount, 2, Grid2D.MAX_CELLS_SQRT);
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Rows", GUILayout.Width(120));
                grid.rowCount = EditorGUILayout.IntSlider(grid.rowCount, 2, Grid2D.MAX_CELLS_SQRT);
                EditorGUILayout.EndHorizontal();
            }
            if (grid.gridTopology == GRID_TOPOLOGY.Hexagonal)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Regular Hexes", GUILayout.Width(120));
                grid.regularHexagons = EditorGUILayout.Toggle(grid.regularHexagons);
                EditorGUILayout.EndHorizontal();
                if (grid.regularHexagons)
                {
                    EditorGUILayout.BeginHorizontal();
                    GUILayout.Label("   Hex Size", GUILayout.Width(120));
                    grid.hexSize = EditorGUILayout.FloatField(grid.hexSize);
                    EditorGUILayout.EndHorizontal();
                }
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Even Layout", GUILayout.Width(120));
                grid.evenLayout = EditorGUILayout.Toggle(grid.evenLayout);
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Curvature", GUILayout.Width(120));
            if (grid.numCells > Grid2D.MAX_CELLS_FOR_CURVATURE)
            {
                DrawInfoLabel("not available with >" + Grid2D.MAX_CELLS_FOR_CURVATURE + " cells");
            }
            else
            {
                grid.gridCurvature = EditorGUILayout.Slider(grid.gridCurvature, 0, 0.1f);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Relaxation", GUILayout.Width(120));
            if (grid.gridTopology != GRID_TOPOLOGY.Irregular)
            {
                DrawInfoLabel("only available with irregular topology");
            }
            else if (grid.numCells > Grid2D.MAX_CELLS_FOR_RELAXATION)
            {
                DrawInfoLabel("not available with >" + Grid2D.MAX_CELLS_FOR_RELAXATION + " cells");
            }
            else
            {
                grid.gridRelaxation = EditorGUILayout.IntSlider(grid.gridRelaxation, 1, 32);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label(new GUIContent("Visibility Mask", "Alpha channel is used to determine cell visibility (0 = cell is not visible)"), GUILayout.Width(120));
            grid.gridMask = (Texture2D)EditorGUILayout.ObjectField(grid.gridMask, typeof(Texture2D), true);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Seed", GUILayout.Width(120));
            grid.seed = EditorGUILayout.IntSlider(grid.seed, 1, 10000);
            if (GUILayout.Button("Redraw"))
            {
                grid.Redraw();
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Territories", GUILayout.Width(120));
            grid.enableTerritories = EditorGUILayout.Toggle(grid.enableTerritories);
            EditorGUILayout.EndHorizontal();

            GUI.enabled = grid.enableTerritories;
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("   Count", GUILayout.Width(120));
            grid.numTerritories = EditorGUILayout.IntSlider(grid.numTerritories, 1, Mathf.Min(grid.numCells, Grid2D.MAX_TERRITORIES));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label(new GUIContent("   Region Texture", "Quickly create territories assigning a color texture in which each territory corresponds to a color."), GUILayout.Width(120));
            grid.territoriesTexture = (Texture2D)EditorGUILayout.ObjectField(grid.territoriesTexture, typeof(Texture2D), true);
            if (grid.territoriesTexture != null)
            {
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label(new GUIContent("  Neutral Color", "Color to be ignored."), GUILayout.Width(120));
                grid.territoriesTextureNeutralColor = EditorGUILayout.ColorField(grid.territoriesTextureNeutralColor, GUILayout.Width(50));
                EditorGUILayout.Space();
                if (GUILayout.Button("Generate Territories", GUILayout.Width(120)))
                {
                    if (grid.territoriesTexture == null)
                    {
                        EditorUtility.DisplayDialog("Missing territories texture!", "Assign a color texture to the territories texture slot.", "Ok");
                    }
                    else
                    {
                        grid.CreateTerritories(grid.territoriesTexture, grid.territoriesTextureNeutralColor);
                    }
                }
            }
            EditorGUILayout.EndHorizontal();

            GUI.enabled = true;

            int cellsCreated       = grid.cells == null ? 0 : grid.cells.Count;
            int territoriesCreated = grid.territories == null ? 0 : grid.territories.Count;

            EditorGUILayout.BeginHorizontal();
            GUILayout.FlexibleSpace();
            DrawInfoLabel("Cells Created: " + cellsCreated + " / Territories Created: " + territoriesCreated + " / Vertex Count: " + grid.lastVertexCount);
            GUILayout.FlexibleSpace();
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();
            EditorGUILayout.BeginVertical(blackStyle);

            DrawTitleLabel("Grid Positioning");

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Hide Objects", GUILayout.Width(120));
            if (GUILayout.Button("Toggle Grid"))
            {
                grid.gameObject.SetActive(!grid.gameObject.activeSelf);
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Depth Offset", GUILayout.Width(120));
            grid.gridDepthOffset = EditorGUILayout.IntSlider(grid.gridDepthOffset, -10, -1);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();
            EditorGUILayout.BeginVertical(blackStyle);

            DrawTitleLabel("Grid Appearance");

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Show Territories", GUILayout.Width(120));
            grid.showTerritories = EditorGUILayout.Toggle(grid.showTerritories);
            if (grid.showTerritories)
            {
                GUILayout.Label("Frontier Color", GUILayout.Width(120));
                grid.territoryFrontiersColor = EditorGUILayout.ColorField(grid.territoryFrontiersColor, GUILayout.Width(50));
            }
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("  Highlight Color", GUILayout.Width(120));
            grid.territoryHighlightColor = EditorGUILayout.ColorField(grid.territoryHighlightColor, GUILayout.Width(50));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("  Colorize Territories", GUILayout.Width(120));
            grid.colorizeTerritories = EditorGUILayout.Toggle(grid.colorizeTerritories);
            GUILayout.Label("Alpha");
            grid.colorizedTerritoriesAlpha = EditorGUILayout.Slider(grid.colorizedTerritoriesAlpha, 0.0f, 1.0f);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("  Outer Borders", GUILayout.Width(120));
            grid.showTerritoriesOuterBorders = EditorGUILayout.Toggle(grid.showTerritoriesOuterBorders);
            GUILayout.Label(new GUIContent("Internal Territories", "Allows territories to be contained by other territories."), GUILayout.Width(110));
            grid.allowTerritoriesInsideTerritories = EditorGUILayout.Toggle(grid.allowTerritoriesInsideTerritories);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Show Cells", GUILayout.Width(120));
            grid.showCells = EditorGUILayout.Toggle(grid.showCells);
            if (grid.showCells)
            {
                GUILayout.Label("Border Color", GUILayout.Width(120));
                grid.cellBorderColor = EditorGUILayout.ColorField(grid.cellBorderColor, GUILayout.Width(50));
            }
            EditorGUILayout.EndHorizontal();
            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("  Highlight Color", GUILayout.Width(120));
            grid.cellHighlightColor = EditorGUILayout.ColorField(grid.cellHighlightColor, GUILayout.Width(50));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Highlight Fade", GUILayout.Width(120));
            grid.highlightFadeAmount = EditorGUILayout.Slider(grid.highlightFadeAmount, 0.0f, 1.0f);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Canvas Texture", GUILayout.Width(120));
            grid.canvasTexture = (Texture2D)EditorGUILayout.ObjectField(grid.canvasTexture, typeof(Texture2D), true);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();
            EditorGUILayout.BeginVertical(blackStyle);

            DrawTitleLabel("Grid Behaviour");

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Selection Mode", GUILayout.Width(120));
            grid.highlightMode = (HIGHLIGHT_MODE)EditorGUILayout.Popup((int)grid.highlightMode, selectionModeOptions);
            EditorGUILayout.EndHorizontal();

            if (grid.highlightMode == HIGHLIGHT_MODE.Cells)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("  Include Invisible Cells", GUILayout.Width(120));
                grid.cellHighlightNonVisible = EditorGUILayout.Toggle(grid.cellHighlightNonVisible);
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Overlay Mode", GUILayout.Width(120));
            grid.overlayMode = (OVERLAY_MODE)EditorGUILayout.Popup((int)grid.overlayMode, overlayModeOptions);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Respect Other UI", GUILayout.Width(120));
            grid.respectOtherUI = EditorGUILayout.Toggle(grid.respectOtherUI);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();

            EditorGUILayout.BeginVertical(blackStyle);
            DrawTitleLabel("Path Finding");

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Algorithm", GUILayout.Width(120));
            grid.pathFindingHeuristicFormula = (Grids2D.PathFinding.HeuristicFormula)EditorGUILayout.EnumPopup(grid.pathFindingHeuristicFormula);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Max Search Cost", GUILayout.Width(120));
            grid.pathFindingMaxCost = EditorGUILayout.IntField(grid.pathFindingMaxCost, GUILayout.Width(100));
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Max Steps", GUILayout.Width(120));
            grid.pathFindingMaxSteps = EditorGUILayout.IntField(grid.pathFindingMaxSteps, GUILayout.Width(100));
            EditorGUILayout.EndHorizontal();

            if (grid.gridTopology == GRID_TOPOLOGY.Box)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Use Diagonals", GUILayout.Width(120));
                grid.pathFindingUseDiagonals = EditorGUILayout.Toggle(grid.pathFindingUseDiagonals, GUILayout.Width(40));
                GUILayout.Label("Heavy Diagonals", GUILayout.Width(120));
                grid.pathFindingHeavyDiagonals = EditorGUILayout.Toggle(grid.pathFindingHeavyDiagonals, GUILayout.Width(40));
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();

            EditorGUILayout.BeginVertical(blackStyle);

            EditorGUILayout.BeginHorizontal();
            DrawTitleLabel("Grid Editor");
            GUILayout.FlexibleSpace();
            if (GUILayout.Button("Export Settings"))
            {
                if (EditorUtility.DisplayDialog("Export Grid Settings", "This option will add a Grid Config component to this game object with current cell settings. You can restore this configuration just enabling this new component.", "Ok", "Cancel"))
                {
                    CreatePlaceholder();
                }
            }
            if (GUILayout.Button("Reset"))
            {
                if (EditorUtility.DisplayDialog("Reset Grid", "Reset cells to their default values?", "Ok", "Cancel"))
                {
                    ResetCells();
                    GUIUtility.ExitGUI();
                    return;
                }
            }
            EditorGUILayout.EndHorizontal();

            if (cellSelectedIndex < 0 || cellSelectedIndex >= grid.cells.Count)
            {
                GUILayout.Label("Click on a cell in Scene View to edit its properties.");
            }
            else
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("Selected Cell", GUILayout.Width(120));
                GUILayout.Label(cellSelectedIndex.ToString(), GUILayout.Width(120));
                EditorGUILayout.EndHorizontal();

                bool needsRedraw = false;
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("  Visible", GUILayout.Width(120));
                Cell selectedCell = grid.cells [cellSelectedIndex];
                bool cellVisible  = selectedCell.visible;
                selectedCell.visible = EditorGUILayout.Toggle(cellVisible);
                if (selectedCell.visible != cellVisible)
                {
                    needsRedraw = true;
                }
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("  Tag", GUILayout.Width(120));
                cellTag = EditorGUILayout.IntField(cellTag, GUILayout.Width(60));
                if (cellTag == selectedCell.tag)
                {
                    GUI.enabled = false;
                }
                if (GUILayout.Button("Set Tag", GUILayout.Width(60)))
                {
                    grid.CellSetTag(cellSelectedIndex, cellTag);
                }
                GUI.enabled = true;
                EditorGUILayout.EndHorizontal();
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("  Territory Index", GUILayout.Width(120));
                cellTerritoryIndex = EditorGUILayout.IntField(cellTerritoryIndex, GUILayout.Width(40));
                if (cellTerritoryIndex == selectedCell.territoryIndex)
                {
                    GUI.enabled = false;
                }
                if (GUILayout.Button("Set Territory", GUILayout.Width(100)) && cellTerritoryIndex != grid.cells [cellSelectedIndex].territoryIndex)
                {
                    grid.CellSetTerritory(cellSelectedIndex, cellTerritoryIndex);
                    needsRedraw = true;
                }
                GUI.enabled = true;
                EditorGUILayout.EndHorizontal();

                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("  Color", GUILayout.Width(120));
                cellColor = EditorGUILayout.ColorField(cellColor, GUILayout.Width(40));
                GUILayout.Label("  Texture", GUILayout.Width(60));
                cellTextureIndex = EditorGUILayout.IntField(cellTextureIndex, GUILayout.Width(40));
                if (grid.CellGetColor(cellSelectedIndex) == cellColor && grid.CellGetTextureIndex(cellSelectedIndex) == cellTextureIndex)
                {
                    GUI.enabled = false;
                }
                if (GUILayout.Button("Set", GUILayout.Width(40)))
                {
                    grid.CellToggle(cellSelectedIndex, true, cellColor, false, cellTextureIndex);
                    needsRedraw = true;
                }
                GUI.enabled = true;
                if (GUILayout.Button("Clear", GUILayout.Width(40)))
                {
                    grid.CellHide(cellSelectedIndex);
                    needsRedraw = true;
                }
                EditorGUILayout.EndHorizontal();

                if (needsRedraw)
                {
                    RefreshGrid();
                    GUIUtility.ExitGUI();
                    return;
                }
            }

            EditorGUILayout.BeginHorizontal();
            GUILayout.Label("Textures", GUILayout.Width(120));
            EditorGUILayout.EndHorizontal();

            if (toggleButtonStyleNormal == null)
            {
                toggleButtonStyleNormal  = "Button";
                toggleButtonStyleToggled = new GUIStyle(toggleButtonStyleNormal);
                toggleButtonStyleToggled.normal.background = toggleButtonStyleToggled.active.background;
            }

            int textureMax = grid.textures.Length - 1;

            while (textureMax >= 1 && grid.textures [textureMax] == null)
            {
                textureMax--;
            }
            textureMax++;
            if (textureMax >= grid.textures.Length)
            {
                textureMax = grid.textures.Length - 1;
            }

            for (int k = 1; k <= textureMax; k++)
            {
                EditorGUILayout.BeginHorizontal();
                GUILayout.Label("  " + k.ToString(), GUILayout.Width(40));
                grid.textures [k] = (Texture2D)EditorGUILayout.ObjectField(grid.textures [k], typeof(Texture2D), false);
                if (grid.textures [k] != null)
                {
                    if (GUILayout.Button(new GUIContent("T", "Texture mode - if enabled, you can paint several cells just clicking over them."), textureMode == k ? toggleButtonStyleToggled : toggleButtonStyleNormal, GUILayout.Width(20)))
                    {
                        textureMode = textureMode == k ? 0 : k;
                    }
                    if (GUILayout.Button(new GUIContent("X", "Remove texture"), GUILayout.Width(20)))
                    {
                        if (EditorUtility.DisplayDialog("Remove texture", "Are you sure you want to remove this texture?", "Yes", "No"))
                        {
                            grid.textures [k] = null;
                            GUIUtility.ExitGUI();
                            return;
                        }
                    }
                }
                EditorGUILayout.EndHorizontal();
            }

            EditorGUILayout.EndVertical();
            EditorGUILayout.Separator();
            if (grid.isDirty)
            {
                                                                                                                                #if UNITY_5_6_OR_NEWER
                serializedObject.UpdateIfRequiredOrScript();
                                                                                                                                #else
                serializedObject.UpdateIfDirtyOrScript();
                                                                                                                                #endif
                isDirty.boolValue = false;
                serializedObject.ApplyModifiedProperties();
                EditorUtility.SetDirty(target);

                // Hide mesh in Editor
                HideEditorMesh();

                SceneView.RepaintAll();
            }
        }
예제 #8
0
        // Use this for initialization
        void Start()
        {
            grid = Grid2D.instance;

            // setup GUI resizer - only for the demo
            GUIResizer.Init(800, 500);
            labelStyle                  = new GUIStyle();
            labelStyle.alignment        = TextAnchor.MiddleLeft;
            labelStyle.normal.textColor = Color.white;

            isSelectingStart = true;

            Random.InitState(2);

            // Draw some blocked areas
            for (int i = 0; i < 25; i++)
            {
                int row = Random.Range(2, grid.rowCount - 3);
                int col = Random.Range(2, grid.columnCount - 3);

                for (int j = -2; j <= 2; j++)
                {
                    for (int k = -2; k <= 2; k++)
                    {
                        int cellIndex = grid.CellGetIndex(row + j, col + k);
                        grid.CellSetGroup(cellIndex, CELL_OBSTACLE);
                        grid.CellToggle(cellIndex, true, Color.white);
                    }
                }
            }


            // Example: sets crossing costs for hexagon sides and draw a line
            int   barrierCost  = 100;
            Color barrierColor = Color.blue;
            float barrierWidth = 5f;

            for (int k = 0; k < 10; k++)
            {
                int cellIndex = grid.CellGetIndex(10, k + 20);
                if (!grid.cells [cellIndex].canCross)
                {
                    continue;
                }
                if (grid.cells [cellIndex].column % 2 == 0)
                {
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.Top, barrierCost);
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.TopLeft, barrierCost);
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.TopRight, barrierCost);
                    grid.DrawLine(cellIndex, CELL_SIDE.Top, barrierColor, barrierWidth);
                    grid.DrawLine(cellIndex, CELL_SIDE.TopLeft, barrierColor, barrierWidth);
                    grid.DrawLine(cellIndex, CELL_SIDE.TopRight, barrierColor, barrierWidth);
                }
                else
                {
                    grid.CellSetSideCrossCost(cellIndex, CELL_SIDE.Top, barrierCost);
                    grid.DrawLine(cellIndex, CELL_SIDE.Top, barrierColor, barrierWidth);
                }
            }



            // Hook into cell click event to toggle start selection or draw a computed path using A* path finding algorithm
            grid.OnCellClick += (cellIndex) => BuildPath(cellIndex);

            grid.OnCellEnter += (cellIndex) => ShowLineOfSight(cellIndex);
        }