InTerra_TracksGUI.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using UnityEngine;
  2. using UnityEditor;
  3. namespace InTerra
  4. {
  5. [CustomEditor(typeof(InTerra_Track))]
  6. [CanEditMultipleObjects]
  7. public class InTerra_TracksGUI : Editor
  8. {
  9. SerializedProperty trackMaterial;
  10. SerializedProperty quadWidth;
  11. SerializedProperty quadLenght;
  12. SerializedProperty quadOffsetX;
  13. SerializedProperty quadOffsetZ;
  14. SerializedProperty stepSize;
  15. SerializedProperty lenghtUV;
  16. SerializedProperty groundedCheckDistance;
  17. SerializedProperty startCheckDistance;
  18. SerializedProperty time;
  19. SerializedProperty ereaseDistance;
  20. MaterialEditor matEditor;
  21. bool minmax;
  22. void OnEnable()
  23. {
  24. quadWidth = serializedObject.FindProperty("quadWidth");
  25. stepSize = serializedObject.FindProperty("stepSize");
  26. lenghtUV = serializedObject.FindProperty("lenghtUV");
  27. trackMaterial = serializedObject.FindProperty("trackMaterial");
  28. ereaseDistance = serializedObject.FindProperty("ereaseDistance");
  29. startCheckDistance = serializedObject.FindProperty("startCheckDistance");
  30. groundedCheckDistance = serializedObject.FindProperty("groundedCheckDistance");
  31. time = serializedObject.FindProperty("time");
  32. quadLenght = serializedObject.FindProperty("quadLenght");
  33. quadOffsetX = serializedObject.FindProperty("quadOffsetX");
  34. quadOffsetZ = serializedObject.FindProperty("quadOffsetZ");
  35. }
  36. public override void OnInspectorGUI()
  37. {
  38. var styleBold = new GUIStyle(EditorStyles.boldLabel);
  39. serializedObject.Update();
  40. var t = (target as InTerra_Track);
  41. using (new GUILayout.VerticalScope(EditorStyles.helpBox))
  42. {
  43. EditorGUILayout.PropertyField(trackMaterial);
  44. }
  45. Material mat = t.trackMaterial;
  46. if (mat == null)
  47. {
  48. GUI.enabled = false;
  49. }
  50. else
  51. {
  52. if (mat.shader && mat.shader.name != null && (mat.shader.name != "InTerra/Tracks Material"))
  53. {
  54. EditorGUILayout.HelpBox("Track Material need to have InTerra/Track Material shader!", MessageType.Warning);
  55. using (new GUILayout.VerticalScope(EditorStyles.helpBox))
  56. {
  57. if (GUILayout.Button("Assign Track Shader to Selected Material"))
  58. {
  59. mat.shader = Shader.Find("InTerra/Tracks Material");
  60. }
  61. }
  62. GUI.enabled = false;
  63. }
  64. else
  65. {
  66. if(matEditor == null)
  67. {
  68. matEditor = (MaterialEditor)CreateEditor(mat);
  69. }
  70. else
  71. {
  72. using (new GUILayout.VerticalScope(EditorStyles.helpBox))
  73. {
  74. if (trackMaterial.hasMultipleDifferentValues)
  75. {
  76. EditorGUILayout.HelpBox("Materials on selected objects are not the same, editing Material is not alloved.", MessageType.Info);
  77. GUI.enabled = false;
  78. }
  79. InTerra_GUI.TrackMaterialEditor(mat, matEditor, ref minmax);
  80. GUI.enabled = true;
  81. }
  82. }
  83. }
  84. }
  85. using (new GUILayout.VerticalScope(EditorStyles.helpBox))
  86. {
  87. EditorGUILayout.LabelField("Ground Check Ray Setting", styleBold);
  88. EditorGUILayout.Space();
  89. PropertyLine(groundedCheckDistance, "Checking Distance", "The length of checking ray, the ray can be seen drawn with red color if gizmos are enabled.");
  90. PropertyLine(startCheckDistance, "Ray Start Offset", " Offset of start position of checking ray.");
  91. PropertyLine(time, "Time Delay", "The ray has to be detecting ground for this amount of time (in seconds) for the object to be considered grounded.");
  92. }
  93. using (new GUILayout.VerticalScope(EditorStyles.helpBox))
  94. {
  95. EditorGUILayout.LabelField("Stamps Size and Position", styleBold);
  96. EditorGUILayout.Space();
  97. if (mat && mat.IsKeywordEnabled("_TRACKS"))
  98. {
  99. PropertyLine(lenghtUV, "UV Length", "Adjust UV tiling of X axis of Heightmap.");
  100. }
  101. PropertyLine(quadWidth, "Width");
  102. PropertyLine(quadLenght, "Length");
  103. PropertyLine(quadOffsetX, "Position Offset X");
  104. PropertyLine(quadOffsetZ, "Position Offset Z");
  105. }
  106. using (new GUILayout.VerticalScope(EditorStyles.helpBox))
  107. {
  108. PropertyLine(stepSize, "Distance Threshold", "The stamps for drawing track will be created only if the object reach this distance.");
  109. PropertyLine(ereaseDistance, "Erase at Distance", "Threshold distance after which the tracks stamps will be erased.");
  110. }
  111. serializedObject.ApplyModifiedProperties();
  112. void PropertyLine(SerializedProperty property, string label, string tooltip = null)
  113. {
  114. EditorGUILayout.PropertyField(property, new GUIContent() { text = label, tooltip = tooltip });
  115. }
  116. }
  117. private void OnSceneGUI()
  118. {
  119. if(!Application.isPlaying)
  120. {
  121. var t = (target as InTerra_Track);
  122. Debug.DrawLine(t.transform.position - new Vector3(0, -t.startCheckDistance, 0), t.transform.position - new Vector3(0, -t.startCheckDistance + t.groundedCheckDistance, 0), Color.red);
  123. Vector3 forwardVector = t.GetForwardVector();
  124. Debug.DrawLine(t.VertexDebugPositions()[0] - forwardVector * (t.quadLenght / 2), t.VertexDebugPositions()[1] - forwardVector * (t.quadLenght / 2), Color.blue);
  125. Debug.DrawLine(t.VertexDebugPositions()[0] + forwardVector * (t.quadLenght / 2), t.VertexDebugPositions()[1] + forwardVector * (t.quadLenght / 2), Color.blue);
  126. Debug.DrawLine(t.VertexDebugPositions()[0] - forwardVector * (t.quadLenght / 2), t.VertexDebugPositions()[0] + forwardVector * (t.quadLenght / 2), Color.blue);
  127. Debug.DrawLine(t.VertexDebugPositions()[1] - forwardVector * (t.quadLenght / 2), t.VertexDebugPositions()[1] + forwardVector * (t.quadLenght / 2), Color.blue);
  128. }
  129. }
  130. private void OnDisable()
  131. {
  132. GameObject.DestroyImmediate(matEditor);
  133. }
  134. }
  135. }