示例#1
0
    /// <summary>
    /// Displays connecting lines in the scene.
    /// </summary>
    private void DisplayLines()
    {
        AiWaypointNetwork network              = (AiWaypointNetwork)target;
        List <Vector3>    linePoints           = new List <Vector3>();
        Vector3?          nonNullStartPosition = null;

        // store non-null connections 0 -> N
        for (int i = 0; i < network.Waypoints.Count; i++)
        {
            if (network.Waypoints[i] != null)
            {
                linePoints.Add(network.Waypoints[i].position);
                // save reference to first non-null position so we can connect it to the end (close loop)
                if (nonNullStartPosition == null)
                {
                    nonNullStartPosition = network.Waypoints[i].position;
                }
            }
        }

        // close the loop by adding start to the end of the list
        // start is at beginning and end
        if (nonNullStartPosition != null)
        {
            linePoints.Add((Vector3)nonNullStartPosition);
        }

        // render our closed network to the scene view
        Handles.color = Color.cyan;
        Handles.DrawPolyLine(linePoints.ToArray());
    }
    /// <summary>
    /// Displays connecting lines in the scene.
    /// </summary>
    private void displayLines()
    {
        AiWaypointNetwork network              = (AiWaypointNetwork)target;
        List <Vector3>    linePoints           = new List <Vector3>();
        Vector3?          nonNullStartPosition = null;

        // create the labels on the scene and populate the linePoints
        for (int i = 0; i < network.Waypoints.Count; i++)
        {
            if (network.Waypoints[i] != null)
            {
                Handles.color = Color.white;
                GUIStyle style = new GUIStyle();
                style.normal.textColor = Color.white;
                Handles.Label(network.Waypoints[i].position, "P" + i, style);
                linePoints.Add(network.Waypoints[i].position);
                if (nonNullStartPosition == null)
                {
                    nonNullStartPosition = network.Waypoints[i].position;
                }
            }
        }

        // close the linePoints loop to the first non-null value
        if (nonNullStartPosition != null)
        {
            linePoints.Add((Vector3)nonNullStartPosition);
        }

        // connect all the non-null waypoints with lines
        Handles.color = Color.cyan;
        Handles.DrawPolyLine(linePoints.ToArray());
    }
示例#3
0
    /// <summary>
    /// Draw the waypoint labels
    /// </summary>
    private void DrawWaypointLabels()
    {
        AiWaypointNetwork network = (AiWaypointNetwork)target;

        for (int index = 0; index < network.Waypoints.Count; index++)
        {
            if (network.Waypoints[index] != null)
            {
                GUIStyle style = new GUIStyle();
                style.normal.textColor = Color.white;
                Handles.Label(network.Waypoints[index].position, "P" + index, style);
                Handles.Label(network.Waypoints[index].position, "\n" + network.Waypoints[index].name, style);
            }
        }
    }
示例#4
0
    /// <summary>
    /// Callback for what to render in the inspector.
    /// </summary>
    public override void OnInspectorGUI()
    {
        AiWaypointNetwork network = (AiWaypointNetwork)target;

        network.DisplayMode = (WaypointDisplayMode)EditorGUILayout.EnumPopup("Display Mode", network.DisplayMode);

        if (network.DisplayMode == WaypointDisplayMode.Paths)
        {
            network.PathStartIndex = EditorGUILayout.IntSlider("Start Index", network.PathStartIndex, 0, network.Waypoints.Count - 1);
            network.PathEndIndex   = EditorGUILayout.IntSlider("End Index", network.PathEndIndex, 0, network.Waypoints.Count - 1);
        }

        // display the default behavior for all non hidden serialized fields
        DrawDefaultInspector();
    }
示例#5
0
    /// <summary>
    /// Displays nav mesh paths in the scene.
    /// </summary>
    private void DisplayPaths()
    {
        AiWaypointNetwork network      = (AiWaypointNetwork)target;
        WaypointEngine    engine       = new WaypointEngine(network);
        Waypoint          fromWaypoint = engine.GetWaypoint(network.PathStartIndex);
        Waypoint          toWaypoint   = engine.GetWaypoint(network.PathEndIndex);

        if (fromWaypoint != null && fromWaypoint.Transform != null && toWaypoint != null && toWaypoint.Transform != null)
        {
            NavMeshPath path = new NavMeshPath();
            NavMesh.CalculatePath(fromWaypoint.Transform.position, toWaypoint.Transform.position, NavMesh.AllAreas, path);
            Handles.color = Color.yellow;
            Handles.DrawPolyLine(path.corners);
        }
    }
    /// <summary>
    /// Displays nav mesh paths in the scene.
    /// </summary>
    private void displayPaths()
    {
        AiWaypointNetwork network = (AiWaypointNetwork)target;

        Transform fromTransform = network.GetStartWaypoint();
        Transform toTransform   = network.GetEndWaypoint();

        if (fromTransform != null && toTransform != null)
        {
            NavMeshPath path = new NavMeshPath();
            NavMesh.CalculatePath(fromTransform.position, toTransform.position, NavMesh.AllAreas, path);
            Handles.color = Color.yellow;
            Handles.DrawPolyLine(path.corners);
        }
    }
    /// <summary>
    /// Callback for what to render in the scene.
    /// </summary>
    private void OnSceneGUI()
    {
        AiWaypointNetwork network = (AiWaypointNetwork)target;

        if (network.DisplayMode == WaypointDisplayMode.Lines)
        {
            displayLines();
        }
        else
        {
            displayPaths();
        }

        Debug.Log(network.GetFirstNonNullWaypoint(-2));
    }
示例#8
0
    /// <summary>
    /// Initialize.
    /// </summary>
    /// <param name="waypointNetwork">A waypoint network with 0 or more connected waypoints.</param>
    public WaypointEngine(AiWaypointNetwork waypointNetwork)
    {
        // did you drag your waypoint network to the AI Entity?
        Assert.IsNotNull(waypointNetwork, "Invalid waypointNetwork; it cannot be null!  Check your inspector!");

        // Does your network any waypoints assigned to it?
        Assert.IsNotNull(waypointNetwork.Waypoints, "Invalid network.Waypoints; it cannot be null!!");
        if (waypointNetwork.Waypoints.Count == 0)
        {
            Debug.LogWarning("The given waypoint network exists, but no waypoints have been added to it!");
        }
        this.waypointNetwork = waypointNetwork;

        // build the contiguos network and pre-compute all the next waypoint destinations
        BuildWaypoints();
    }
示例#9
0
    /// <summary>
    /// Callback for what to render in the scene.
    /// </summary>
    private void OnSceneGUI()
    {
        AiWaypointNetwork network = (AiWaypointNetwork)target;

        DrawWaypointLabels();

        switch (network.DisplayMode)
        {
        case WaypointDisplayMode.Lines:
            DisplayLines();
            break;

        case WaypointDisplayMode.Paths:
            DisplayPaths();
            break;

        default:
            break;
        }
    }