Unity ScriptableObject自定义属性显示
1. 继承Editor,重写OnInspectorGUI方法
需求
将TestClass中intData属性和stringData按指定格式显示。
实现
定义一个测试类TestClass,一个可序列化类DataClass
[CreateAssetMenu]
public class TestClass : ScriptableObject
{
[Range(, )]
public int intData;
public string stringData;
public List<DataClass> dataList;
}
[System.Serializable]
public class DataClass
{
[Range(, )]
public int id;
public Vector3 position;
public List<int> list;
}
//指定类型
[CustomEditor(typeof(TestClass))]
public class TestClassEditor : Editor
{
SerializedProperty intField;
SerializedProperty stringField;
void OnEnable()
{
intField = serializedObject.FindProperty("intData");
stringField = serializedObject.FindProperty("stringData");
}
public override void OnInspectorGUI()
{
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update();
EditorGUILayout.IntSlider(intField, , , new GUIContent("initData"));
EditorGUILayout.BeginHorizontal();
EditorGUILayout.PropertyField(stringField);
if(GUILayout.Button("Select"))
{
stringField.stringValue = EditorUtility.OpenFilePanel("", Application.dataPath, "");
}
EditorGUILayout.EndHorizontal();
// Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
//需要在OnInspectorGUI之前修改属性,否则无法修改值
serializedObject.ApplyModifiedProperties();
base.OnInspectorGUI();
}
}
Editor嵌套
通过Edtiro.CreateEditor可实现Editor的嵌套。
创建一个类TestClass2,它包含一个TestClass的属性。
[CreateAssetMenu]
public class TestClass2 : ScriptableObject
{
public TestClass data;
}
创建一个Test2Class的asset。它的Inspector面板的默认显示:
它并没有把TestClass的属性显示出来,如果要查看TestClass的属性,必须双击,跳到相应界面,但这样有看不到TestClass2的属性。
如果想在Test2Class的Inspector面板中直接看到并且可以修改TestClass的属性,可以重写TestClass2的Editor,并在其中嵌套TestClass的Editor。
[CustomEditor(typeof(TestClass2))]
public class TestClass2Editor : Editor
{
Editor cacheEditor;
public override void OnInspectorGUI()
{
// Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
serializedObject.Update();
//显示TestClass2的默认UI
base.OnInspectorGUI();
GUILayout.Space();
var data = ( (TestClass2)target ).data;
if(data != null)
{
//创建TestClass的Editor
if (cacheEditor == null)
cacheEditor = Editor.CreateEditor(data);
GUILayout.Label("this is TestClass2");
cacheEditor.OnInspectorGUI();
}
}
}
这样就可以直接在TestClass2的面板中直接查看和编辑TestClass的属性。
2. 使用PropertyDrawer
如果想修改某种特定类型的显示,使用继承Editor的方式就会变得很麻烦,因为所有使用特定类型的asset都需要去实现一个自定义的Editor,效率非常低。这种情况就可以通过继承PropertyDrawer的方式,对指定类型的属性,进行统一显示。
需求
为Inspector面板中的所有string属性添加一个选择文件按钮,选中文件的路径直接赋值给该变量。
实现
[CustomPropertyDrawer(typeof(string))]
public class StringPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
Rect btnRect = new Rect(position);
position.width -= ;
btnRect.x += btnRect.width - ;
btnRect.width = ;
EditorGUI.BeginProperty(position, label, property);
EditorGUI.PropertyField(position, property, true);
if (GUI.Button(btnRect, "select"))
{
string path = property.stringValue;
string selectStr = EditorUtility.OpenFilePanel("选择文件", path, "");
if (!string.IsNullOrEmpty(selectStr))
{
property.stringValue = selectStr;
}
}
EditorGUI.EndProperty();
}
}
加了一个PropertyDrawer之后,Inspector面板中的所有string变量都会额外添加一个Select按钮。
注意事项
- PropertyDrawer只对可序列化的类有效,非可序列化的类没法在Inspector面板中显示。
- OnGUI方法里只能使用GUI相关方法,不能使用Layout相关方法。
- PropertyDrawer对应类型的所有属性的显示方式都会修改,例如创建一个带string属性的MonoBehaviour:
3. 使用PropertyAttribute
如果想要修改部分类的指定类型的属性的显示,直接使用PropertyDrawer就无法满足条件,这时可以结合PropertyAttribute和PropertyAttribute来实现需求。
需求
为部分指定类的int或float属性的显示添加滑动条,滑动条的上下限可根据类和属性自行设置。
实现
public class RangeAttribute : PropertyAttribute
{
public float min;
public float max;
public RangeAttribute(float min, float max)
{
this.min = min;
this.max = max;
}
}
[CustomPropertyDrawer(typeof(RangeAttribute))]
public class RangeDrawer : PropertyDrawer
{
// Draw the property inside the given rect
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
// First get the attribute since it contains the range for the slider
RangeAttribute range = attribute as RangeAttribute;
// Now draw the property as a Slider or an IntSlider based on whether it's a float or integer.
if (property.propertyType == SerializedPropertyType.Float)
EditorGUI.Slider(position, property, range.min, range.max, label);
else if (property.propertyType == SerializedPropertyType.Integer)
EditorGUI.IntSlider(position, property, (int)range.min, (int)range.max, label);
else
EditorGUI.LabelField(position, label.text, "Use Range with float or int.");
}
}
修改TestClass和DataClass
[CreateAssetMenu]
public class TestClass : ScriptableObject
{
[Range(, )]
public int intData;
public string stringData;
public List<DataClass> dataList;
}
[System.Serializable]
public class DataClass
{
[Range(, )]
public int id;
public Vector3 position;
public List<int> list;
}
其他
- 需要修改显示的类都需要满足Unity的序列化规则
- 这几种显示方式对Serializable Class都可以使用,并不需要一定是ScriptableObject。只是在编辑器下,ScriptableObject来保存临时数据比较常用,所以使用ScriptableObject来做例子。
转载请注明出处:http://www.cnblogs.com/chiguozi/p/6873050.html
Unity ScriptableObject自定义属性显示的更多相关文章
- Draw Call(Unity 5中显示为SetPass calls
Draw Call(Unity 5中显示为SetPass calls
- 自定义ScriptableObject属性显示
自定义ScriptableObject属性显示的三种方式 1. 继承Editor,重写OnInspectorGUI方法 Editor官方文档 需求 将TestClass中intData属性和strin ...
- Unity ScriptableObject的使用
ScriptableObject主要实现对象序列化的保存,因为是Unity自己的序列化,所以比xml,json序列化方便很多,但相对可控性也比较差 1.Editor下写入和读取测试: using Un ...
- Ubuntu14.04下Unity桌面托盘图标显示问题
本来想丰富一下功能,遂开始安装大开眼界:Ubuntu下10个厉害的Indicator小程序这里的Indicator小程序. 很不幸,在安装到indicator-multiload的时候,准备注销看一下 ...
- Unity 之 图片显示的真实大小
图片放入Unity中自身的属性 在做帽子游戏的时候,看到这么一段代码 //获取保龄球的自身宽度 float ballWidth=ball.GetComponent<Renderer>(). ...
- 【Unity/Kinect】显示Kinect摄像头内容,屏幕显示环境背景及人体投影
最近学习用Unity做些体感小游戏,使用Kinect的Unity插件,结合一些官方Demo学习(网上资源用Unity做的较少,蛋疼).插件及其Demo就在Unity商店里搜Kinect即可找到,其中下 ...
- Unity项目中显示项目的FPS
using UnityEngine; using System.Collections; public class ShowFpsOnGUI : MonoBehaviour { public floa ...
- Unity 3D 无法显示中文的解决方法
大家开始用unity3D时想必都会遇到一个问题,使用中文时会乱码.这是由于编码方式不同导致的,具体解决方法如下: 程序写代码什么的最好下个像Notepad++类似的工具,这里使用Notepad++修改 ...
- unity 分数的显示
通常 在完成 条件之后再增加分数 所以 一开始先增加 public int 得到分数; public Text 分数ui; 在完成条件后增加 得到分数++; 分数ui.text = 得到分数.ToSt ...
随机推荐
- Python 基于request库的get,post,delete,封装
# coding=utf-8 import json import requests class TestApi(object): """ /* @param: @ses ...
- 阅读Deep Packet Inspection based Application-Aware Traffic Control for Software Defined Networks
Deep Packet Inspection based Application-Aware Traffic Control for Software Defined Networks Globlec ...
- 闲话缓存:ZFS 读缓存深入研究-ARC(一)
在Solaris ZFS 中实现的ARC(Adjustable Replacement Cache)读缓存淘汰算法真是很有意义的一块软件代码.它是基于IBM的Megiddo和Modha提出的ARC(A ...
- 部分用户间接性访问不了linux服务器解决方法
linux的/etc/sysctl.conf中应设置 net.ipv4.tcp_tw_reuse = net.ipv4.tcp_tw_recycle = 参考文章: https://ieevee.co ...
- 【原创】如何设置Virtual Box虚拟机CentOS7为静态IP地址
如何设置Virtual Box虚拟机CentOS7为静态IP地址 最近要搭建一个Kubernetes集群,需要设置虚拟机为静态IP地址不变.翻了一些资料,参差不齐,有些也比较过时了.自己实测总结了一下 ...
- javascript编写的一个完整全方位轮播图效果
1 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&q ...
- webpack4+Vue搭建自己的Vue-cli
前言 最近在看webpack4,深感知识浅薄,这两天也一直在思考cli的配置,借助一些别人的实践,尝试自己搭建vue的项目,这里使用webpack4版本,之前我在网上查找别人的vue项目搭建,但是都是 ...
- php设置文件类型content-type
在PHP中可以通过header函数来发送头信息,还可以设置文件的content-type,下面整理了一些常见文件类型对于的content-type值. //date 2015-06-22//定义编码h ...
- Python中各种进制之间的转化
1.十进制转化为其它进制 (1)bin(x):十进制转化为二进制 [实例1] x=bin(20) # x的值为字符串'0b10100' (2)oct(x):十进制转化为八进制 [实例2] x=oc ...
- python 将歌词解析封装成类,要求:提供一个方法(根据时间返回歌词) - 提示:封装两个类:歌词类、歌词管理类
自己写的 有更好方案的大佬可以讨论一下 import bisectclass Lrc(): def __init__(self, sec, lrc): self.sec = sec self.lrc ...