AIBaseEditor.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace Pathfinding {
  4. [CustomEditor(typeof(AIBase), true)]
  5. [CanEditMultipleObjects]
  6. public class BaseAIEditor : EditorBase {
  7. protected SerializedProperty gravity, groundMask, centerOffset, rotationIn2D, acceleration;
  8. float lastSeenCustomGravity = float.NegativeInfinity;
  9. protected override void OnEnable () {
  10. base.OnEnable();
  11. gravity = serializedObject.FindProperty("gravity");
  12. groundMask = serializedObject.FindProperty("groundMask");
  13. centerOffset = serializedObject.FindProperty("centerOffset");
  14. rotationIn2D = serializedObject.FindProperty("rotationIn2D");
  15. acceleration = serializedObject.FindProperty("maxAcceleration");
  16. }
  17. protected override void Inspector () {
  18. // Iterate over all properties of the script
  19. var p = serializedObject.GetIterator();
  20. p.Next(true);
  21. while (p.NextVisible(false)) {
  22. if (!SerializedProperty.EqualContents(p, groundMask) && !SerializedProperty.EqualContents(p, centerOffset) && !SerializedProperty.EqualContents(p, gravity) && !SerializedProperty.EqualContents(p, rotationIn2D)) {
  23. if (SerializedProperty.EqualContents(p, acceleration) && typeof(AIPath).IsAssignableFrom(target.GetType())) {
  24. EditorGUI.BeginChangeCheck();
  25. int grav = acceleration.hasMultipleDifferentValues ? -1 : (acceleration.floatValue >= 0 ? 1 : 0);
  26. var ngrav = EditorGUILayout.Popup("Max Acceleration", grav, new [] { "Default", "Custom" });
  27. if (EditorGUI.EndChangeCheck()) {
  28. if (ngrav == 0) acceleration.floatValue = -2.5f;
  29. else if (acceleration.floatValue < 0) acceleration.floatValue = 10;
  30. }
  31. if (!acceleration.hasMultipleDifferentValues && ngrav == 1) {
  32. EditorGUI.indentLevel++;
  33. PropertyField(acceleration.propertyPath);
  34. EditorGUI.indentLevel--;
  35. acceleration.floatValue = Mathf.Max(acceleration.floatValue, 0.01f);
  36. }
  37. } else {
  38. PropertyField(p);
  39. }
  40. }
  41. }
  42. PropertyField(rotationIn2D);
  43. var mono = target as MonoBehaviour;
  44. var rigid = mono.GetComponent<Rigidbody>();
  45. var rigid2D = mono.GetComponent<Rigidbody2D>();
  46. var controller = mono.GetComponent<CharacterController>();
  47. var canUseGravity = (controller != null && controller.enabled) || ((rigid == null || rigid.isKinematic) && (rigid2D == null || rigid2D.isKinematic));
  48. if (canUseGravity) {
  49. EditorGUI.BeginChangeCheck();
  50. int grav = gravity.hasMultipleDifferentValues ? -1 : (gravity.vector3Value == Vector3.zero ? 0 : (float.IsNaN(gravity.vector3Value.x) ? 1 : 2));
  51. var ngrav = EditorGUILayout.Popup("Gravity", grav, new [] { "None", "Use Project Settings", "Custom" });
  52. if (EditorGUI.EndChangeCheck()) {
  53. if (ngrav == 0) gravity.vector3Value = Vector3.zero;
  54. else if (ngrav == 1) gravity.vector3Value = new Vector3(float.NaN, float.NaN, float.NaN);
  55. else if (float.IsNaN(gravity.vector3Value.x) || gravity.vector3Value == Vector3.zero) gravity.vector3Value = Physics.gravity;
  56. lastSeenCustomGravity = float.NegativeInfinity;
  57. }
  58. if (!gravity.hasMultipleDifferentValues) {
  59. // A sort of delayed Vector3 field (to prevent the field from dissappearing if you happen to enter zeroes into x, y and z for a short time)
  60. // Note: cannot use != in this case because that will not give the correct result in case of NaNs
  61. if (!(gravity.vector3Value == Vector3.zero)) lastSeenCustomGravity = Time.realtimeSinceStartup;
  62. if (Time.realtimeSinceStartup - lastSeenCustomGravity < 2f) {
  63. EditorGUI.indentLevel++;
  64. if (!float.IsNaN(gravity.vector3Value.x)) {
  65. PropertyField(gravity.propertyPath);
  66. }
  67. if (controller == null || !controller.enabled) {
  68. PropertyField(groundMask.propertyPath, "Raycast Ground Mask");
  69. PropertyField(centerOffset.propertyPath, "Raycast Center Offset");
  70. }
  71. EditorGUI.indentLevel--;
  72. }
  73. }
  74. } else {
  75. EditorGUI.BeginDisabledGroup(true);
  76. EditorGUILayout.Popup(new GUIContent(gravity.displayName, "Disabled because a non-kinematic rigidbody is attached"), 0, new [] { new GUIContent("Handled by Rigidbody") });
  77. EditorGUI.EndDisabledGroup();
  78. }
  79. if ((rigid != null || rigid2D != null) && (controller != null && controller.enabled)) {
  80. EditorGUILayout.HelpBox("You are using both a Rigidbody and a Character Controller. Those components are not really designed for that. Please use only one of them.", MessageType.Warning);
  81. }
  82. }
  83. }
  84. }