NavMeshModifierVolumeEditor.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. using UnityEditor.IMGUI.Controls;
  2. using UnityEditorInternal;
  3. using UnityEngine.AI;
  4. using UnityEngine;
  5. namespace UnityEditor.AI
  6. {
  7. [CanEditMultipleObjects]
  8. [CustomEditor(typeof(NavMeshModifierVolume))]
  9. class NavMeshModifierVolumeEditor : Editor
  10. {
  11. SerializedProperty m_AffectedAgents;
  12. SerializedProperty m_Area;
  13. SerializedProperty m_Center;
  14. SerializedProperty m_Size;
  15. static Color s_HandleColor = new Color(187f, 138f, 240f, 210f) / 255;
  16. static Color s_HandleColorDisabled = new Color(187f * 0.75f, 138f * 0.75f, 240f * 0.75f, 100f) / 255;
  17. BoxBoundsHandle m_BoundsHandle = new BoxBoundsHandle();
  18. bool editingCollider
  19. {
  20. get { return EditMode.editMode == EditMode.SceneViewEditMode.Collider && EditMode.IsOwner(this); }
  21. }
  22. void OnEnable()
  23. {
  24. m_AffectedAgents = serializedObject.FindProperty("m_AffectedAgents");
  25. m_Area = serializedObject.FindProperty("m_Area");
  26. m_Center = serializedObject.FindProperty("m_Center");
  27. m_Size = serializedObject.FindProperty("m_Size");
  28. NavMeshVisualizationSettings.showNavigation++;
  29. }
  30. void OnDisable()
  31. {
  32. NavMeshVisualizationSettings.showNavigation--;
  33. }
  34. Bounds GetBounds()
  35. {
  36. var navModifier = (NavMeshModifierVolume)target;
  37. return new Bounds(navModifier.transform.position, navModifier.size);
  38. }
  39. public override void OnInspectorGUI()
  40. {
  41. serializedObject.Update();
  42. EditMode.DoEditModeInspectorModeButton(EditMode.SceneViewEditMode.Collider, "Edit Volume",
  43. EditorGUIUtility.IconContent("EditCollider"), GetBounds, this);
  44. EditorGUILayout.PropertyField(m_Size);
  45. EditorGUILayout.PropertyField(m_Center);
  46. NavMeshComponentsGUIUtility.AreaPopup("Area Type", m_Area);
  47. NavMeshComponentsGUIUtility.AgentMaskPopup("Affected Agents", m_AffectedAgents);
  48. EditorGUILayout.Space();
  49. serializedObject.ApplyModifiedProperties();
  50. }
  51. [DrawGizmo(GizmoType.Selected | GizmoType.Active)]
  52. static void RenderBoxGizmo(NavMeshModifierVolume navModifier, GizmoType gizmoType)
  53. {
  54. var color = navModifier.enabled ? s_HandleColor : s_HandleColorDisabled;
  55. var colorTrans = new Color(color.r * 0.75f, color.g * 0.75f, color.b * 0.75f, color.a * 0.15f);
  56. var oldColor = Gizmos.color;
  57. var oldMatrix = Gizmos.matrix;
  58. Gizmos.matrix = navModifier.transform.localToWorldMatrix;
  59. Gizmos.color = colorTrans;
  60. Gizmos.DrawCube(navModifier.center, navModifier.size);
  61. Gizmos.color = color;
  62. Gizmos.DrawWireCube(navModifier.center, navModifier.size);
  63. Gizmos.matrix = oldMatrix;
  64. Gizmos.color = oldColor;
  65. Gizmos.DrawIcon(navModifier.transform.position, "NavMeshModifierVolume Icon", true);
  66. }
  67. [DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.Pickable)]
  68. static void RenderBoxGizmoNotSelected(NavMeshModifierVolume navModifier, GizmoType gizmoType)
  69. {
  70. if (NavMeshVisualizationSettings.showNavigation > 0)
  71. {
  72. var color = navModifier.enabled ? s_HandleColor : s_HandleColorDisabled;
  73. var oldColor = Gizmos.color;
  74. var oldMatrix = Gizmos.matrix;
  75. Gizmos.matrix = navModifier.transform.localToWorldMatrix;
  76. Gizmos.color = color;
  77. Gizmos.DrawWireCube(navModifier.center, navModifier.size);
  78. Gizmos.matrix = oldMatrix;
  79. Gizmos.color = oldColor;
  80. }
  81. Gizmos.DrawIcon(navModifier.transform.position, "NavMeshModifierVolume Icon", true);
  82. }
  83. void OnSceneGUI()
  84. {
  85. if (!editingCollider)
  86. return;
  87. var vol = (NavMeshModifierVolume)target;
  88. var color = vol.enabled ? s_HandleColor : s_HandleColorDisabled;
  89. using (new Handles.DrawingScope(color, vol.transform.localToWorldMatrix))
  90. {
  91. m_BoundsHandle.center = vol.center;
  92. m_BoundsHandle.size = vol.size;
  93. EditorGUI.BeginChangeCheck();
  94. m_BoundsHandle.DrawHandle();
  95. if (EditorGUI.EndChangeCheck())
  96. {
  97. Undo.RecordObject(vol, "Modified NavMesh Modifier Volume");
  98. Vector3 center = m_BoundsHandle.center;
  99. Vector3 size = m_BoundsHandle.size;
  100. vol.center = center;
  101. vol.size = size;
  102. EditorUtility.SetDirty(target);
  103. }
  104. }
  105. }
  106. [MenuItem("GameObject/AI/NavMesh Modifier Volume", false, 2001)]
  107. static public void CreateNavMeshModifierVolume(MenuCommand menuCommand)
  108. {
  109. var parent = menuCommand.context as GameObject;
  110. var go = NavMeshComponentsGUIUtility.CreateAndSelectGameObject("NavMesh Modifier Volume", parent);
  111. go.AddComponent<NavMeshModifierVolume>();
  112. var view = SceneView.lastActiveSceneView;
  113. if (view != null)
  114. view.MoveToView(go.transform);
  115. }
  116. }
  117. }