Unity Inspector面板经常使用的属性

在扩展Unity的时候,每每会用到一些属性,这里将经常使用的列一下。html

一、属性只读;ide

#if UNITY_EDITOR
using UnityEditor;
#endif

using UnityEngine;


public 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 string PLUGIN = "";

 

二、私有变量在 Inspector 显示出来  [SerializeField]spa

[ReadOnly]
[SerializeField]
 private string ABC = "abc";

 

效果以下:3d

image

 

三、为属性添加头部说明 [HeaderAttribute]code

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    [Header("Health Settings")]
    public int health = 0;
    public int maxHealth = 100;
    [Header("Shield Settings")]
    public int shield = 0;
    public int maxShield = 0;
}

 

 

四、隐藏属性 [HideInInspector]htm

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour {
    [HideInInspector]
    public int p = 5;
}

 

其它还有诸如 HelpURL 等,详情可参考 官方帮忙文档 https://docs.unity3d.com/ScriptReference/HeaderAttribute.htmlblog

相关文章
相关标签/搜索