美文网首页unity编辑器扩展
Unity 编辑器扩展七 Property Attributes

Unity 编辑器扩展七 Property Attributes

作者: 合肥黑 | 来源:发表于2021-12-11 08:09 被阅读0次

    参考
    Unity Editor 基础篇(七):Property Attributes

    一、示例
        [ReadOnly]
        public string myString = "Liction";
    

    我们可以对ReadOnly这种标签在面板上的显示做自定义,对比效果如下:


    image.png
    1.自定义标签

    在Scripts文件夹中创建两个C#脚本,分别命名为:“ReadOnlyAttribute.cs”和“TestPropertyAttr.cs”

    //TestPropertyAttr.cs
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class TestPropertyAttr : MonoBehaviour
    {
        [ReadOnly]
        public string myString = "Liction";
    
        [ReadOnly]
        public int myInt = 22;
    
        [ReadOnly]
        public Color myColor = Color.green;
    
        [ReadOnly]
        public Vector3 myVector = Vector3.one;
    
        [ReadOnly]
        public Rect myRect = new Rect(1, 1, 1, 1);
    }
    
    //ReadOnlyAttribute.cs
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class ReadOnlyAttribute : PropertyAttribute
    {
        public ReadOnlyAttribute()
        {
    
        }
    }
    

    继承PropertyAttribute这个类,就能用一个自定义的PropertyDrawer类去控制继承PropertyAttribute类的变量在Inspector面板中的显示。在IDE中,按下CTRL,再点击TestPropertyAttr 中的[ReadOnly]标签,会发现跳到了ReadOnlyAttribute.cs中。可以理解为,我们继承PropertyAttribute这个类,实现了自己定义一个标签。

    2.重绘

    在Editor文件夹中创建一个名为“ReadOnlyAttributeDrawer.cs”的脚本

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEditor;
    using System;
    
    [CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    public class ReadOnlyAttributeDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            //base.OnGUI(position, property, label);
            string value;
            switch (property.propertyType)
            {
                case SerializedPropertyType.Integer:
                    value = property.intValue.ToString();
                    break;
                case SerializedPropertyType.String:
                    value = property.stringValue;
                    break;
                case SerializedPropertyType.Color:
                    value = property.colorValue.ToString();
                    break;
                case SerializedPropertyType.Vector3:
                    value = property.vector3Value.ToString();
                    break;
                case SerializedPropertyType.Rect:
                    value = property.rectValue.ToString();
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }
            EditorGUI.LabelField(position, property.name + "\t\t:\t" + value);
        }
    
        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return base.GetPropertyHeight(property, label);
        }
    }
    
    3.带参数
    public class TestPropertyAttr : MonoBehaviour
    {
        [ReadOnly(1f, 0f, 0f)]
        public string myString = "Liction";
    
        [ReadOnly(1, 0, 0, 0.5f)]
        public int myInt = 22;
    
        [ReadOnly]
        public Color myColor = Color.green;
    
        [ReadOnly]
        public Vector3 myVector = Vector3.one;
    
        [ReadOnly]
        public Rect myRect = new Rect(1, 1, 1, 1);
    }
    
    //ReadOnlyAttributeDrawer.cs
    ...
            //EditorGUI.LabelField(position, property.name + "\t\t:\t" + value);
    
            ReadOnlyAttribute myAttribute = (ReadOnlyAttribute)attribute;
            Debug.Log(attribute.GetType().Name);
            GUI.color = myAttribute.textColor;
            Debug.Log(property.name + ":" + myAttribute.textColor);
            GUI.Label(position, property.displayName + "\t\t:\t" + value);
            //GUI.color = Color.white;
    
    image.png
    image.png

    MyString的颜色未生效

    相关文章

      网友评论

        本文标题:Unity 编辑器扩展七 Property Attributes

        本文链接:https://www.haomeiwen.com/subject/tidltrtx.html