Hi,
I am creating my own waypoint system for AI and decided to implement custom inspector for handling waypoints. I didn’t want to use gameobjects as there would be too much work for other users. That’s why I decided to draw gizmos and select them in scene view.
I found a solution how to do it, however, it doesn’t seem to work as I wanted and just selects the first gizmo I drew, even when I click other gizmos.
Here is some code with my implementation:
public class AIWaypointsInspector : Editor {
//_aiWaypoints.positions is a vector3[] with waypoints positions; IMPORTANT NOTE: I previously added two waypoints there to check the script
//_aiWaypoints.selected is a bool[] which shows which waypoint is selected
public override void OnInspectorGUI() {
if (this._aiWaypoints == null) {
this._aiWaypoints = (AIWaypoints)target;
}
this.DrawWaypoints();
}
private void DrawWaypoints() {
Vector3 vec;
this._moreThanOneSelected = false;
for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
EditorGUI.BeginChangeCheck();
bool sel = EditorGUILayout.ToggleLeft("", this._aiWaypoints.selected*);*
if (EditorGUI.EndChangeCheck()) {
if (sel != this.aiWaypoints.selected*) {*
this.aiWaypoints.selected = sel;
SceneView.RepaintAll();
}
}
EditorGUI.BeginChangeCheck();
vec = EditorGUILayout.Vector3Field(“”, this.aiWaypoints.positions*);*
if (EditorGUI.EndChangeCheck()) {
if (vec != this.aiWaypoints.positions*) {*
this.aiWaypoints.positions = vec;
SceneView.RepaintAll();
}
}
}_
}
private void OnSceneGUI() {
if (this._aiWaypoints == null) {
this._aiWaypoints = (AIWaypoints)target;
}
Vector3 vec;
if (Event.current.type == EventType.MouseDown) {
if (Event.current.button == 0) {
Ray ray = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
SphereCollider sph = this._aiWaypoints.gameObject.AddComponent();
sph.radius = 1f;
for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
sph.center = this.aiWaypoints.positions*;*
if (Physics.Raycast(ray)) {
this.aiWaypoints.selected = true;
break;
}
}
DestroyImmediate(this._aiWaypoints.gameObject.GetComponent());
}
}
for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
if (this.aiWaypoints.selected*) {*
EditorGUI.BeginChangeCheck();
vec = Handles.PositionHandle(this.aiWaypoints.positions*, Quaternion.identity);*
if (EditorGUI.EndChangeCheck()) {
if (vec != this.aiWaypoints.positions*) {
this.aiWaypoints.positions = vec;
}*
}
}
}
if (GUI.changed) {
EditorUtility.SetDirty(this._aiWaypoints);
}
}}
I don’t know what I do wrong. I will be grateful for any suggestions and help.