- Home /
OnInspectorGui : how to get selected field ?
Hello !
I would like to know if it's possible to get the selected field in OnInspectorGui.
On the first image, it would be the foldout "TriggerZone" and on the second image "TestTriggerZone2".
Why I want to get this selected field ? To allow to remove it when key is down rather than add a "remove" button.
Thanks !
Answer by DoTA_KAMIKADzE · May 01, 2015 at 05:06 PM
I've had a hard time understanding what exactly you want, so I'll try to give broad answer. I'll provide an example setup of YourEditor:
[CustomEditor(typeof(YourType))]
public class YourEditor : Editor
{
private YourType _yourCtrl = null;
void OnEnable()
{
_yourCtrl = (YourType)target;
}
public override void OnInspectorGUI()
{
//blablabla
_yourCtrl.yourObjectInYourScript = EditorGUILayout.ObjectField(_yourCtrl.yourObjectInYourScript, typeof(SomeType), false) as SomeType;
//blablabla
}
}
As far as I understood you either want to set it to None (as example for ObjectField) or to remove it from displaying.
For the first one you'll need to either set your object to null:
_yourCtrl.yourObjectInYourScript = null;//anywhere in Editor or the script itself
or to explicitly set it null:
EditorGUILayout.ObjectField(null, typeof(SomeType), false) as SomeType;
If you just don't want to show that field then add some boolean either in script or youreditor (for example let it be showMyField) and change it to false/true on click on condition or on whatever else you want, and then:
if (showMyField) EditorGUILayout.ObjectField(null, typeof(SomeType), false) as SomeType;
Answer by zeman97 · Jan 07, 2016 at 07:55 AM
I'm not sure exactly what you meant, but the title is "How to get selected field?" which I'm going to take to mean "How to get the field that is currently selected in the editor?"
You could use EditorGUIUtility.hotControl which returns the ControlID of the last field the left mouse was clicked on.
You could essentially save the value of hotControl as it updates and compare that to the ID's of the controls you are wanting to check.
int currentControlID = 0;
int myFieldControlID = /*Whatever the ID of the field is*/;
if(EditorGUIUtility.hotControl != 0)
currentControlID = EditorGUIUtility.hotControl;
if(currentControlID == myFieldControlID)
// Create your popup here.
As far as getting the ControlID of the fields you want, I would just suggest making a LabelField that displays EditorGUIUtility.hotControl and what what the value is whenever you click on the field you want.
Answer by uromastix87 · Sep 28, 2017 at 04:26 PM
Here you go. I just encountered this problem. Thanks to zeman97 I was able to get started! using UnityEditor; using UnityEngine; using System.Collections;
[CustomEditor (typeof(BoxColliderHelper))]
[CanEditMultipleObjects]
public class BoxColliderHelperEditor : Editor
{
int val1FieldControlValue, val2FieldControlValue, val3FieldControlValue, selectedField;
float val1 = 0f, val2 = 0f, val3 = 0f;
public override void OnInspectorGUI ()
{
val1FieldControlValue = EditorGUIUtility.GetControlID (new GUIContent ("value01"), FocusType.Passive);
val1 = EditorGUILayout.FloatField (new GUIContent ("value01"), val1);
val2FieldControlValue = EditorGUIUtility.GetControlID (new GUIContent ("value02"), FocusType.Passive);
val2 = EditorGUILayout.FloatField ("value02", val2);
val3FieldControlValue = EditorGUIUtility.GetControlID (new GUIContent ("value03"), FocusType.Passive);
val3 = EditorGUILayout.FloatField ("value03", val3);
//get current selected control
if (EditorGUIUtility.hotControl != 0)
selectedField = EditorGUIUtility.hotControl - 1;
//display values
EditorGUILayout.LabelField (selectedField.ToString ());
EditorGUILayout.LabelField ("01's label " + val1FieldControlValue.ToString ());
EditorGUILayout.LabelField ("02's label " + val2FieldControlValue.ToString ());
EditorGUILayout.LabelField ("03's label " + val3FieldControlValue.ToString ());
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613194513im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using a CustomEditor in the inspector and also seeing normal inspector fields 1 Answer
How do you force a custom inspector to redraw? 7 Answers
Callback when custom PropertyAttribute changes 0 Answers
OnInspectorGUI Custom Drawing? 1 Answer
Custom editor desperation: instance of custom class 1 Answer