1、enum转int:强制转换
int index = (int)枚举类名.枚举值;
2、int转enum:强制转换
枚举类名 index = (枚举类名)3;
3、enum转object:类型转换
System.Enum.Parse(typeof(枚举类名), "Ring");
object obj = System.Enum.Parse(typeof(枚举类名), "枚举值对应的名称");
4、 string转enum:先转成Object,再强转enum;
(枚举类名)System.Enum.Parse(typeof(枚举类的名字),string字符)
枚举类名 enu= (枚举类名)System.Enum.Parse(typeof(枚举类名), "枚举值对应的名称");
5、示例
public enum EnumT
{
Head,Neck,Chest,Ring,Leg,Bracer,
Boots,Shoulder,Belt,OffHand,
}
public void StringToEnum()
{
string typeStr = "Ring"; //字符串
//根据字符串获得的枚举值是Object类型
object obj = System.Enum.Parse(typeof(EnumT), "Ring");
//将根据字符串得到的Object转成对应的枚举类型
EnumT enu= (EnumT)System.Enum.Parse(typeof(EnumT), typeStr);
//可直接赋值,验证之前类型转换
enu = EnumT.Ring;
//枚举值与int强转
enu = (EnumT)3;
int index = (int)EnumT.Ring;
/* Enum源码方法
* public static object Parse(Type enumType, string value);
*/
Debug.Log(index); //验证枚举值的获取是否正确
}