private static void DrawArrowTypeSelection(TrafficSystem trafficSystem) { trafficSystem.arrowDrawType = (ArrowDraw)EditorGUILayout.EnumPopup("Arrow Draw Type", trafficSystem.arrowDrawType); EditorGUI.indentLevel++; switch (trafficSystem.arrowDrawType) { case ArrowDraw.FixedCount: IntField("Count", ref trafficSystem.arrowCount, 1, int.MaxValue); break; case ArrowDraw.ByLength: FloatField("Distance Between Arrows", ref trafficSystem.arrowDistance); break; case ArrowDraw.Off: break; default: throw new ArgumentOutOfRangeException(); } if (trafficSystem.arrowDrawType != ArrowDraw.Off) { FloatField("Arrow Size Waypoint", ref trafficSystem.arrowSizeWaypoint); FloatField("Arrow Size Intersection", ref trafficSystem.arrowSizeIntersection); } EditorGUI.indentLevel--; }
private static void SetupVehicle() { EditorHelper.SetUndoGroup("Setup Vehicle"); GameObject selected = Selection.activeGameObject; //Create raycast anchor GameObject anchor = EditorHelper.CreateGameObject("Raycast Anchor", selected.transform); //Add AI scripts VehicleAI veAi = EditorHelper.AddComponent <VehicleAI>(selected); WheelDrive wheelDrive = EditorHelper.AddComponent <WheelDrive>(selected); TrafficSystem ts = GameObject.FindObjectOfType <TrafficSystem>(); //Configure the vehicle AI script with created objects anchor.transform.localPosition = Vector3.zero; anchor.transform.localRotation = Quaternion.Euler(Vector3.zero); veAi.raycastAnchor = anchor.transform; if (ts != null) { veAi.trafficSystem = ts; } //Create layer AutonomousVehicle if it doesn't exist EditorHelper.CreateLayer("AutonomousVehicle"); //Set the tag and layer name selected.tag = "AutonomousVehicle"; EditorHelper.SetLayer(selected, LayerMask.NameToLayer("AutonomousVehicle"), true); }
//Whole Inspector layout public static void DrawInspector(TrafficSystem trafficSystem, SerializedObject serializedObject, out bool restructureSystem) { //-- Gizmo settings Header("Gizmo Config"); Toggle("Hide Gizmos", ref trafficSystem.hideGuizmos); //Arrow config DrawArrowTypeSelection(trafficSystem); FloatField("Waypoint Size", ref trafficSystem.waypointSize); EditorGUILayout.Space(); //-- System config Header("System Config"); FloatField("Segment Detection Threshold", ref trafficSystem.segDetectThresh); PropertyField("Collision Layers", "collisionLayers", serializedObject); EditorGUILayout.Space(); //Helper HelpBox("Ctrl + Left Click to create a new segment\nShift + Left Click to create a new waypoint.\nAlt + Left Click to create a new intersection"); HelpBox("Reminder: The vehicles will follow the point depending on the sequence you added them. (go to the 1st waypoint added, then to the second, etc.)"); EditorGUILayout.Space(); restructureSystem = Button("Re-Structure Traffic System"); }
public static void BeginUndoGroup(string undoName, TrafficSystem trafficSystem) { //Create new Undo Group to collect all changes in one Undo Undo.SetCurrentGroupName(undoName); //Register all TrafficSystem changes after this (string not relevant here) Undo.RegisterFullObjectHierarchyUndo(trafficSystem.gameObject, undoName); }
public bool IsOnSegment(Vector3 p) { TrafficSystem ts = GameObject.FindObjectOfType <TrafficSystem>(); for (int i = 0; i < waypoints.Count - 1; i++) { float d1 = Vector3.Distance(waypoints[i].transform.position, p); float d2 = Vector3.Distance(waypoints[i + 1].transform.position, p); float d3 = Vector3.Distance(waypoints[i].transform.position, waypoints[i + 1].transform.position); float a = (d1 + d2) - d3; if (a < ts.segDetectThresh && a > -ts.segDetectThresh) { return(true); } } return(false); }
private static int GetArrowCount(Vector3 pointA, Vector3 pointB, TrafficSystem script) { switch (script.arrowDrawType) { case ArrowDraw.FixedCount: return(script.arrowCount); case ArrowDraw.ByLength: //Minimum of one arrow return(Mathf.Max(1, (int)(Vector3.Distance(pointA, pointB) / script.arrowDistance))); case ArrowDraw.Off: return(0); default: throw new ArgumentOutOfRangeException(); } }
void OnEnable() { wps = target as TrafficSystem; }
private static void DrawGizmo(TrafficSystem script, GizmoType gizmoType) { //Don't go further if we hide gizmos if (script.hideGuizmos) { return; } foreach (Segment segment in script.segments) { //Draw segment names GUIStyle style = new GUIStyle { normal = { textColor = new Color(1, 0, 0) }, fontSize = 15 }; Handles.Label(segment.transform.position, segment.name, style); //Draw waypoint for (int j = 0; j < segment.waypoints.Count; j++) { //Get current waypoint position Vector3 p = segment.waypoints[j].GetVisualPos(); //Draw sphere, increase color to show the direction Gizmos.color = new Color(0f, 0f, 1f, (j + 1) / (float)segment.waypoints.Count); Gizmos.DrawSphere(p, script.waypointSize); //Get next waypoint position Vector3 pNext = Vector3.zero; if (j < segment.waypoints.Count - 1 && segment.waypoints[j + 1] != null) { pNext = segment.waypoints[j + 1].GetVisualPos(); } if (pNext != Vector3.zero) { if (segment == script.curSegment) { Gizmos.color = new Color(1f, .3f, .1f); } else { Gizmos.color = new Color(1f, 0f, 0f); } //Draw connection line of the two waypoints Gizmos.DrawLine(p, pNext); //Set arrow count based on arrowDrawType int arrows = GetArrowCount(p, pNext, script); //Draw arrows for (int i = 1; i < arrows + 1; i++) { Vector3 point = Vector3.Lerp(p, pNext, (float)i / (arrows + 1)); DrawArrow(point, p - pNext, script.arrowSizeWaypoint); } } } //Draw line linking segments foreach (Segment nextSegment in segment.nextSegments) { if (nextSegment != null) { Vector3 p1 = segment.waypoints.Last().GetVisualPos(); Vector3 p2 = nextSegment.waypoints.First().GetVisualPos(); Gizmos.color = new Color(1f, 1f, 0f); Gizmos.DrawLine(p1, p2); if (script.arrowDrawType != ArrowDraw.Off) { DrawArrow((p1 + p2) / 2f, p1 - p2, script.arrowSizeIntersection); } } } } }