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

相關文章
相關標籤/搜索