vPoint GetPatrolPoint(vWaypoint waypoint) { var subPoints = pathArea.GetValidSubPoints(waypoint); if (waypoint.randomPatrolPoint) { currentPatrolPoint = randomPatrolPoint.Next(subPoints.Count); } else { currentPatrolPoint++; } if (currentPatrolPoint >= subPoints.Count) { currentPatrolPoint = 0; } if (subPoints.Count == 0) { agent.Stop(); return(null); } if (visitedPatrolPoint.Contains(subPoints[currentPatrolPoint])) { return(null); } agent.Resume(); return(subPoints[currentPatrolPoint]); }
void CreateNode(vWaypointArea wayArea, Vector3 position) { var nodeObj = new GameObject("node"); var node = nodeObj.AddComponent <vWaypoint>(); node.subPoints = new List <vPoint>(); nodeObj.transform.position = position; nodeObj.transform.parent = wayArea.transform; wayArea.waypoints.Add(node); currentNode = node; indexOfWaypoint = wayArea.waypoints.IndexOf(currentNode); }
void OnEnable() { pathArea = (vWaypointArea)target; if (pathArea.waypoints == null) { pathArea.waypoints = new List <vWaypoint>(); } if (pathArea.waypoints.Count > 0) { currentNode = pathArea.waypoints[0]; } EditorApplication.playmodeStateChanged = HandleOnPlayModeChanged; SetVisiblePoints(false); }
void OnSceneGUI() { if (!pathArea) { return; } CheckNodes(ref pathArea.waypoints); Event e = Event.current; if (editMode) { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); if (e.type == EventType.MouseDown && e.button == 0 && e.shift) { Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(worldRay, out hitInfo, Mathf.Infinity)) { CreateNode(pathArea, hitInfo.point); EditorUtility.SetDirty(pathArea); } } else if (e.type == EventType.MouseDown && e.button == 0 && e.control && currentNode) { Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(worldRay, out hitInfo, Mathf.Infinity)) { CreatePatrolPoint(pathArea, hitInfo.point); EditorUtility.SetDirty(pathArea); } } } else { HandleUtility.AddDefaultControl(GUIUtility.GetControlID(FocusType.Passive)); } if (editMode) { Handles.color = Color.white; } else { Handles.color = new Color(1, 1, 1, 0.2f); } if (pathArea.waypoints.Count > 0) { var node0 = pathArea.waypoints[0]; foreach (vWaypoint node in pathArea.waypoints) { if (node != null) { if (editMode && currentNode != null) { Handles.color = node.isValid ? (currentNode.Equals(node) ? Color.green : Color.white) : Color.red; } if (!editMode) { Handles.SphereCap(0, node.transform.position, Quaternion.identity, 0.25f); } else if (Handles.Button(node.transform.position, Quaternion.identity, currentNode ? (currentNode == node ? .5f : 0.25f) : .25f, currentNode ? (currentNode == node ? .5f : 0.25f) : .25f, Handles.SphereCap)) { indexOfWaypoint = pathArea.waypoints.IndexOf(node); currentNode = node; indexOfPatrolPoint = 0; Selection.activeGameObject = node.gameObject; Repaint(); } if (editMode) { Handles.color = new Color(0, 0, 1, .2f); } Handles.DrawLine(node0.transform.position, node.transform.position); node0 = node; var index = pathArea.waypoints.IndexOf(node) + 1; if (currentNode == null || !currentNode.Equals(node)) { Handles.Label(node.transform.position, new GUIContent("WP-" + index.ToString("00"))); if (node.subPoints != null && node.subPoints.Count > 0) { var patrolPoint0 = node.subPoints[0]; Handles.DrawLine(node.transform.position, patrolPoint0.position); foreach (vPoint patrolPoint in node.subPoints) { if (patrolPoint != null) { Handles.DrawLine(patrolPoint0.transform.position, patrolPoint.position); patrolPoint0 = patrolPoint; Handles.CubeCap(0, patrolPoint.transform.position, Quaternion.identity, 0.15f); } } } } } } } Handles.color = Color.white; if (currentNode && editMode) { Handles.color = Color.green; Handles.DrawWireDisc(currentNode.transform.position, Vector3.up, currentNode.areaRadius); e = Event.current; if (e.type == EventType.MouseDown && e.button == 1 && e.shift) { Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(worldRay, out hitInfo, Mathf.Infinity)) { Debug.Log(hitInfo.collider.gameObject.name, hitInfo.collider.gameObject); currentNode.transform.position = hitInfo.point; EditorUtility.SetDirty(pathArea); } } if (currentNode.subPoints == null) { currentNode.subPoints = new List <vPoint>(); } if (currentNode.subPoints.Count > 0) { var patrolPoint0 = currentNode.subPoints[0]; Handles.color = Color.cyan; Handles.DrawLine(currentNode.transform.position, patrolPoint0.position); foreach (vPoint patrolPoint in currentNode.subPoints) { Handles.color = Color.cyan; Handles.DrawLine(patrolPoint0.transform.position, patrolPoint.position); patrolPoint0 = patrolPoint; var index = currentNode.subPoints.IndexOf(patrolPoint); Handles.color = patrolPoint.isValid ? Color.cyan : Color.red; if (patrolPoint != null) { if (Handles.Button(patrolPoint.transform.position, Quaternion.Euler(0, 0, 0), .25f, .25f, Handles.CubeCap)) { indexOfPatrolPoint = currentNode.subPoints.IndexOf(patrolPoint); Selection.activeGameObject = patrolPoint.gameObject; Repaint(); } Handles.color = new Color(1, 1, 1, 0.1f); Handles.Label(patrolPoint.position, new GUIContent("P-" + (index + 1).ToString("00"))); } } Handles.color = Color.green; if (currentNode.subPoints.Count > 0 && indexOfPatrolPoint < currentNode.subPoints.Count) { EditorGUI.BeginChangeCheck(); Handles.DrawWireDisc(currentNode.subPoints[indexOfPatrolPoint].transform.position, Vector3.up, currentNode.subPoints[indexOfPatrolPoint].areaRadius); if (e.type == EventType.MouseDown && e.button == 1 && e.control && currentNode) { Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition); RaycastHit hitInfo; if (Physics.Raycast(worldRay, out hitInfo, Mathf.Infinity)) { currentNode.subPoints[indexOfPatrolPoint].position = hitInfo.point; EditorUtility.SetDirty(pathArea); } } } } } }
public override void OnInspectorGUI() { if (skin == null) { skin = Resources.Load("skin") as GUISkin; } GUI.skin = skin; //base.DrawDefaultInspector(); _wayArea = new SerializedObject(target); var waypoints = _wayArea.FindProperty("waypoints"); pathArea = (vWaypointArea)target; GUILayout.BeginVertical("Waypoint Area", "window", GUILayout.ExpandHeight(false), GUILayout.ExpandWidth(true)); GUILayout.Space(40); if (GUILayout.Button(editMode ? "Exit Edit Mode" : "Enter Edit Mode", GUILayout.ExpandWidth(true))) { editMode = !editMode; if (!editMode) { Selection.activeGameObject = pathArea.gameObject; } ActiveEditorTracker.sharedTracker.isLocked = editMode; SetVisiblePoints(editMode); Repaint(); } GUI.color = Color.white; GUI.enabled = editMode; EditorGUILayout.Space(); if (editMode && pathArea.waypoints.Count == 0) { EditorGUILayout.HelpBox("Starting by holding Shift and Left Click on any surface with a collider.", MessageType.Info); } if (pathArea.waypoints != null && pathArea.waypoints.Count > 0) { for (int i = 0; i < waypoints.arraySize; i++) { GUI.color = (i.Equals(indexOfWaypoint) ? Color.green : Color.white); GUILayout.BeginHorizontal(); if (GUILayout.Button("Waypoint " + (i + 1).ToString("00"), "box", GUILayout.ExpandWidth(true))) { indexOfWaypoint = i; currentNode = pathArea.waypoints[i]; Selection.activeGameObject = currentNode.gameObject; SceneView.lastActiveSceneView.FrameSelected(); } if (!PointIsInNavMesh(pathArea.waypoints[i].transform.position)) { GUI.color = Color.white; EditorGUILayout.HelpBox("Out of NavMesh", MessageType.Error); Repaint(); } EditorGUILayout.Space(); if (GUILayout.Button("x", GUILayout.MaxWidth(20))) { RemoveNode(ref waypoints, i); GUILayout.EndVertical(); break; } GUILayout.EndHorizontal(); EditorGUILayout.Space(); } } GUI.color = Color.white; if (indexOfWaypoint >= waypoints.arraySize) { indexOfWaypoint = waypoints.arraySize - 1; } try { if (waypoints != null && waypoints.arraySize > 0) { var vPoint = new SerializedObject(waypoints.GetArrayElementAtIndex(indexOfWaypoint).objectReferenceValue as vWaypoint); DrawWaypoint(vPoint, waypoints); } } catch { } GUILayout.EndVertical(); GUI.enabled = true; GUILayout.BeginVertical("Hot Keys", "window"); GUILayout.Space(40); hotKeys = GUILayout.Toggle(hotKeys, hotKeys ? "Hide Hot Keys" : "Show Hot Keys", "button", GUILayout.ExpandWidth(true)); if (hotKeys) { GUILayout.BeginVertical("box"); GUI.color = Color.green; GUILayout.Label("Shift + Left Mouse Click", "box"); GUI.color = Color.white; GUILayout.Label("Create New Way Point"); GUILayout.EndVertical(); EditorGUILayout.Separator(); GUILayout.BeginVertical("box"); GUI.color = Color.green; GUILayout.Label("Ctrl + Left Mouse Click", "box"); GUI.color = Color.white; GUILayout.Label("Create New Patrol Point to selected way point"); GUILayout.EndVertical(); EditorGUILayout.Separator(); GUILayout.BeginVertical("box"); GUI.color = Color.green; GUILayout.Label("Shift + Right Mouse Click", "box"); GUI.color = Color.white; GUILayout.Label("Set click point position to way point selected"); GUILayout.EndVertical(); EditorGUILayout.Separator(); GUILayout.BeginVertical("box"); GUI.color = Color.green; GUILayout.Label("Ctrl + Right Mouse Click", "box"); GUI.color = Color.white; GUILayout.Label("Set click point position to patrol point selected"); GUILayout.EndVertical(); EditorGUILayout.Separator(); } EditorGUILayout.Separator(); GUILayout.EndVertical(); if (Event.current.commandName == "UndoRedoPerformed") { Repaint(); } if (GUI.changed) { _wayArea.ApplyModifiedProperties(); EditorUtility.SetDirty(pathArea); } }
public List <vPoint> GetValidSubPoints(vWaypoint waipoint) { var _nodes = waipoint.subPoints.FindAll(node => node.isValid); return(_nodes); }