Unity获取Enum内对应索引、名字、值

public enum PosType
{
    Up,
    Down,
    Left,
    Right
}

获取在Enum内索引

 PosType type  = PosType.Down;
 int index =type.GetHashCode(); ;

获取在Enum内对应名字

int type =0;
string name= Enum.GetName(typeof(PosType), type);

获取对应Enum值

int type =0;
PosType pos =(PosType)type;
//or
//PosType pos = (PosType)Enum.Parse(typeof(PosType), Enum.GetName(typeof(PosType), type));

获取随机Enum值

PosType pos = (PosType)Random.Range(0, 4);

版权声明:本文为hu_xingyue原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。