標籤:bottom serial ide only pre IV help ngui amp
在擴充Unity的時候,往往會用到一些屬性,這裡將常用的列一下。
1、屬性唯讀;
#if UNITY_EDITORusing UnityEditor;#endifusing 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
Unity Inspector面板常用的屬性