Class ReadOnlyAttribute
Makes a serialized field permanently non-editable in the Unity Inspector, or optionally non-editable only during Play mode.
Inherited Members
Namespace: Scylla.Core.Attributes
Assembly: ScyllaCore.dll
Syntax
[AttributeUsage(AttributeTargets.Field)]
public sealed class ReadOnlyAttribute : PropertyAttribute
Remarks
The decorated field is displayed in a disabled (grayed-out) GUI state by the custom property drawer, preventing any user interaction. The field is still serialized normally and can be written to from code; only the Inspector presentation is affected.
This attribute is useful for:
- Exposing computed or framework-assigned identifiers (IDs, GUIDs) that must not be changed by hand.
- Showing runtime state (health, velocity, timers) for debugging purposes while preventing accidental mid-session edits.
- Locking configuration fields that are safe to edit in Edit mode but must remain stable at runtime.
For fields that should be hidden (not merely disabled) under certain conditions, consider HideIfAttribute or DisableIfAttribute instead.
public class Enemy : MonoBehaviour
{
/* Always read-only - useful for framework-assigned IDs and computed values */
[ReadOnly]
[SerializeField] private string _instanceID;
/* Editable in Edit mode, read-only during Play mode - useful for debugging live values */
[ReadOnly(RuntimeOnly = true)]
[SerializeField] private float _currentHealth;
/* Read-only runtime display of an internal counter */
[ReadOnly(RuntimeOnly = true)]
[SerializeField] private int _attackCount;
}
Properties
RuntimeOnly
Gets or sets whether the field is read-only exclusively during Play mode, or always read-only regardless of the Editor state.
Declaration
public bool RuntimeOnly { get; set; }
Property Value
| Type | Description |
|---|---|
| bool |
|
Remarks
When false (the default), the field is always rendered in a disabled
state - both in Edit mode and in Play mode - so it can never be modified
through the Inspector.
When true, the field is editable normally in Edit mode (allowing
designers to configure its initial value) but becomes non-editable as soon
as the Editor enters Play mode. This is evaluated by checking
Application.isPlaying in the property drawer.