TrafficEditorInspector.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Traffic Simulation
  2. // https://github.com/mchrbn/unity-traffic-simulation
  3. using System;
  4. using UnityEditor;
  5. using UnityEngine;
  6. namespace TrafficSimulation {
  7. public static class TrafficEditorInspector {
  8. //Whole Inspector layout
  9. public static void DrawInspector(TrafficSystem trafficSystem, SerializedObject serializedObject, out bool restructureSystem) {
  10. //-- Gizmo settings
  11. Header("Gizmo Config");
  12. Toggle("Hide Gizmos", ref trafficSystem.hideGuizmos);
  13. //Arrow config
  14. DrawArrowTypeSelection(trafficSystem);
  15. FloatField("Waypoint Size", ref trafficSystem.waypointSize);
  16. EditorGUILayout.Space();
  17. //-- System config
  18. Header("System Config");
  19. FloatField("Segment Detection Threshold", ref trafficSystem.segDetectThresh);
  20. PropertyField("Collision Layers", "collisionLayers", serializedObject);
  21. EditorGUILayout.Space();
  22. //Helper
  23. 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");
  24. 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.)");
  25. EditorGUILayout.Space();
  26. restructureSystem = Button("Re-Structure Traffic System");
  27. }
  28. //-- Helper to draw the Inspector
  29. private static void Label(string label) {
  30. EditorGUILayout.LabelField(label);
  31. }
  32. private static void Header(string label) {
  33. EditorGUILayout.LabelField(label, EditorStyles.boldLabel);
  34. }
  35. private static void Toggle(string label, ref bool toggle) {
  36. toggle = EditorGUILayout.Toggle(label, toggle);
  37. }
  38. private static void IntField(string label, ref int value) {
  39. value = EditorGUILayout.IntField(label, value);
  40. }
  41. private static void IntField(string label, ref int value, int min, int max) {
  42. value = Mathf.Clamp(EditorGUILayout.IntField(label, value), min, max);
  43. }
  44. private static void FloatField(string label, ref float value) {
  45. value = EditorGUILayout.FloatField(label, value);
  46. }
  47. private static void PropertyField(string label, string value, SerializedObject serializedObject){
  48. SerializedProperty extra = serializedObject.FindProperty(value);
  49. EditorGUILayout.PropertyField(extra, new GUIContent(label), true);
  50. }
  51. private static void HelpBox(string content) {
  52. EditorGUILayout.HelpBox(content, MessageType.Info);
  53. }
  54. private static bool Button(string label) {
  55. return GUILayout.Button(label);
  56. }
  57. private static void DrawArrowTypeSelection(TrafficSystem trafficSystem) {
  58. trafficSystem.arrowDrawType = (ArrowDraw) EditorGUILayout.EnumPopup("Arrow Draw Type", trafficSystem.arrowDrawType);
  59. EditorGUI.indentLevel++;
  60. switch (trafficSystem.arrowDrawType) {
  61. case ArrowDraw.FixedCount:
  62. IntField("Count", ref trafficSystem.arrowCount, 1, int.MaxValue);
  63. break;
  64. case ArrowDraw.ByLength:
  65. FloatField("Distance Between Arrows", ref trafficSystem.arrowDistance);
  66. break;
  67. case ArrowDraw.Off:
  68. break;
  69. default:
  70. throw new ArgumentOutOfRangeException();
  71. }
  72. if (trafficSystem.arrowDrawType != ArrowDraw.Off) {
  73. FloatField("Arrow Size Waypoint", ref trafficSystem.arrowSizeWaypoint);
  74. FloatField("Arrow Size Intersection", ref trafficSystem.arrowSizeIntersection);
  75. }
  76. EditorGUI.indentLevel--;
  77. }
  78. }
  79. }