博客
关于我
Unity3d自定义枚举多选
阅读量:610 次
发布时间:2019-03-12

本文共 1061 字,大约阅读时间需要 3 分钟。

Unity3D中的枚举类型通常默认为单选,但有时候我们需要实现多选功能。这只需要通过脚本实现即可。以下是具体实现步骤:

实现多选枚举的步骤

首先,我们需要创建两个新的C#脚本:EnumFlagsEnumFlagsDrawer

1. 创建EnumFlags脚本

打开EnumFlags脚本,添加如下代码:

using UnityEngine;public class EnumFlags : PropertyAttribute{}

这个脚本继承自PropertyAttribute,用来为属性添加自定义属性。这里不需要额外的字段,仅仅是标记我们的属性应受EnumFlagsDrawer处理。

2. 创建EnumFlagsDrawer脚本

打开EnumFlagsDrawer脚本,添加如下代码:

using UnityEngine;using UnityEditor;[CustomPropertyDrawer(typeof(EnumFlags))]public class EnumFlagsAttributeDrawer : PropertyDrawer{    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)    {        property.intValue = EditorGUI.MaskField(position, label, property.intValue,                                            property.enumNames);    }}

这个脚本继承自PropertyDrawer,添加了一个OnGUI方法。这个方法定义了如何绘制属性界面。当使用EnumFlags属性时,OnGUI会使用EditorGUI.MaskField绘制一个多选框。

3. 实现多选功能

在脚本中使用如下方式:

public enum solt{    solt1,    solt2,    solt3}[sazenFlags]public solt example = solt.solt1;
  • solt1solt2solt3是枚举值。
  • [sazenFlags]注释添加到枚举属性上(可以根据项目需要调整注释名称)。

这样,下一次在Unity中编辑这些属性时,EnumFlagsDrawer会将它们作为多选框显示。

转载地址:http://rqzxz.baihongyu.com/

你可能感兴趣的文章
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>
MySQLIntegrityConstraintViolationException异常处理
查看>>
mysqlreport分析工具详解
查看>>
MySQLSyntaxErrorException: Unknown error 1146和SQLSyntaxErrorException: Unknown error 1146
查看>>
Mysql_Postgresql中_geometry数据操作_st_astext_GeomFromEWKT函数_在java中转换geometry的16进制数据---PostgreSQL工作笔记007
查看>>
mysql_real_connect 参数注意
查看>>
mysql_secure_installation初始化数据库报Access denied
查看>>
MySQL_西安11月销售昨日未上架的产品_20161212
查看>>
Mysql——深入浅出InnoDB底层原理
查看>>
MySQL“被动”性能优化汇总
查看>>
MySQL、HBase 和 Elasticsearch:特点与区别详解
查看>>