Unity学习记录4-枚举类型

一、枚举类型

一种给数字命名的方式

二、创建一个枚举类型

示例代码:

声明枚举类型:

1
2
3
4
5
6
7
public enum state
{
Singing=1,
Junmping=2,
Rapping=3,
Basketballing=4,
}

指定枚举的数字数据类型:

1
2
3
4
5
6
7
public enum hatNum : long
{
common = 0,
christmas = 1,
cow = 2,
block = 3
}

创建枚举类型变量:

1
public state cstate=state.Rapping;

三、枚举的简单应用

  1. 转化为整数

    1
    int a = (int)hatNum.cow;
  2. 转化为字符串

    1
    string b = hatNum.cow.ToString();
  3. GetName方法:检索指定枚举中常数名称的数组

    1
    string hatName = System.Enum.GetName(typeof(hatNum), 3);
  4. Parse方法:将一个或多个枚举常数的名称或数字转换成等效的枚举对象

    1
    2
    3
    4
    5
    6
    string hatName = System.Enum.Parse(typeof(hatNum), "0").ToString();

    int num = Convert.ToInt32(Enum.Parse(typeof(hatNum), hatNum.cow.ToString()));

    //第三个参数为是否忽略大小写
    int num = Convert.ToInt32(Enum.Parse(typeof(hatNum), "cow",true));