1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
#define NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.IMGUI.Controls;
using UnityEditor.SceneManagement;
using UnityEditorInternal;
using UnityEngine.AI;
using UnityEngine;
namespace UnityEditor.AI
{
[CanEditMultipleObjects]
[CustomEditor(typeof(NavMeshSurface))]
class NavMeshSurfaceEditor : Editor
{
SerializedProperty m_AgentTypeID;
SerializedProperty m_BuildHeightMesh;
SerializedProperty m_Center;
SerializedProperty m_CollectObjects;
SerializedProperty m_DefaultArea;
SerializedProperty m_LayerMask;
SerializedProperty m_OverrideTileSize;
SerializedProperty m_OverrideVoxelSize;
SerializedProperty m_Size;
SerializedProperty m_TileSize;
SerializedProperty m_UseGeometry;
SerializedProperty m_VoxelSize;
#if NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF
SerializedProperty m_NavMeshData;
#endif
class Styles
{
public readonly GUIContent m_LayerMask = new GUIContent("Include Layers");
public readonly GUIContent m_ShowInputGeom = new GUIContent("Show Input Geom");
public readonly GUIContent m_ShowVoxels = new GUIContent("Show Voxels");
public readonly GUIContent m_ShowRegions = new GUIContent("Show Regions");
public readonly GUIContent m_ShowRawContours = new GUIContent("Show Raw Contours");
public readonly GUIContent m_ShowContours = new GUIContent("Show Contours");
public readonly GUIContent m_ShowPolyMesh = new GUIContent("Show Poly Mesh");
public readonly GUIContent m_ShowPolyMeshDetail = new GUIContent("Show Poly Mesh Detail");
}
static Styles s_Styles;
static bool s_ShowDebugOptions;
static Color s_HandleColor = new Color(127f, 214f, 244f, 100f) / 255;
static Color s_HandleColorSelected = new Color(127f, 214f, 244f, 210f) / 255;
static Color s_HandleColorDisabled = new Color(127f * 0.75f, 214f * 0.75f, 244f * 0.75f, 100f) / 255;
BoxBoundsHandle m_BoundsHandle = new BoxBoundsHandle();
bool editingCollider
{
get { return EditMode.editMode == EditMode.SceneViewEditMode.Collider && EditMode.IsOwner(this); }
}
void OnEnable()
{
m_AgentTypeID = serializedObject.FindProperty("m_AgentTypeID");
m_BuildHeightMesh = serializedObject.FindProperty("m_BuildHeightMesh");
m_Center = serializedObject.FindProperty("m_Center");
m_CollectObjects = serializedObject.FindProperty("m_CollectObjects");
m_DefaultArea = serializedObject.FindProperty("m_DefaultArea");
m_LayerMask = serializedObject.FindProperty("m_LayerMask");
m_OverrideTileSize = serializedObject.FindProperty("m_OverrideTileSize");
m_OverrideVoxelSize = serializedObject.FindProperty("m_OverrideVoxelSize");
m_Size = serializedObject.FindProperty("m_Size");
m_TileSize = serializedObject.FindProperty("m_TileSize");
m_UseGeometry = serializedObject.FindProperty("m_UseGeometry");
m_VoxelSize = serializedObject.FindProperty("m_VoxelSize");
#if NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF
m_NavMeshData = serializedObject.FindProperty("m_NavMeshData");
#endif
NavMeshVisualizationSettings.showNavigation++;
}
void OnDisable()
{
NavMeshVisualizationSettings.showNavigation--;
}
Bounds GetBounds()
{
var navSurface = (NavMeshSurface)target;
return new Bounds(navSurface.transform.position, navSurface.size);
}
public override void OnInspectorGUI()
{
if (s_Styles == null)
s_Styles = new Styles();
serializedObject.Update();
var bs = NavMesh.GetSettingsByID(m_AgentTypeID.intValue);
if (bs.agentTypeID != -1)
{
// Draw image
const float diagramHeight = 80.0f;
Rect agentDiagramRect = EditorGUILayout.GetControlRect(false, diagramHeight);
NavMeshEditorHelpers.DrawAgentDiagram(agentDiagramRect, bs.agentRadius, bs.agentHeight, bs.agentClimb, bs.agentSlope);
}
NavMeshComponentsGUIUtility.AgentTypePopup("Agent Type", m_AgentTypeID);
EditorGUILayout.Space();
EditorGUILayout.PropertyField(m_CollectObjects);
if ((CollectObjects)m_CollectObjects.enumValueIndex == CollectObjects.Volume)
{
EditorGUI.indentLevel++;
EditMode.DoEditModeInspectorModeButton(EditMode.SceneViewEditMode.Collider, "Edit Volume",
EditorGUIUtility.IconContent("EditCollider"), GetBounds, this);
EditorGUILayout.PropertyField(m_Size);
EditorGUILayout.PropertyField(m_Center);
EditorGUI.indentLevel--;
}
else
{
if (editingCollider)
EditMode.QuitEditMode();
}
EditorGUILayout.PropertyField(m_LayerMask, s_Styles.m_LayerMask);
EditorGUILayout.PropertyField(m_UseGeometry);
EditorGUILayout.Space();
m_OverrideVoxelSize.isExpanded = EditorGUILayout.Foldout(m_OverrideVoxelSize.isExpanded, "Advanced");
if (m_OverrideVoxelSize.isExpanded)
{
EditorGUI.indentLevel++;
NavMeshComponentsGUIUtility.AreaPopup("Default Area", m_DefaultArea);
// Override voxel size.
EditorGUILayout.PropertyField(m_OverrideVoxelSize);
using (new EditorGUI.DisabledScope(!m_OverrideVoxelSize.boolValue || m_OverrideVoxelSize.hasMultipleDifferentValues))
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_VoxelSize);
if (!m_OverrideVoxelSize.hasMultipleDifferentValues)
{
if (!m_AgentTypeID.hasMultipleDifferentValues)
{
float voxelsPerRadius = m_VoxelSize.floatValue > 0.0f ? (bs.agentRadius / m_VoxelSize.floatValue) : 0.0f;
EditorGUILayout.LabelField(" ", voxelsPerRadius.ToString("0.00") + " voxels per agent radius", EditorStyles.miniLabel);
}
if (m_OverrideVoxelSize.boolValue)
EditorGUILayout.HelpBox("Voxel size controls how accurately the navigation mesh is generated from the level geometry. A good voxel size is 2-4 voxels per agent radius. Making voxel size smaller will increase build time.", MessageType.None);
}
EditorGUI.indentLevel--;
}
// Override tile size
EditorGUILayout.PropertyField(m_OverrideTileSize);
using (new EditorGUI.DisabledScope(!m_OverrideTileSize.boolValue || m_OverrideTileSize.hasMultipleDifferentValues))
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(m_TileSize);
if (!m_TileSize.hasMultipleDifferentValues && !m_VoxelSize.hasMultipleDifferentValues)
{
float tileWorldSize = m_TileSize.intValue * m_VoxelSize.floatValue;
EditorGUILayout.LabelField(" ", tileWorldSize.ToString("0.00") + " world units", EditorStyles.miniLabel);
}
if (!m_OverrideTileSize.hasMultipleDifferentValues)
{
if (m_OverrideTileSize.boolValue)
EditorGUILayout.HelpBox("Tile size controls the how local the changes to the world are (rebuild or carve). Small tile size allows more local changes, while potentially generating more data overall.", MessageType.None);
}
EditorGUI.indentLevel--;
}
// Height mesh
using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.PropertyField(m_BuildHeightMesh);
}
EditorGUILayout.Space();
EditorGUI.indentLevel--;
}
EditorGUILayout.Space();
serializedObject.ApplyModifiedProperties();
var hadError = false;
var multipleTargets = targets.Length > 1;
foreach (NavMeshSurface navSurface in targets)
{
var settings = navSurface.GetBuildSettings();
// Calculating bounds is potentially expensive when unbounded - so here we just use the center/size.
// It means the validation is not checking vertical voxel limit correctly when the surface is set to something else than "in volume".
var bounds = new Bounds(Vector3.zero, Vector3.zero);
if (navSurface.collectObjects == CollectObjects.Volume)
{
bounds = new Bounds(navSurface.center, navSurface.size);
}
var errors = settings.ValidationReport(bounds);
if (errors.Length > 0)
{
if (multipleTargets)
EditorGUILayout.LabelField(navSurface.name);
foreach (var err in errors)
{
EditorGUILayout.HelpBox(err, MessageType.Warning);
}
GUILayout.BeginHorizontal();
GUILayout.Space(EditorGUIUtility.labelWidth);
if (GUILayout.Button("Open Agent Settings...", EditorStyles.miniButton))
NavMeshEditorHelpers.OpenAgentSettings(navSurface.agentTypeID);
GUILayout.EndHorizontal();
hadError = true;
}
}
if (hadError)
EditorGUILayout.Space();
#if NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF
var nmdRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);
EditorGUI.BeginProperty(nmdRect, GUIContent.none, m_NavMeshData);
var rectLabel = EditorGUI.PrefixLabel(nmdRect, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(m_NavMeshData.displayName));
EditorGUI.EndProperty();
using (new EditorGUI.DisabledScope(true))
{
EditorGUI.BeginProperty(nmdRect, GUIContent.none, m_NavMeshData);
EditorGUI.ObjectField(rectLabel, m_NavMeshData, GUIContent.none);
EditorGUI.EndProperty();
}
#endif
using (new EditorGUI.DisabledScope(Application.isPlaying || m_AgentTypeID.intValue == -1))
{
GUILayout.BeginHorizontal();
GUILayout.Space(EditorGUIUtility.labelWidth);
if (GUILayout.Button("Clear"))
{
NavMeshAssetManager.instance.ClearSurfaces(targets);
SceneView.RepaintAll();
}
if (GUILayout.Button("Bake"))
{
NavMeshAssetManager.instance.StartBakingSurfaces(targets);
}
GUILayout.EndHorizontal();
}
// Show progress for the selected targets
var bakeOperations = NavMeshAssetManager.instance.GetBakeOperations();
for (int i = bakeOperations.Count - 1; i >= 0; --i)
{
if (!targets.Contains(bakeOperations[i].surface))
continue;
var oper = bakeOperations[i].bakeOperation;
if (oper == null)
continue;
var p = oper.progress;
if (oper.isDone)
{
SceneView.RepaintAll();
continue;
}
GUILayout.BeginHorizontal();
if (GUILayout.Button("Cancel", EditorStyles.miniButton))
{
var bakeData = bakeOperations[i].bakeData;
UnityEngine.AI.NavMeshBuilder.Cancel(bakeData);
bakeOperations.RemoveAt(i);
}
EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), p, "Baking: " + (int)(100 * p) + "%");
if (p <= 1)
Repaint();
GUILayout.EndHorizontal();
}
}
[DrawGizmo(GizmoType.Selected | GizmoType.Active | GizmoType.Pickable)]
static void RenderBoxGizmoSelected(NavMeshSurface navSurface, GizmoType gizmoType)
{
RenderBoxGizmo(navSurface, gizmoType, true);
}
[DrawGizmo(GizmoType.NotInSelectionHierarchy | GizmoType.Pickable)]
static void RenderBoxGizmoNotSelected(NavMeshSurface navSurface, GizmoType gizmoType)
{
if (NavMeshVisualizationSettings.showNavigation > 0)
RenderBoxGizmo(navSurface, gizmoType, false);
else
Gizmos.DrawIcon(navSurface.transform.position, "NavMeshSurface Icon", true);
}
static void RenderBoxGizmo(NavMeshSurface navSurface, GizmoType gizmoType, bool selected)
{
var color = selected ? s_HandleColorSelected : s_HandleColor;
if (!navSurface.enabled)
color = s_HandleColorDisabled;
var oldColor = Gizmos.color;
var oldMatrix = Gizmos.matrix;
// Use the unscaled matrix for the NavMeshSurface
var localToWorld = Matrix4x4.TRS(navSurface.transform.position, navSurface.transform.rotation, Vector3.one);
Gizmos.matrix = localToWorld;
if (navSurface.collectObjects == CollectObjects.Volume)
{
Gizmos.color = color;
Gizmos.DrawWireCube(navSurface.center, navSurface.size);
if (selected && navSurface.enabled)
{
var colorTrans = new Color(color.r * 0.75f, color.g * 0.75f, color.b * 0.75f, color.a * 0.15f);
Gizmos.color = colorTrans;
Gizmos.DrawCube(navSurface.center, navSurface.size);
}
}
else
{
if (navSurface.navMeshData != null)
{
var bounds = navSurface.navMeshData.sourceBounds;
Gizmos.color = Color.grey;
Gizmos.DrawWireCube(bounds.center, bounds.size);
}
}
Gizmos.matrix = oldMatrix;
Gizmos.color = oldColor;
Gizmos.DrawIcon(navSurface.transform.position, "NavMeshSurface Icon", true);
}
void OnSceneGUI()
{
if (!editingCollider)
return;
var navSurface = (NavMeshSurface)target;
var color = navSurface.enabled ? s_HandleColor : s_HandleColorDisabled;
var localToWorld = Matrix4x4.TRS(navSurface.transform.position, navSurface.transform.rotation, Vector3.one);
using (new Handles.DrawingScope(color, localToWorld))
{
m_BoundsHandle.center = navSurface.center;
m_BoundsHandle.size = navSurface.size;
EditorGUI.BeginChangeCheck();
m_BoundsHandle.DrawHandle();
if (EditorGUI.EndChangeCheck())
{
Undo.RecordObject(navSurface, "Modified NavMesh Surface");
Vector3 center = m_BoundsHandle.center;
Vector3 size = m_BoundsHandle.size;
navSurface.center = center;
navSurface.size = size;
EditorUtility.SetDirty(target);
}
}
}
[MenuItem("GameObject/AI/NavMesh Surface", false, 2000)]
public static void CreateNavMeshSurface(MenuCommand menuCommand)
{
var parent = menuCommand.context as GameObject;
var go = NavMeshComponentsGUIUtility.CreateAndSelectGameObject("NavMesh Surface", parent);
go.AddComponent<NavMeshSurface>();
var view = SceneView.lastActiveSceneView;
if (view != null)
view.MoveToView(go.transform);
}
}
}
|