Version 2.1.0.0

Odin has a dedicated attribute overview with examples

ReadOnlyAttribute class

Namespace: Sirenix.OdinInspector
Assembly: Sirenix.OdinInspector.Attributes
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Module | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Constructor | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Event | AttributeTargets.Interface | AttributeTargets.Parameter | AttributeTargets.Delegate | AttributeTargets.ReturnValue | AttributeTargets.GenericParameter | AttributeTargets.All, AllowMultiple = false, Inherited = true)]
public sealed class ReadOnlyAttribute : Attribute, _Attribute

ReadOnly is used on any property, and disabled the property from being changed in the inspector.

Use this for when you want to see the value of a property in the inspector, but don't want it to be changed.

Inheritance
  • System.Object
  • System.Attribute
  • ReadOnlyAttribute
Remarks

note

This attribute only affects the inspector! Values can still be changed by script.

Example

The following example shows how a field can be displayed in the editor, but not be editable.

public class Health : MonoBehaviour
{
	public int MaxHealth;

	[ReadOnly]
	public int CurrentHealth;
}
Example

ReadOnly can also be combined with ShowInInspectorAttribute.

public class Health : MonoBehaviour
{
	public int MaxHealth;

	[ShowInInspector, ReadOnly]
	private int currentHealth;
}

Constructors

ReadOnlyAttribute()
public ReadOnlyAttribute()

This website is brand new and built with the Odin 2.1 beta in mind. This means you might discover some features such as attribute expressions that are only available in the beta.

You can download the 2.1 beta from here and read the patch-notes here.