- Home /
C# public - dropdown selection?
Hello all. I'm looking on how I can declare a public string in c# and it will offer a drop down list within the inspector. What can I google to look to see how it's done?
Answer by fafase · Apr 13, 2013 at 02:31 PM
A drop down list, do you mean a set of choice among a list?
You may want to use an enum then:
public enum State{
walking,idle,attacking,eating,sleeping
}
public class AClass:MonoBehaviour{
public State current;
}
This will show the variable with a list of possible choices
How would I use this? $$anonymous$$eaning, how could I read this information in the script? If you could provide a link in the documentation that'd be good too because I can't find anything
Answer by Aqibsadiq · Nov 20, 2017 at 09:16 AM
Here is a simpler way - Multiple Drop Down Selection
Simple 4 Steps
Step 1 : Make a new Script "EnumFlagsAttribute"
using UnityEngine;
using System.Collections;
public class EnumFlagsAttribute : PropertyAttribute
{
public EnumFlagsAttribute() { }
}
Step 2 : Make another Script "EnumFlagsAttributeDrawer"
using UnityEngine;
using System.Collections;
using System;
using UnityEditor;
[CustomPropertyDrawer(typeof(EnumFlagsAttribute))]
public class EnumFlagsAttributeDrawer : PropertyDrawer
{
public override void OnGUI(Rect _position, SerializedProperty _property, GUIContent _label)
{
_property.intValue = EditorGUI.MaskField( _position, _label, _property.intValue, _property.enumNames );
}
}
Step 3: Enum Declaration
[System.Flags]
public enum FurnitureType
{
None , SofaPrefab , Curtains , Table , Chairs
}
[EnumFlagsAttribute]
public FurnitureType enumType;
Step 4: Getting Selected Elements
List<int> ReturnSelectedElements()
{
List<int> selectedElements = new List<int>();
for (int i = 0; i < System.Enum.GetValues(typeof(FurnitureType)).Length; i++)
{
int layer = 1 << i;
if (((int) enumType & layer) != 0)
{
selectedElements.Add(i);
}
}
return selectedElements;
}
@Bonfire-Boy Oops $$anonymous$$y Bad its a $$anonymous$$ultiple Drop Down Selection not just dropdown selection
Answer by khanbhai1992 · Apr 30, 2020 at 08:56 PM
There is another short trick for this
public enum State{
walking,idle,attacking,eating,sleeping
}
public class AClass:MonoBehaviour{
public static string[] names = {"walking","idle","attacking","eating","sleeping"}
public State current;
public string getCurrentState(){
return names[(int)current];
}
}
,
Several things bad about this, I'm afraid.
You never need to make your own list of names like that, and it's really flaky.
Using System.Enum.GetNames(typeof(State))
when you want an array of the names means you don't need to change the array every time you change the enum.
The only time you should make your own array like that is when you want to make a subset available, or want to specify a different ordering. But even then you're better off with an array of the enum type itself. That way it's typechecked and robust to rena$$anonymous$$g and reordering.
But in any case, if you want the current state as a string why not just do this ?
public string getCurrentState()
{
return current.ToString();
}
...if you really need a function for that, that is - most people would just use the ToString()
as and when it's needed.
You're only making things harder for yourself here.
you are right @Bonfire-Boy. We can should use ToString() in this case. this style is used when have data array relative to enums as in case of script-able objects. Now you will say we can use dictionary in that case..
No I won't say that but I've no idea what you mean. ScriptableObjects makes no difference. Like I said, I can't think of any reason to make an array like that unless you want them sorted differently to the default, or only want to expose a subset of them.
And even then you should still use the enums themselves rather than hardcode their names as strings.
Sorry I know you're trying to be helpful, I'm only critiqueing because a noob could come across your code and think it makes sense. They should learn to use Enum.GetValues
and Enum.GetNames
ins$$anonymous$$d, and remember if you find yourself typing the name of an enum into a string then you're doing things a bad way.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Multidimentional Array for Inventory style system in inspector. 1 Answer
Inspector cannot find script instance 1 Answer
c# enum wont show in inspector 3 Answers