- Home /
How to create a drop down menu in editor inspector
Hey everyone,
Now I know there have been quite a few questions like this, but just hear me out!!!!!
Want I want to do, is just have a drop down menu that the user can click on, and then select a value in the inspector where the rest of the variables are.... I don't want it in any extra GUI Window or anything, just next to the rest of the variables for a script in the editor!!!!!!! Here is a link to an image on my website that I snapshotted from unity that basically shows what I'm looking for!!!!!!!!!!!!!!!
Where you see "Dropdown" in my lovely computer hand writing, that is the variable name like the rest of them. When the user clicks on "Var value" I want it to show a dropdown box in which they can then pick from a list of values to assign to the variable. And the other little scribble next to that is where there would be a little arrow most likely to show that it's a drop down menu!!!!!!
It's sort of like a Editor GUI Popup, except I want it in the inspector window next to the rest of the variables!
Any suggestions are welcome!!!!!!!!!!!
Thanks!
-Grady
Answer by VivienS · Jan 04, 2012 at 09:56 AM
Hi Grady.
I think what you want is a custom enumeration and a public variable of the type of this enumeration. Unity should display it as a drop down menu, with the elements of the enum as list items. Try:
enum myEnum // your custom enumeration
{
Item1,
Item2,
Item3
};
var dropDown = myEnum.Item1; // this public var should appear as a drop down
cheers, Vivien
O$$anonymous$$, thanks, but how would i use an if() statement to check what the value would be?
Would I go if(dropDown == Item1){ //do stuff here }
it's ok, i just figured it out, i have to use myEnum.Item1 in the if() statement ins$$anonymous$$d of just Item1, but how would i make it so that it has a little downward arrow? I didn't realise that i forgot the link in the post above, so i added it and you might be able to see what i mean!!!!!
EDIT [I realised that the arrow is there, it's just that my screen resolution just cuts off that bit of the screen!!!!!!!]
I also had to go: var dropDown : myEnum; to allow the variable to be set as whatever the value of the enum is!!!!!!!!!!!!!!!!
Thanks for your help!!!!!!!!!!!!!!!!
I read your questions just yet. I've only tried this with C# yet ;) Sry, if the syntax didn't work out completely!
HELP, I did exactly as shown but it says that var can not be used in that context. When I replace it with any datatype, It says it cannot be converted. So what datatype IS myEnum.Item1 ?
I know this is old but anyone needing this answered. You can convert the enum value with .ToString() and then use an If Statement to make a decision based on the string.
Answer by NazmunAnny · Dec 29, 2014 at 05:33 PM
This would be the c# version of the code
public enum Orientation{ Left, Right, Top, Bottom }
public Orientation orientation;
Thanks for sharing!
Also, I learned from another post that if you put this in global space (outside your class braces in your cs file) you can use the enum in other scripts as well.
Answer by triangle4studios · Oct 21, 2020 at 05:53 AM
Enums are declared the same way you declare a class or a struct.
Here is a more complete answer for those who came here looking for an answer and not part of one.
using UnityEngine;
public class UpgradeModule : MonoBehaviour
{
public MyEnum myDropDown = new MyEnum();
}
public enum MyEnum
{
Item1,
Item2,
Item3
};
And a side note, please always declare structs, classes and enums with a capital first letter. It is standard practice. Cheers!
@triangle4studios After declaring the enum outside of other classes, how do you get the value of "Item1" in other scripts then?
e.g. if ($$anonymous$$yEnum == Item1) { //Do this }
You can convert the enum value with .ToString() and then use an If Statement to make a decision based on the string.
Answer by hackerG7 · Oct 21, 2020 at 10:22 AM
You can try the asset dropdown attribute, it is simple and allows dynamic list or array to be selected but not only enum. https://assetstore.unity.com/packages/tools/utilities/dropdown-attribute-180951
Example:
public class Player : Monobehaviour{
public List<string> NameList;
[Dropdown("NameList")]//input the path of the list
public string MyName;
}
Answer by HariharanVN5 · Mar 05, 2021 at 06:20 AM
Actually If you planning not to expose the enum varialbles , we can do with Serializefields
enum Directions
{up,down,left,right}
#pragma warning disable 0649
[Serializefield] Directions direction;
#pragma warning disable 0649
this will expose the enums , I added serializefield to avoid warnings
Your answer
