示例#1
0
    public GameObject AddNode(NavNode2D source, NavNodeType type)
    {
        Undo.SetCurrentGroupName("Create Nav2D Node All");
        int undoGroup = Undo.GetCurrentGroup();

        GameObject newObject  = new GameObject();
        NavNode2D  newNavNode = newObject.AddComponent <NavNode2D>();

        newNavNode.nav2d       = source.nav2d;
        newNavNode.navNodeType = type;
        newNavNode.connections.Add(source);

        if (source.nav2d)
        {
            int count = source.nav2d.nodes.Count;
            newNavNode.name = $"Node ({count}) ({type.ToString()})";
        }

        newObject.transform.position = source.transform.position + Vector3.right;
        newObject.transform.parent   = source.transform.parent;
        source.connections.Add(newNavNode);

        Undo.RegisterCreatedObjectUndo(newObject, "Create Nav2D Node Object");
        Undo.CollapseUndoOperations(undoGroup);

        return(newObject);
    }
示例#2
0
    public void RemoveNode(NavNode2D source)
    {
        Undo.SetCurrentGroupName("Remove Nav2D Node");
        int group = Undo.GetCurrentGroup();

        Undo.DestroyObjectImmediate(source.gameObject);
        Undo.CollapseUndoOperations(group);
    }
    private Stack <NavNodeData> AStar(NavNode2D startNode2D, NavNode2D goalNode2D)
    {
        int maxIterations = 2000;

        NavNodeData start = startNode2D.GetNavData();
        NavNodeData goal  = goalNode2D.GetNavData();

        Stack <NavNodeData> path        = new Stack <NavNodeData>();
        List <NavNodeData>  openNodes   = new List <NavNodeData>();
        List <NavNodeData>  closedNodes = new List <NavNodeData>();
        NavNodeData         current     = start;

        openNodes.Add(current);
        while (maxIterations > 0 && openNodes.Count > 0 && (closedNodes.Contains(goal) == false))
        {
            maxIterations--;
            current = openNodes[0];
            openNodes.Remove(current);
            closedNodes.Add(current);

            List <NavNodeData> neighbors = current.source.connections.ConvertAll((NavNode2D source) => source.GetNavData());
            foreach (NavNodeData neighbor in neighbors)
            {
                if (closedNodes.Contains(neighbor) == false)
                {
                    if (openNodes.Contains(neighbor) == false)
                    {
                        neighbor.parent           = current;
                        neighbor.distanceToTarget = Vector2.Distance(neighbor.position, current.position);
                        neighbor.cost             = neighbor.weight + neighbor.parent.cost;
                        openNodes.Add(neighbor);
                        openNodes = openNodes.OrderBy(node => node.F).ToList <NavNodeData>();
                    }
                }
            }
        }

        if (closedNodes.Contains(goal) == false)
        {
            return(null);
        }

        NavNodeData temp = current;

        if (closedNodes.Contains(current) == false)
        {
            return(null);
        }

        do
        {
            path.Push(temp);
            temp = temp.parent;
        } while (temp != start && temp != null);

        return(path);
    }
    public Stack <NavNodeData> GetPathToTarget(Vector3 target)
    {
        lastWorldTarget = target;
        NavNode2D targetNode = nav2d.FindNearestNavNode(target);

        lastSnapTarget = targetNode.position;
        NavNode2D startNode = nav2d.FindNearestNavNode(transform.position);

        return(AStar(startNode, targetNode));
    }
示例#5
0
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();
        NavNode2D component = (NavNode2D)target;

        if (GUILayout.Button("+ Normal"))
        {
            GameObject addedObject = AddNode(component, NavNodeType.NORMAL);
            Selection.SetActiveObjectWithContext(addedObject, addedObject);
        }

        if (GUILayout.Button("+ Stairs"))
        {
            GameObject addedObject = AddNode(component, NavNodeType.STAIRS);
            Selection.SetActiveObjectWithContext(addedObject, addedObject);
        }

        if (GUILayout.Button("- Remove Node"))
        {
            RemoveNode(component);
        }
    }