public void OnDrawGizmos(Pathfinding.Util.RetainedGizmos gizmos)
        {
            var hasher = new Pathfinding.Util.RetainedGizmos.Hasher(AstarPath.active);

            hasher.AddHash(gizmoVersion);

            if (!gizmos.Draw(hasher))
            {
                var builder = ObjectPool <RetainedGizmos.Builder> .Claim();

                var centers = ArrayPool <UnityEngine.Vector3> .Claim(areas.Length);

                for (int i = 0; i < areas.Length; i++)
                {
                    Int3 center = Int3.zero;
                    var  childs = children[i];
                    if (childs.Count > 0)
                    {
                        for (int j = 0; j < childs.Count; j++)
                        {
                            center += childs[j].position;
                        }
                        center    /= childs.Count;
                        centers[i] = (UnityEngine.Vector3)center;
                    }
                }

                for (int i = 0; i < areas.Length; i++)
                {
                    if (children[i].Count > 0)
                    {
                        for (int j = 0; j < connections[i].Count; j++)
                        {
                            if (connections[i][j] > i)
                            {
                                builder.DrawLine(centers[i], centers[connections[i][j]], UnityEngine.Color.black);
                            }
                        }
                    }
                }

                builder.Submit(gizmos, hasher);
            }
        }
Exemple #2
0
        public static void OnDrawGizmos(this NavmeshBase navmeshBase, Pathfinding.Util.RetainedGizmos gizmos, bool drawNodes)
        {
            if (!drawNodes)
            {
                return;
            }

            using (var helper = gizmos.GetSingleFrameGizmoHelper()) {
                var bounds = new Bounds();
                bounds.SetMinMax(Vector3.zero, navmeshBase.forcedBoundsSize);
                // Draw a write cube using the latest transform
                // (this makes the bounds update immediately if some field is changed in the editor)
                helper.builder.DrawWireCube(navmeshBase.CalculateTransform(), bounds, Color.white);
            }

            if (navmeshBase.tiles != null)
            {
                // Update navmesh vizualizations for
                // the tiles that have been changed
                for (int i = 0; i < navmeshBase.tiles.Length; i++)
                {
                    // This may happen if an exception has been thrown when the graph was scanned.
                    // We don't want the gizmo code to start to throw exceptions as well then as
                    // that would obscure the actual source of the error.
                    if (navmeshBase.tiles[i] == null)
                    {
                        continue;
                    }

                    // Calculate a hash of the tile
                    var hasher = new RetainedGizmos.Hasher(AstarPath.active);
                    hasher.AddHash(navmeshBase.showMeshOutline ? 1 : 0);
                    hasher.AddHash(navmeshBase.showMeshSurface ? 1 : 0);
                    hasher.AddHash(navmeshBase.showNodeConnections ? 1 : 0);

                    var nodes = navmeshBase.tiles[i].nodes;
                    for (int j = 0; j < nodes.Length; j++)
                    {
                        hasher.HashNode(nodes[j]);
                    }

                    if (!gizmos.Draw(hasher))
                    {
                        using (var helper = gizmos.GetGizmoHelper(hasher)) {
                            if (navmeshBase.showMeshSurface || navmeshBase.showMeshOutline)
                            {
                                navmeshBase.CreateNavmeshSurfaceVisualization(navmeshBase.tiles[i], helper);
                            }
                            if (navmeshBase.showMeshSurface || navmeshBase.showMeshOutline)
                            {
                                CreateNavmeshOutlineVisualization(navmeshBase.tiles[i], helper);
                            }

                            if (navmeshBase.showNodeConnections)
                            {
                                for (int j = 0; j < nodes.Length; j++)
                                {
                                    helper.DrawConnections(nodes[j]);
                                }
                            }
                        }
                    }

                    gizmos.Draw(hasher);
                }
            }

            if (AstarPath.active.showUnwalkableNodes)
            {
                navmeshBase.DrawUnwalkableNodes(AstarPath.active.unwalkableNodeDebugSize);
            }
        }