[Unity] Unity3D研究院编辑器之独立Inspector属性
2016-03-22 09:26
288 查看
本文转自: http://www.xuanyusong.com/archives/3680 雨松MOMO
Unity提供了强大的Editor功能, 我们可以很轻易的在EditorGUI中绘制任意的属性。比如我之前写的文章 http://www.xuanyusong.com/archives/2202
那么问题就来了,如果我有多属性想共用同一段自定义控件,那么这种方法我就需要在多份代码里绘制控件了OnInspectorGUI 这一节中我们需要用到两个全新的自定义属性PropertyAttribute和PropertyDrawer。 可以理解为一个是数据,一个是渲染。
数据代码:
渲染代码,如果你想做一些复杂的结构,直接在OnGUI里面插入代码即可。
最后在需要用这个通用组件的代码上添加如下代码即可。
如下图所示,这个属性的渲染就已经完全独立出来了。
本文固定链接: http://www.xuanyusong.com/archives/3680
转载请注明: 雨松MOMO 2015年10月21日 于 雨松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(0,100)] public int intValue = 0; }
如下图所示,这个属性的渲染就已经完全独立出来了。
本文固定链接: http://www.xuanyusong.com/archives/3680
转载请注明: 雨松MOMO 2015年10月21日 于 雨松MOMO程序研究院 发表
相关文章推荐
- Unity 四元数详解
- Create 2D game with Unity 3D - ( 1 )
- Unity实时反射——AngryBots示例项目地面实时反射效果剖析
- 项目记录30--unity-tolua框架 回顾之前Dotween导入
- Unity5的AssetBundle的一点使用心得
- [UnityUI]UGUI射线检测
- 4000 unity学习笔记—一部分粒子系统
- Unity3D 内存优化(一)对象池
- 如何使用unity中Resources文件
- Unity shader 入门之渲染管线
- 关于Unity程序在IOS和Android上显示内嵌网页的方式
- [Unity] 使用Profiler.BeginSample()定位性能热点
- unity 切圆角矩形 --shader编程
- Unity3D UDP 服务端例子
- Ubuntu 16.04 LTS 现在可将 Unity 启动器移动到桌面底部
- unity setActive( false )与移出摄像机范围的区别
- [UnityShader2]表面着色器实例(一)
- Unity3D插件开发
- Unity Shaders and Effects Cookbook (1-5)使用2D渐变纹理模拟BRDF(双向反射分布函数)
- java程序员图文并茂细说Unity中调用Android的接口 (转)