- Home /
[Custom Inspector] Selecting Gizmo in Scene View and issues
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[i]);
if (EditorGUI.EndChangeCheck()) {
if (sel != this._aiWaypoints.selected[i]) {
this._aiWaypoints.selected[i] = sel;
SceneView.RepaintAll();
}
}
EditorGUI.BeginChangeCheck();
vec = EditorGUILayout.Vector3Field("", this._aiWaypoints.positions[i]);
if (EditorGUI.EndChangeCheck()) {
if (vec != this._aiWaypoints.positions[i]) {
this._aiWaypoints.positions[i] = 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<SphereCollider>();
sph.radius = 1f;
for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
sph.center = this._aiWaypoints.positions[i];
if (Physics.Raycast(ray)) {
this._aiWaypoints.selected[i] = true;
break;
}
}
DestroyImmediate(this._aiWaypoints.gameObject.GetComponent<SphereCollider>());
}
}
for (int i = 0; i < this._aiWaypoints.positions.Length; i++) {
if (this._aiWaypoints.selected[i]) {
EditorGUI.BeginChangeCheck();
vec = Handles.PositionHandle(this._aiWaypoints.positions[i], Quaternion.identity);
if (EditorGUI.EndChangeCheck()) {
if (vec != this._aiWaypoints.positions[i]) {
this._aiWaypoints.positions[i] = 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.
Answer by Draco Nared · Jul 03, 2015 at 07:39 AM
I managed to find answer that goes around the problem. To make waypoints interactable and movable, instead of Vector3 array, I decided to use GameObject array. Also, I added a GameOject that is a child of target's transform and parent to array of GameObjects and is using HideFlags to hide in hierarchy. This way I don't have waypoints in the hierarchy but I can move them as I want to, so mission accomplished.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201622im_/https://answers.unity.com/themes/thub/images/avi.jpg)