Unity Inspector面板常用的屬性

在擴展Unity的時候,往往會用到一些屬性,這裏將常用的列一下。

1、屬性只讀;

#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 = "";

2、私有變量在 Inspector 顯示出來  [SerializeField]

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

效果如下:

3、爲屬性添加頭部說明 [HeaderAttribute]

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;
}

4、隱藏屬性 [HideInInspector]

using UnityEngine;
using System.Collections;

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

其它還有諸如 HelpURL 等,詳情可參考 官方幫忙文檔 https://docs.unity3d.com/ScriptReference/HeaderAttribute.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章