MegaTrainFollowEditor.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections.Generic;
  4. [CustomEditor(typeof(MegaTrainFollow))]
  5. public class MegaTrainFollowEditor : Editor
  6. {
  7. public override void OnInspectorGUI()
  8. {
  9. MegaTrainFollow mod = (MegaTrainFollow)target;
  10. #if !UNITY_5 && !UNITY_2017 && !UNITY_2018 && !UNITY_2019 && !UNITY_2020
  11. EditorGUIUtility.LookLikeControls();
  12. #endif
  13. mod.path = (MegaShape)EditorGUILayout.ObjectField("Path", mod.path, typeof(MegaShape), true);
  14. if ( mod.path && mod.path.splines != null )
  15. {
  16. if ( mod.path.splines.Count > 1 )
  17. mod.curve = EditorGUILayout.IntSlider("Curve", mod.curve, 0, mod.path.splines.Count - 1);
  18. if ( mod.curve < 0 ) mod.curve = 0;
  19. if ( mod.curve > mod.path.splines.Count - 1 )
  20. mod.curve = mod.path.splines.Count - 1;
  21. }
  22. mod.distance = EditorGUILayout.FloatField("Distance", mod.distance);
  23. mod.speed = EditorGUILayout.FloatField("Speed", mod.speed);
  24. mod.showrays = EditorGUILayout.Toggle("Show Rays", mod.showrays);
  25. if ( mod.carriages.Count < 1 )
  26. {
  27. if ( GUILayout.Button("Add") )
  28. {
  29. MegaCarriage car = new MegaCarriage();
  30. mod.carriages.Add(car);
  31. }
  32. }
  33. for ( int i = 0; i < mod.carriages.Count; i++ )
  34. {
  35. MegaCarriage car = mod.carriages[i];
  36. EditorGUILayout.BeginVertical("Box");
  37. car.length = EditorGUILayout.FloatField("Length", car.length);
  38. car.bogeyoff = EditorGUILayout.FloatField("Bogey Off", car.bogeyoff);
  39. car.carriage = (GameObject)EditorGUILayout.ObjectField("Carriage", car.carriage, typeof(GameObject), true);
  40. car.carriageOffset = EditorGUILayout.Vector3Field("Carriage Off", car.carriageOffset);
  41. car.rot = EditorGUILayout.Vector3Field("Carriage Rot", car.rot);
  42. car.bogey1 = (GameObject)EditorGUILayout.ObjectField("Front Bogey", car.bogey1, typeof(GameObject), true);
  43. car.bogey1Offset = EditorGUILayout.Vector3Field("Front Bogey Off", car.bogey1Offset);
  44. car.bogey1Rot = EditorGUILayout.Vector3Field("Front Bogey Rot", car.bogey1Rot);
  45. car.bogey2 = (GameObject)EditorGUILayout.ObjectField("Rear Bogey", car.bogey2, typeof(GameObject), true);
  46. car.bogey2Offset = EditorGUILayout.Vector3Field("Rear Bogey Off", car.bogey2Offset);
  47. car.bogey2Rot = EditorGUILayout.Vector3Field("Rear Bogey Rot", car.bogey2Rot);
  48. EditorGUILayout.EndVertical();
  49. EditorGUILayout.BeginHorizontal();
  50. if ( GUILayout.Button("Add") )
  51. {
  52. MegaCarriage nc = new MegaCarriage();
  53. mod.carriages.Add(nc);
  54. }
  55. if ( GUILayout.Button("Delete") )
  56. mod.carriages.Remove(car);
  57. EditorGUILayout.EndHorizontal();
  58. }
  59. if ( GUI.changed ) //rebuild )
  60. {
  61. EditorUtility.SetDirty(target);
  62. }
  63. }
  64. #if UNITY_5_1 || UNITY_5_2 || UNITY_5_3 || UNITY_5_4 || UNITY_5_5 || UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  65. [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.Pickable | GizmoType.InSelectionHierarchy)]
  66. #else
  67. [DrawGizmo(GizmoType.NotSelected | GizmoType.Pickable | GizmoType.SelectedOrChild)]
  68. #endif
  69. static void RenderGizmo(MegaTrainFollow mod, GizmoType gizmoType)
  70. {
  71. if ( (gizmoType & GizmoType.Active) != 0 && Selection.activeObject == mod.gameObject )
  72. {
  73. if ( !mod.showrays )
  74. return;
  75. for ( int i = 0; i < mod.carriages.Count; i++ )
  76. {
  77. MegaCarriage car = mod.carriages[i];
  78. Handles.color = Color.white;
  79. Handles.DrawLine(car.b1, car.b2);
  80. //Gizmos.DrawSphere(car.b1, car.length * 0.025f);
  81. //Gizmos.DrawSphere(car.b2, car.length * 0.025f);
  82. #if UNITY_5_6 || UNITY_2017 || UNITY_2018 || UNITY_2019 || UNITY_2020
  83. Handles.SphereHandleCap(0, car.cp, Quaternion.identity, car.length * 0.025f, EventType.Layout);
  84. Handles.SphereHandleCap(0, car.b1, Quaternion.identity, car.length * 0.025f, EventType.Layout);
  85. Handles.SphereHandleCap(0, car.b2, Quaternion.identity, car.length * 0.025f, EventType.Layout);
  86. //if ( showrays )
  87. Handles.color = Color.red;
  88. Handles.DrawLine(car.cp, car.bp1);
  89. Handles.SphereHandleCap(0, car.bp1, Quaternion.identity, car.length * 0.025f, EventType.Layout);
  90. //Gizmos.color = Color.green;
  91. Handles.color = Color.green;
  92. //Gizmos.DrawLine(car.cp, car.bp2);
  93. Handles.DrawLine(car.cp, car.bp2);
  94. //Gizmos.DrawSphere(car.bp2, car.length * 0.025f);
  95. Handles.SphereHandleCap(0, car.bp2, Quaternion.identity, car.length * 0.025f, EventType.Layout);
  96. #else
  97. Handles.SphereCap(0, car.cp, Quaternion.identity, car.length * 0.025f);
  98. Handles.SphereCap(0, car.b1, Quaternion.identity, car.length * 0.025f);
  99. Handles.SphereCap(0, car.b2, Quaternion.identity, car.length * 0.025f);
  100. //if ( showrays )
  101. Handles.color = Color.red;
  102. Handles.DrawLine(car.cp, car.bp1);
  103. Handles.SphereCap(0, car.bp1, Quaternion.identity, car.length * 0.025f);
  104. //Gizmos.color = Color.green;
  105. Handles.color = Color.green;
  106. //Gizmos.DrawLine(car.cp, car.bp2);
  107. Handles.DrawLine(car.cp, car.bp2);
  108. //Gizmos.DrawSphere(car.bp2, car.length * 0.025f);
  109. Handles.SphereCap(0, car.bp2, Quaternion.identity, car.length * 0.025f);
  110. #endif
  111. }
  112. }
  113. }
  114. }