Tag Archives: Unity Inspector

Common attributes of unity inspector panel

When expanding unity, some attributes are often used. Here are the common ones

1. The attribute is read-only

#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. Private variables are displayed in inspector [serializefield]

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

The results are as follows

3. Add a header description [headerattribute] to an attribute

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

412289;38544;34255;*23646;246151;HideInInspector]

using UnityEngine;
using System.Collections;

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

Others, such as help URL, can refer to the official help document for details https://docs.unity3d.com/ScriptReference/HeaderAttribute.html