- Home /
how to create a drop-down menu in inspector
Hi, I want to bring the presentation in the dialog class to a drop-down list. But I hit the chairs.
help
I think you're looking for something like this?
public class DropDownEditor : Editor
{
private Dialog diag;
private int selectedIndex = 0;
private void OnEnable()
{
diag = (Dialog)target;
}
public override void OnInspectorGUI()
{
selectedIndex = EditorGUILayout.Popup("Label", selectedIndex, diag.nameusers);
}
}
Edit: Note that custom editors can only be written for scripts which inherit from $$anonymous$$onoBehaviour.
Err can It says that the dialog class must be monobehaviour. I do not need to get it
Your CustomEditor
is for type DialogUserSet
not for Dialog
Answer by Hellium · Mar 14, 2019 at 07:55 AM
What you need is a PropertyDrawer
(to draw a serialized field declared in a MonoBehaviour), not a CustomEditor
.
Following code not tested because you have provided your code as images instead of plain text....
[CustomPropertyDrawer(typeof( Dialog ))]
public class DialogDrawer : PropertyDrawer
{
int _choiceIndex;
string[] _choices = new string[]{ "User1", "User2" };
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
SerializedProperty userIndexProperty = property.FindPropertyRelative("UserIndex");
EditorGUI.BeginChangeCheck();
_choiceIndex = EditorGUI.Popup( position, userIndexProperty.intValue, _choices );
if( EditorGUI.EndChangeCheck() )
{
userIndexProperty.intValue = _choiceIndex;
}
}
}
tnx bro, But there is a problem. I do not see other information in the "dialog" class in the insector. How to fix this?
You will have to retrieve the other properties and call EditorGUI.PropertyField
for each one.
https://docs.unity3d.com/ScriptReference/EditorGUI.PropertyField.html
Actually it should work when you just point your "CustomPropertyDrawer" to an attribute which you attach to the UserIndex field. That way this property drawer does not replace the drawing of the whole Dialog but only that one sub property. In this case the "property" parameter in the property drawer will already be the UserIndex.
Though simply using an enum ins$$anonymous$$d of that int field would probably be simpler.