Skip to content

Commit e289039

Browse files
Pathway component with Idea (#373)
* Pathway component with Idea * Pathway component deletion of error messages * Pathway component cleaning Pathway component * pathway interactive * Update PathwayGizmos.cs * cleaning * Pathway component update * Pathway component correction * Phatway update * Update PathWayNavMeshUI.cs * Update PathWayNavMeshUI.cs * Update PathWayNavMeshUI.cs * Rename variables add a tooltip * Rename variables Add hide pathway option * Update PathWayNavMeshUI.cs * rename add HasNavMeshAt * Correction DisplayHandles() incorrect use of EditorGUI.BeginChangeCheck (); * Update PathwayHandles.cs * correction HasNavMeshAt, I forgot the delete case Gismos just a rewrite * less calculus more data * cleaning * Update PathWayNavMeshUI.cs Correction * correction * update update in the path only the points moved by the handles * correction * small fixes
1 parent 93ea5d1 commit e289039

File tree

10 files changed

+400
-43
lines changed

10 files changed

+400
-43
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using UnityEngine;
2+
using UnityEditorInternal;
3+
4+
5+
public class PathWayNavMeshUI
6+
{
7+
private Pathway _pathway;
8+
private PathwayNavMesh _pathwayNavMesh;
9+
10+
public PathWayNavMeshUI(Pathway pathway)
11+
{
12+
_pathway = pathway;
13+
_pathwayNavMesh = new PathwayNavMesh(pathway);
14+
RestorePath();
15+
}
16+
17+
public void OnInspectorGUI()
18+
{
19+
if (!_pathway.ToggledNavMeshDisplay)
20+
{
21+
if (GUILayout.Button("NavMesh Path"))
22+
{
23+
_pathway.ToggledNavMeshDisplay = true;
24+
GeneratePath();
25+
InternalEditorUtility.RepaintAllViews();
26+
}
27+
}
28+
else
29+
{
30+
if (GUILayout.Button("Handles Path"))
31+
{
32+
_pathway.ToggledNavMeshDisplay = false;
33+
InternalEditorUtility.RepaintAllViews();
34+
}
35+
}
36+
}
37+
38+
public void UpdatePath() {
39+
40+
if (!_pathway.DisplayProbes)
41+
{
42+
_pathwayNavMesh.UpdatePath();
43+
}
44+
}
45+
46+
public void UpdatePathAt(int index)
47+
{
48+
if (index >= 0)
49+
{
50+
_pathway.DisplayProbes = !_pathwayNavMesh.HasNavMeshAt(index);
51+
52+
if (!_pathway.DisplayProbes && _pathway.ToggledNavMeshDisplay)
53+
{
54+
_pathway.DisplayProbes = !_pathwayNavMesh.UpdateCornersAt(index);
55+
}
56+
}
57+
}
58+
59+
public void RealTime(int index)
60+
{
61+
if (_pathway.RealTimeEnabled)
62+
{
63+
UpdatePathAt(index);
64+
65+
if (_pathway.ToggledNavMeshDisplay)
66+
{
67+
UpdatePath();
68+
}
69+
}
70+
}
71+
72+
private void RestorePath()
73+
{
74+
bool existsPath = true;
75+
76+
_pathway.Hits.Clear();
77+
_pathway.DisplayProbes = false;
78+
79+
if (_pathway.Waypoints.Count > 1)
80+
{
81+
for (int i = 0; i < _pathway.Waypoints.Count; i++)
82+
{
83+
existsPath &= _pathwayNavMesh.HasNavMeshAt(i);
84+
existsPath &= _pathwayNavMesh.UpdateCornersAt(i);
85+
}
86+
87+
if (existsPath)
88+
{
89+
_pathwayNavMesh.UpdatePath();
90+
}
91+
}
92+
93+
_pathway.DisplayProbes = !existsPath;
94+
}
95+
96+
public void GeneratePath()
97+
{
98+
if (_pathway.ToggledNavMeshDisplay)
99+
{
100+
RestorePath();
101+
_pathway.ToggledNavMeshDisplay = !_pathway.DisplayProbes;
102+
}
103+
}
104+
105+
}

UOP1_Project/Assets/Scripts/Editor/Pathway/PathWayNavMeshUI.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayEditor.cs

Lines changed: 49 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
using UnityEngine;
22
using UnityEditor;
33
using UnityEditorInternal;
4-
4+
using System.Linq;
55

66
[CustomEditor(typeof(Pathway))]
77
public class PathwayEditor : Editor
88
{
99
private ReorderableList _reorderableList;
1010
private Pathway _pathway;
1111
private PathwayHandles _pathwayHandles;
12-
12+
private PathWayNavMeshUI _pathWayNavMeshUI;
13+
private enum LIST_MODIFICATION { ADD, SUPP, DRAG, OTHER};
14+
private LIST_MODIFICATION _currentListModification;
15+
private int _indexCurrentModification;
1316

1417
public void OnSceneGUI()
1518
{
16-
_pathwayHandles.DispalyHandles();
19+
int index = _pathwayHandles.DisplayHandles();
20+
_pathWayNavMeshUI.RealTime(index);
1721
}
1822

1923
public override void OnInspectorGUI()
2024
{
2125
DrawDefaultInspector();
2226
serializedObject.Update();
2327
_reorderableList.DoLayoutList();
28+
_pathWayNavMeshUI.OnInspectorGUI();
29+
Tools.hidden = _pathway.HidePathway;
2430
serializedObject.ApplyModifiedProperties();
2531
}
2632

@@ -32,10 +38,12 @@ private void OnEnable()
3238
_reorderableList.drawElementCallback += DrawElement;
3339
_reorderableList.onAddCallback += AddItem;
3440
_reorderableList.onRemoveCallback += RemoveItem;
35-
_reorderableList.onSelectCallback += SelectItem;
3641
_reorderableList.onChangedCallback += ListModified;
42+
_reorderableList.onMouseDragCallback += DragItem;
3743
_pathway = (target as Pathway);
44+
_pathWayNavMeshUI = new PathWayNavMeshUI(_pathway);
3845
_pathwayHandles = new PathwayHandles(_pathway);
46+
_currentListModification = LIST_MODIFICATION.OTHER;
3947
}
4048

4149
private void OnDisable()
@@ -45,8 +53,9 @@ private void OnDisable()
4553
_reorderableList.drawElementCallback -= DrawElement;
4654
_reorderableList.onAddCallback -= AddItem;
4755
_reorderableList.onRemoveCallback -= RemoveItem;
48-
_reorderableList.onSelectCallback -= SelectItem;
4956
_reorderableList.onChangedCallback -= ListModified;
57+
_reorderableList.onMouseDragCallback -= DragItem;
58+
_pathway.waypoints = _pathway.Waypoints.Select(x => x.waypoint).ToArray();
5059
}
5160

5261
private void DrawHeader(Rect rect)
@@ -56,7 +65,7 @@ private void DrawHeader(Rect rect)
5665

5766
private void DrawElement(Rect rect, int index, bool active, bool focused)
5867
{
59-
SerializedProperty item = _reorderableList.serializedProperty.GetArrayElementAtIndex(index);
68+
SerializedProperty item = _reorderableList.serializedProperty.GetArrayElementAtIndex(index).FindPropertyRelative("waypoint");
6069
item.vector3Value = EditorGUI.Vector3Field(rect, Pathway.FIELD_LABEL + index, item.vector3Value);
6170
}
6271

@@ -67,16 +76,18 @@ private void AddItem(ReorderableList list)
6776
if (index > -1 && list.serializedProperty.arraySize >= 1)
6877
{
6978
list.serializedProperty.InsertArrayElementAtIndex(index + 1);
70-
Vector3 previous = list.serializedProperty.GetArrayElementAtIndex(index).vector3Value;
71-
list.serializedProperty.GetArrayElementAtIndex(index + 1).vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
79+
Vector3 previous = list.serializedProperty.GetArrayElementAtIndex(index).FindPropertyRelative("waypoint").vector3Value;
80+
list.serializedProperty.GetArrayElementAtIndex(index + 1).FindPropertyRelative("waypoint").vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
81+
_indexCurrentModification = index + 1;
7282
}
7383
else
7484
{
7585
list.serializedProperty.InsertArrayElementAtIndex(list.serializedProperty.arraySize);
7686
Vector3 previous = _pathway.transform.position;
77-
list.serializedProperty.GetArrayElementAtIndex(list.serializedProperty.arraySize - 1).vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
87+
list.serializedProperty.GetArrayElementAtIndex(list.serializedProperty.arraySize - 1).FindPropertyRelative("waypoint").vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
88+
_indexCurrentModification = list.serializedProperty.arraySize - 1;
7889
}
79-
90+
_currentListModification = LIST_MODIFICATION.ADD;
8091
list.index++;
8192
}
8293

@@ -90,27 +101,51 @@ private void RemoveItem(ReorderableList list)
90101
{
91102
list.index--;
92103
}
93-
104+
_indexCurrentModification = index - 1;
105+
_currentListModification = LIST_MODIFICATION.SUPP;
94106
}
95107

96-
private void SelectItem(ReorderableList list)
108+
private void DragItem(ReorderableList list)
97109
{
98-
InternalEditorUtility.RepaintAllViews();
110+
_indexCurrentModification = list.index;
111+
_currentListModification = LIST_MODIFICATION.DRAG;
99112
}
100113

101114
private void ListModified(ReorderableList list)
102115
{
103116
list.serializedProperty.serializedObject.ApplyModifiedProperties();
117+
118+
switch (_currentListModification)
119+
{
120+
case LIST_MODIFICATION.ADD:
121+
_pathWayNavMeshUI.UpdatePathAt(_indexCurrentModification);
122+
break;
123+
124+
case LIST_MODIFICATION.SUPP:
125+
if (list.serializedProperty.arraySize > 1)
126+
{
127+
_pathWayNavMeshUI.UpdatePathAt((list.serializedProperty.arraySize + _indexCurrentModification) % list.serializedProperty.arraySize);
128+
}
129+
break;
130+
case LIST_MODIFICATION.DRAG:
131+
_pathWayNavMeshUI.UpdatePathAt(list.index);
132+
_pathWayNavMeshUI.UpdatePathAt(_indexCurrentModification);
133+
break;
134+
default:
135+
break;
136+
}
137+
_currentListModification = LIST_MODIFICATION.OTHER;
104138
}
105139

106140
private void DoUndo()
107141
{
108142
serializedObject.UpdateIfRequiredOrScript();
109143

110-
if (_reorderableList.index >= _reorderableList.serializedProperty.arraySize)
144+
if (_reorderableList.index >= _reorderableList.serializedProperty.arraySize )
111145
{
112146
_reorderableList.index = _reorderableList.serializedProperty.arraySize - 1;
113147
}
148+
_pathWayNavMeshUI.GeneratePath();
114149
}
115150

116151
}

UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayEditor.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,93 @@
11
using UnityEngine;
22
using UnityEditor;
3-
using System.Collections.Generic;
43

54

6-
public class PathwayGizmos
5+
public class PathwayGizmos
76
{
87
[DrawGizmo(GizmoType.Selected)]
98
private static void DrawGizmosSelected(Pathway pathway, GizmoType gizmoType)
10-
{
9+
{
10+
if (!pathway.ToggledNavMeshDisplay)
11+
{
12+
DrawHandlesPath(pathway);
13+
}
14+
else
15+
{
16+
DrawNavMeshPath(pathway);
17+
}
1118

12-
DrawHandlesPath(pathway);
19+
DrawHitPoints(pathway);
1320
}
1421

15-
private static void DrawElements(Pathway pathway, List<Vector3> path, int index)
22+
23+
private static void DrawLabel(Pathway pathway,Vector3 path, int index)
1624
{
1725
GUIStyle style = new GUIStyle();
1826
Vector3 textHeight = Vector3.up;
1927

2028
style.normal.textColor = pathway.TextColor;
2129
style.fontSize = pathway.TextSize;
2230

23-
Handles.Label(path[index] + textHeight, index.ToString(), style);
31+
Handles.Label(path + textHeight, index.ToString(), style);
2432
}
2533

2634
private static void DrawHandlesPath(Pathway pathway)
2735
{
36+
Handles.color = pathway.LineColor;
37+
2838
if (pathway.Waypoints.Count != 0)
2939
{
30-
DrawElements(pathway, pathway.Waypoints, 0);
31-
}
40+
DrawLabel(pathway, pathway.Waypoints[0].waypoint, 0);
3241

33-
for (int i = 0; i < pathway.Waypoints.Count; i++)
34-
{
35-
if (i != 0 && pathway.Waypoints.Count > 1)
42+
if (pathway.Waypoints.Count > 1)
3643
{
37-
DrawElements(pathway, pathway.Waypoints, i);
38-
using (new Handles.DrawingScope(pathway.LineColor))
44+
for (int i = 1; i < pathway.Waypoints.Count; i++)
3945
{
40-
Handles.DrawDottedLine(pathway.Waypoints[i - 1], pathway.Waypoints[i], 2);
46+
DrawLabel(pathway, pathway.Waypoints[i].waypoint, i);
47+
Handles.DrawDottedLine(pathway.Waypoints[i - 1].waypoint, pathway.Waypoints[i].waypoint, 2);
4148
}
4249

50+
if (pathway.Waypoints.Count > 2)
51+
{
52+
Handles.DrawDottedLine(pathway.Waypoints[0].waypoint, pathway.Waypoints[pathway.Waypoints.Count - 1].waypoint, 2);
53+
}
4354
}
4455
}
56+
}
4557

46-
if (pathway.Waypoints.Count > 2)
58+
private static void DrawNavMeshPath(Pathway pathway)
59+
{
60+
Handles.color = pathway.LineColor;
61+
62+
for (int i = 0 ; i < pathway.Path.Count - 1; i++)
4763
{
48-
using (new Handles.DrawingScope(pathway.LineColor))
49-
{
50-
Handles.DrawDottedLine(pathway.Waypoints[0], pathway.Waypoints[pathway.Waypoints.Count - 1], 2);
64+
Handles.DrawLine(pathway.Path[i], pathway.Path[i + 1]);
65+
66+
if (i < pathway.Waypoints.Count) {
67+
DrawLabel(pathway, pathway.Waypoints[i].waypoint, i);
5168
}
5269
}
53-
5470
}
5571

72+
private static void DrawHitPoints(Pathway pathway)
73+
{
74+
if (pathway.DisplayProbes)
75+
{
76+
float sphereRadius = pathway.ProbeRadius;
77+
78+
for (int i = 0; i < pathway.Hits.Count; i++)
79+
{
80+
if (pathway.Hits[i])
81+
{
82+
Gizmos.color = new Color(0, 255, 0, 0.5f);
83+
Gizmos.DrawSphere(pathway.Waypoints[i].waypoint, sphereRadius);
84+
}
85+
else
86+
{
87+
Gizmos.color = new Color(255, 0, 0, 0.5f);
88+
Gizmos.DrawSphere(pathway.Waypoints[i].waypoint, sphereRadius);
89+
}
90+
}
91+
}
92+
}
5693
}

UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayGizmos.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)