InspectorReadOnly - BluePixelDev/utilkit GitHub Wiki

InspectorReadOnlyAttribute is a Unity attribute that renders serialized fields as read-only in the Inspector. This prevents the user from modifying the value in the Editor while still making it visible.

Overview

Use this attribute to display values that should not be editable in the Unity Inspector, such as runtime data or internal references meant for debugging only.

Features

  • Renders serialized fields as disabled (grayed out)
  • Compatible with UI Toolkit (UIElements) inspectors
  • Minimal performance cost, editor-only

Usage Example

[InspectorReadOnly]
[SerializeField] private int readOnlyValue;

The field will be visible but disabled in the Unity Inspector.

Implementation Details

Class: InspectorReadOnlyAttribute

  • Inherits from PropertyAttribute
  • Marks fields to be drawn as read-only in the editor

Class: InspectorReadOnlyAttributeDrawer

  • Custom PropertyDrawer for InspectorReadOnlyAttribute
  • Overrides CreatePropertyGUI to wrap the property in a disabled container

Method

CreatePropertyGUI(SerializedProperty property)

  • Creates a VisualElement, disables interaction, and embeds the default property field
  • Only applies to UI Toolkit-based inspectors

Editor-Only Compilation

This drawer is wrapped in #if UNITY_EDITOR to ensure it's excluded from builds:

#if UNITY_EDITOR
// Drawer implementation
#endif