版权声明:转载 请说明 https://blog.csdn.net/qq2512667/article/details/82621008
public enum ApplyType
{
Passive,
Buff,
SingleTarget,
MulitTarget
}
private void Awake()
{
ApplyType applyType= (ApplyType) System.Enum.Parse( typeof( ApplyType), "Buff"); //装箱与拆箱 返回对象是Obj
Debug.Log( applyType.ToString());
string Buff = "1";
applyType = (ApplyType)int.Parse(Buff);
}
有时候在读取一些 字符数据时,要把它转换换 枚举类型。 System.Enum.Parse 可以 把字符转成 obj类型,返回obj,装箱操作,
强制转换下 类型,拆箱操作,如果 在枚举里面没有包含 Buff则会显示转换失败,包一个Argument Erro;
上面是读取 string的时候, 还有就是读取 数字的时候,
applyType = (ApplyType)int.Parse(Buff); 这样就可以把 想要的枚举类型存进对象里面。