PropertyDrawer 自定义属性绘图
2015-10-10 18:28
309 查看
public class PlayerAttributeExample : MonoBehaviour { //无滑块的属性 public int VIPLevel = 0; //特性限定,有滑块 [Range(0, 10)] public int SliderVIPLevel = 0; }
Range特性的方法实现:
using UnityEngine; using System.Collections; //特性的定义要继承自PropertyAttribute public class MyRangeAttribute : PropertyAttribute { public float Min;//最小值 public float Max;//最大值 public MyRangeAttribute(float min, float max) { this.Min = min; this.Max = max; } }
using UnityEngine; using System.Collections; using UnityEditor; //继承PropertyDrawer, 必须放入Editor文件夹下 [CustomPropertyDrawer(typeof(MyRangeAttribute))] public class MyRangeAttributeDrawer : PropertyDrawer { //重载OnGUI方法 public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { MyRangeAttribute myRange = attribute as MyRangeAttribute; if (property.propertyType == SerializedPropertyType.Integer) { EditorGUI.IntSlider(position, property, (int)myRange.Min, (int)myRange.Max, label); } else if (property.propertyType == SerializedPropertyType.Float) { EditorGUI.Slider(position, property, myRange.Min, myRange.Max, label); } else { } } }
2.绘制多选
public enum SomeFood { 汉堡 = 0, 鸡肉卷 = 1, 薯条 = 3, } //只能单选 public SomeFood MyLoveFood; //多选特性 [EnumListAttribute] public SomeFood MyLoveFoodList;
using UnityEngine; using System.Collections; public class EnumListAttribute : PropertyAttribute { }
using UnityEngine; using System.Collections; using UnityEditor; [CustomPropertyDrawer(typeof(EnumListAttribute))] public class EnumListAttributeDrawer : PropertyDrawer { public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { property.intValue = EditorGUI.MaskField(position, label, property.intValue, property.enumNames); } }
相关文章推荐
- 如何搭建web服务器 使用Nginx搭建反向代理服务器
- Linux chmod命令详解
- jenkins使用Git为源码管理(windows master && linux slave)
- 通过zssh在服务器和本地之间上传和下载文件
- OpenCV基本绘图
- 构造 - SGU 109 Magic of David Copperfield II
- Linux-CentOS7 KDE 中文语言支持
- Linux进程间通信方式--信号,管道,消息队列,信号量,共享内存
- java 获取进程ID 物理内存 cpu使用百分比(linux环境)
- 使用Python + Apache POI提取Excel中的图片
- 手机网站左侧栏弹出,固定整个页面,不让页面滚动
- linux 高并发socket通信模型
- linux下vim不显示高亮解决方案
- 学习boundingRectWithSize:options:attributes:context:计算文本尺寸
- Linux中/var空间不足的解决办法
- MMORPG服务器架构
- linux设备树使用手册
- nginx配置
- Linux下如何修改MySQL物理目录
- linux 信号注册处理发送signal