Exemple #1
0
 private PathingAgent getAgent()
 {
     if (navAgent == null)
     {
         navAgent = GetComponent <PathingAgent>();
     }
     return(navAgent);
 }
Exemple #2
0
 static void StopShowingAgentPath()
 {
     lastSelectedPathingAgent = null;
 }
    public void OnEnable()
    {
        agent = (PathingAgent)target;
        PathingAgent.lastSelectedPathingAgent = null;

        // Cache common variables
        SerializedProperty connectedWaypointsProperty = serializedObject.FindProperty("connectedWaypoints");
        SerializedProperty waypointsProperty          = serializedObject.FindProperty("waypoints");

        // Make reordable waypoints list
        waypointsList = new ReorderableList(serializedObject, waypointsProperty);
        waypointsList.drawHeaderCallback = (Rect rect) =>
        {
            EditorGUI.LabelField(rect, "Waypoints");
        };
        // Expand relevant paired lists when adding a new waypoint
        waypointsList.onAddCallback = (ReorderableList list) =>
        {
            showWaypointSubInfo.Add(false);
            showRandomWaypointConns.Add(false);
            connectedWaypointsProperty.arraySize += 1;

            waypointsProperty.arraySize += 1;
            waypointsList.index          = waypointsProperty.arraySize - 1;
        };
        // Shrink relevant paired lists when deleting a waypoint
        waypointsList.onRemoveCallback = (ReorderableList list) =>
        {
            Object waypoint = waypointsProperty.GetArrayElementAtIndex(list.index).objectReferenceValue;
            // If there is a waypoint, set it to null
            if (waypoint != null)
            {
                waypointsProperty.DeleteArrayElementAtIndex(list.index);
            }
            // If it's null, delete the item and its place in the relevant paired lists
            else
            {
                waypointsProperty.DeleteArrayElementAtIndex(list.index);
                connectedWaypointsProperty.DeleteArrayElementAtIndex(list.index);
                showWaypointSubInfo.RemoveAt(list.index);
                showRandomWaypointConns.RemoveAt(list.index);
            }
        };
        // Handle reordering relevant paired lists when reordering waypoints
        waypointsList.onReorderCallbackWithDetails = (ReorderableList list, int oldIndex, int newIndex) =>
        {
            connectedWaypointsProperty.MoveArrayElement(oldIndex, newIndex);

            bool temp = showWaypointSubInfo[oldIndex];
            showWaypointSubInfo[oldIndex] = showWaypointSubInfo[newIndex];
            showWaypointSubInfo[newIndex] = temp;

            temp = showRandomWaypointConns[oldIndex];
            showRandomWaypointConns[oldIndex] = showRandomWaypointConns[newIndex];
            showRandomWaypointConns[newIndex] = temp;
        };
        // Visualize waypoints list
        waypointsList.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
        {
            float singleItemHeight = EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;
            float cumulativeHeight = 0;

            var element = waypointsProperty.GetArrayElementAtIndex(index);
            EditorGUI.PropertyField(new Rect(rect.x, rect.y, rect.width, EditorGUIUtility.singleLineHeight), element, new GUIContent("Waypoint " + index.ToString()));
            cumulativeHeight += singleItemHeight;

            // Make non-reordable connectedWaypoints list per waypoint
            SerializedProperty connectionsProperty = connectedWaypointsProperty.GetArrayElementAtIndex(index).FindPropertyRelative("connections");
            string             listKey             = connectionsProperty.propertyPath;
            ReorderableList    connectedWaypointsList;
            if (connectedWaypointsListDict.ContainsKey(listKey))
            {
                connectedWaypointsList = connectedWaypointsListDict[listKey];
            }
            else
            {
                connectedWaypointsList = new ReorderableList(connectionsProperty.serializedObject, connectionsProperty, false, true, true, true);
                connectedWaypointsList.drawHeaderCallback = (Rect innerRect) =>
                {
                    EditorGUI.LabelField(innerRect, "Connected Waypoints");
                };
                connectedWaypointsList.drawElementCallback = (Rect innerRect, int innerIndex, bool innerIsActive, bool innerIsFocused) =>
                {
                    var innerElement = connectionsProperty.GetArrayElementAtIndex(innerIndex);
                    EditorGUI.PropertyField(new Rect(innerRect.x, innerRect.y, innerRect.width, connectionsProperty.arraySize * EditorGUIUtility.singleLineHeight), innerElement, GUIContent.none, true);
                };
                connectedWaypointsListDict[listKey] = connectedWaypointsList;
            }

            showWaypointSubInfo[index] = EditorGUI.Foldout(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight, rect.width, EditorGUIUtility.singleLineHeight), showWaypointSubInfo[index], "Per Waypoint Settings");
            if (showWaypointSubInfo[index])
            {
                cumulativeHeight += 2 * singleItemHeight;
                showRandomWaypointConns[index] = GUI.Toggle(new Rect(rect.x, rect.y + EditorGUIUtility.singleLineHeight * 2, rect.width, EditorGUIUtility.singleLineHeight), showRandomWaypointConns[index], "Draw Random Waypoint Connections");

                connectedWaypointsList.DoList(new Rect(rect.x, rect.y + singleItemHeight * 3, rect.width, EditorGUIUtility.singleLineHeight));
                cumulativeHeight += EditorGUI.GetPropertyHeight(connectionsProperty);
                cumulativeHeight += (connectionsProperty.arraySize + 1) * singleItemHeight;
                cumulativeHeight += waypointsList.headerHeight + waypointsList.footerHeight;

                // If a waypoint is assigned, show the onEnter/onExit events
                if (element.objectReferenceValue)
                {
                    SerializedObject waypointSerializedObject = new SerializedObject(element.objectReferenceValue);
                    waypointSerializedObject.Update();

                    SerializedProperty waypointEnterProperty = waypointSerializedObject.FindProperty("onWaypointEnter");
                    EditorGUI.PropertyField(new Rect(rect.x, rect.y + cumulativeHeight, rect.width, EditorGUIUtility.singleLineHeight),
                                            waypointEnterProperty,
                                            new GUIContent("On Waypoint Enter"));
                    cumulativeHeight += EditorGUI.GetPropertyHeight(waypointEnterProperty);
                    EditorGUI.PropertyField(new Rect(rect.x, rect.y + cumulativeHeight, rect.width, EditorGUIUtility.singleLineHeight),
                                            waypointSerializedObject.FindProperty("onWaypointExit"),
                                            new GUIContent("On Waypoint Exit"));

                    waypointSerializedObject.ApplyModifiedProperties();
                }
            }
        };
        waypointsList.elementHeightCallback = (int index) =>
        {
            var   waypoint       = waypointsProperty.GetArrayElementAtIndex(index);
            float waypointHeight = EditorGUI.GetPropertyHeight(waypoint);
            // Spacing for the foldout
            waypointHeight += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

            if (showWaypointSubInfo[index])
            {
                // Spacing for the random connection drawing toggle
                waypointHeight += EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing;

                SerializedProperty connectionsProperty = connectedWaypointsProperty.GetArrayElementAtIndex(index).FindPropertyRelative("connections");
                waypointHeight += EditorGUI.GetPropertyHeight(connectionsProperty);
                waypointHeight += (connectionsProperty.arraySize + 1) * (EditorGUIUtility.singleLineHeight + EditorGUIUtility.standardVerticalSpacing);
                waypointHeight += waypointsList.footerHeight + waypointsList.headerHeight;

                if (waypoint.objectReferenceValue)
                {
                    SerializedObject waypointSerializedObject = new SerializedObject(waypoint.objectReferenceValue);
                    waypointHeight += EditorGUI.GetPropertyHeight(waypointSerializedObject.FindProperty("onWaypointEnter"));
                    waypointHeight += EditorGUI.GetPropertyHeight(waypointSerializedObject.FindProperty("onWaypointExit"));
                }
            }

            return(waypointHeight);
        };

        while (showWaypointSubInfo.Count < waypointsProperty.arraySize)
        {
            showWaypointSubInfo.Add(false);
        }
        while (showRandomWaypointConns.Count < waypointsProperty.arraySize)
        {
            showRandomWaypointConns.Add(false);
        }
    }