- Home /
Getting the selected enum value set in the Inspector
I cant seem to understand how I could use the inspector enum to set a variable in the Start function of my MonoBehaviour class.
// Class TypeList [System.Serializable] public class TypeList { public enum types { PLAYER, PROP, ENEMY }
public const string PLAYER = "Player";
public const string PROP = "Prop";
public const string ENEMY = "Enemy";
};
// MonoBehaviour class public TypeList.types selectedType;
public void Start() { // How can I get the selected type from the variable selectedType? string selected = ?; }
Answer by taoa · Mar 01, 2011 at 09:10 AM
How about selectedType.ToString()?
Or, if you absolutely need to pick the string not from the name of the enum value but from some constant string, then assign an incrementing value to each member of your enum, and then create a constant string array where each member stores the string value of the corresponding enum value:
[System.Serializable] public class TypeList { public enum types { PLAYER = 0, PROP = 1, ENEMY = 2 }
public const string[] typesStrings = {"Player", "Prop", "Enemy"};
};
// MonoBehaviour class public TypeList.types selectedType;
public void Start() { string selected = TypeList.typesStrings[(int)selectedType]; }
Or do you mean you want to pick the proper const string from the series you declared in your TypeList class?
Wouldn't it be easier to name your enum values like you want them to look like as strings and use ToString()?
Answer by Bovine · Mar 29, 2011 at 10:44 PM
You can also add attributes to your enum values such that you can get a string or other Value back from it as follows:
public class InfoValue : System.Attribute { private string _value;
public InfoValue(string value)
{
_value = value;
}
public string Value
{
get { return _value; }
}
public static string GetInfoValue(Enum value)
{
string output = string.Empty;
Type type = value.GetType();
FieldInfo fi = type.GetField(value.ToString());
InfoValue[] attrs = fi.GetCustomAttributes(typeof(InfoValue), false) as InfoValue[];
if (attrs.Length > 0) output = attrs[0].Value;
return output;
}
}
And then do something like:
public enum types
{
[InfoValue("Player")]
PLAYER = 0,
[InfoValue("Prop")]
PROP = 1,
[InfoValue("Enemy")]
ENEMY = 2
}
And retrieve this with:
string my_string = InfoValue.GetInfoValue(types);
I am doing this a lot where I have data associated with an enum. In the context of my project, I am working on an RPG, so I have a bunch of statistics associated with each playable race and class and it makes sense to me to associate these with the enum value and then retrieve that.
However, I am doing this kind of thing VERY occasionally during Character Creation and not many times per frame and the code is reasonably heavy-weight compared to other options such as naming your enums such that ToString() can be used. You could also do some static construction of a Dictionary, but that's also heavy-weight.
If you're doing this in Start() methods, then I don't think it would be too bad, but I haven't exactly profiled it.
Hope that helps H
If your doing things the other way around, you would want to do Enum.Parse() perhaps...
Your answer
Follow this Question
Related Questions
Accessible collection of Type 1 Answer
Enum drop down menu in inspector for nested arrays 2 Answers
enum in inspector? 2 Answers
C# Edit enum values in inspector 1 Answer