Untiy 只读属性实现

来源:互联网 发布:淘宝财富值怎么刷的 编辑:程序博客网 时间:2024/05/22 17:25

本文章由cartzhang编写,转载请注明出处。 所有权利保留。
文章链接:http://blog.csdn.net/cartzhang/article/details/53888588
作者:cartzhang


想在Untiy 中,在检视板上看到,但是有不希望别人在检视板上改动。

这时候就会有人说,怎么不使用检视板的debug模式呢?
这里写图片描述


我不想动手点击,就希望看到,灰色起来方便看属性值的变化。

下面的代码就十分有用了。

using UnityEngine;#if UNITY_EDITORusing UnityEditor;#endifpublic class ReadOnlyAttribute : PropertyAttribute{}#if UNITY_EDITOR[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]public class ReadOnlyDrawer : PropertyDrawer{    public override float GetPropertyHeight(SerializedProperty property,GUIContent label)    {        return EditorGUI.GetPropertyHeight(property, label, true);    }    public override void OnGUI(Rect position,SerializedProperty property,GUIContent label)    {        GUI.enabled = false;        EditorGUI.PropertyField(position, property, label, true);        GUI.enabled = true;    }}#endif


只需要代码写的时候

[ReadOnly]        public int numberTest = 100;

这里写图片描述


这样就可以了。


—————————THE———–END———
若有问题,请随时来联系!!
非常感谢!!

0 0
原创粉丝点击