Unity 经典教程 Editor扩展之RangeAttribute

 时间:2026-02-12 17:09:34

1、RangeAttribute:Attribute used to make a float or int variable in a script be restricted to a specific range.

When this attribute is used, the float or int will be shown as a slider in the Inspector instead of the default number field.

1、打开Unity,新建一个空工程,Unity界面如下图

Unity 经典教程 Editor扩展之RangeAttribute

2、在工程里面新建一个脚本,可以命名为“RangeTest”,具体如下图

Unity 经典教程 Editor扩展之RangeAttribute

3、选中脚本“RangeTest”,双击脚本或者右键“Open C# Project”打开脚本,具体如下图

Unity 经典教程 Editor扩展之RangeAttribute

4、在打开的脚本上进行代码编辑,用“Range”来限定一个变量的取值范围,具体代码如下图

Unity 经典教程 Editor扩展之RangeAttribute

5、脚本编译正确后,回到Unity界面,新建一个“GameObject”,然后把“RangeTest”赋给“GameObject”,即可看到脚本中对“age”的数值限定,具体如下图

Unity 经典教程 Editor扩展之RangeAttribute

6、到此,《Unity 经典教程 Editor扩展之RangeAttribute的使用》讲解结束,更多资料可参见 Unity Manul 手册,谢谢

  • 手把手教你更改Tkinter(TK)窗口默认图标
  • C#如何查找数组
  • Postman如何发送Post请求
  • 教你解决注册表无法打开
  • win11修改密码如何操作
  • 热门搜索
    砂锅的做法大全 请病假工资怎么算 印度为什么叫阿三 粉皮的做法大全 炒肝的做法 优博奶粉怎么样 第一次进不去怎么办 泯怎么读 鳜怎么读 打嗝怎么办 怎么止嗝