博客
关于我
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/

你可能感兴趣的文章
Nginx + uWSGI + Flask + Vhost
查看>>
Nginx - Header详解
查看>>
Nginx - 反向代理、负载均衡、动静分离、底层原理(案例实战分析)
查看>>
nginx 1.24.0 安装nginx最新稳定版
查看>>
nginx 301 永久重定向
查看>>
nginx css,js合并插件,淘宝nginx合并js,css插件
查看>>
Nginx gateway集群和动态网关
查看>>
Nginx Location配置总结
查看>>
Nginx log文件写入失败?log文件权限设置问题
查看>>
Nginx Lua install
查看>>
nginx net::ERR_ABORTED 403 (Forbidden)
查看>>
Nginx SSL私有证书自签,且反代80端口
查看>>
Nginx upstream性能优化
查看>>
Nginx 中解决跨域问题
查看>>
nginx 代理解决跨域
查看>>
Nginx 动静分离与负载均衡的实现
查看>>
Nginx 反向代理 MinIO 及 ruoyi-vue-pro 配置 MinIO 详解
查看>>
nginx 反向代理 转发请求时,有时好有时没反应,产生原因及解决
查看>>
Nginx 反向代理解决跨域问题
查看>>
Nginx 反向代理配置去除前缀
查看>>