本文转自: http://www.xuanyusong.com/archives/3680雨松MOMO

Unity提供了强大的Editor功能, 我们可以很轻易的在EditorGUI中绘制任意的属性。比如我之前写的文章 http://www.xuanyusong.com/archives/2202

那么问题就来了,如果我有多属性想共用同一段自定义控件,那么这种方法我就需要在多份代码里绘制控件了OnInspectorGUI 这一节中我们需要用到两个全新的自定义属性PropertyAttribute和PropertyDrawer。 可以理解为一个是数据,一个是渲染。

数据代码:

using UnityEngine;
using System.Collections; public class MyTestAttribute : PropertyAttribute { public int max;
public int min; public MyTestAttribute(int a, int b){
min =a;
max =b;
}
}

渲染代码,如果你想做一些复杂的结构,直接在OnGUI里面插入代码即可。

using UnityEditor;
using System.Collections.Generic;
using UnityEngine; [CustomPropertyDrawer(typeof(MyTestAttribute))]
public class MyTestDrawer : PropertyDrawer { public override void OnGUI (UnityEngine.Rect position, SerializedProperty property, UnityEngine.GUIContent label)
{
MyTestAttribute attribute = (MyTestAttribute)base.attribute; property.intValue =Mathf.Min(Mathf.Max(EditorGUI.IntField(position,label.text,property.intValue),attribute.min),attribute.max);
}
}

最后在需要用这个通用组件的代码上添加如下代码即可。

using UnityEngine;
using System.Collections; public class Game : MonoBehaviour { [MyTestAttribute(,)]
public int intValue = ;
}

如下图所示,这个属性的渲染就已经完全独立出来了。

最新文章

  1. 【BZOJ-4698】Sandy的卡片 后缀数组
  2. php报表使用
  3. Linux下tar-rar-unrar解压/压缩缩命令大全
  4. p364习题1
  5. python3.4安装suds
  6. 未能加载Connector/NET :: v6.7.4
  7. Sublime Text 3 代码格式化插件推荐 CodeFormatter
  8. HDU-- Buy Tickets
  9. asp发邮件控件
  10. 最近一些朋友问我,临近快毕业了专业不对口,想转行看到IT行业就业前景不错,但是编程语言众多不了解,不知道哪门语言能够快速入门掌握,短期能让我找到工作
  11. springmvc 添加@ResponseBody
  12. python记录day_20 多继承
  13. qt资源加载出错
  14. 用C语言操作MySQL数据库,进行连接、插入、修改、删除等操作
  15. .NET 中小心嵌套等待的 Task,它可能会耗尽你线程池的现有资源,出现类似死锁的情况
  16. hdu 1253 胜利大逃亡 (广搜)
  17. JS判断键盘上的上下左右键
  18. SKIP(插入空行)
  19. Linux命令之find(一)
  20. msm8909+android5.1分区及烧录的镜像文件介绍【转】

热门文章

  1. 强连通 HDU 1827
  2. 寻找数组中的第K大的元素,多种解法以及分析
  3. Android中对内存和外存的读写
  4. 60.Android通用流行框架大全
  5. sstream使用简介
  6. RabbitMQctl命令
  7. 使用PreApplicationStartMethodAttribute
  8. hihocoder #1112 树上的好路径
  9. HDU #5733 tetrahedron
  10. JavaScript制作时钟特效