ReadOnly Attribute in Unity Editor

Time to build custom attribute for code and Unity Editor! Time to create a ReadOnly attribute!

Creating custom attributes in Unity is not a big thing. A rather pretty small one πŸ˜‰

It’s this one useful trick you want to have under your sleeves.

Let’s get right into it! πŸ› 

To create our ReadOnly attribute, we have to create our own PropertyAttribute.

using UnityEngine;

/// <summary>
/// Read Only attribute.
/// Attribute is use only to mark ReadOnly properties.
/// </summary>
public class ReadOnlyAttribute : PropertyAttribute { }

This class will be used only to mark properties in Editor as non-editable.

The next step will be to create a drawer for our attribute. You will have to create this class in some Editor folder. Don’t forget about that!

using UnityEngine;
using UnityEditor;

/// <summary>
/// This class contain custom drawer for ReadOnly attribute.
/// </summary>
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyDrawer : PropertyDrawer
{
    /// <summary>
    /// Unity method for drawing GUI in Editor
    /// </summary>
    /// <param name="position">Position.</param>
    /// <param name="property">Property.</param>
    /// <param name="label">Label.</param>
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        // Saving previous GUI enabled value
        var previousGUIState = GUI.enabled;

        // Disabling edit for property
        GUI.enabled = false;

        // Drawing Property
        EditorGUI.PropertyField(position, property, label);

        // Setting old GUI enabled value
        GUI.enabled = previousGUIState;
    }
}

Awesome! With these two classes, now we can use our ReadOnly attribute!

Example πŸ†

Let’s create some example script with our newly created attribute.

using UnityEngine;

/// <summary>
/// This class contain example of how to use "ReadOnly" attribute.
/// </summary>
public class ExampleScript : MonoBehaviour
{
    [ReadOnly]
    [SerializeField]
    private string textNonEditable = "You can't edit this text.";
    [SerializeField]
    private string textEditable = "But you definitely can edit this one.";

    [Space]

    [ReadOnly]
    [SerializeField]
    protected int numNonEditable = 23;
    [SerializeField]
    protected int numEditable = 12;

    [Space]

    [ReadOnly]
    public Vector2 vectorNonEditable = new Vector2(25, 13);
    public Vector2 vectorEditable = new Vector2(7, 3);
}

Now, we just need to open the Unity Editor and attach the script to a game object.

Read Only fields in Unity Inspector

See? I hope it will be helpful for you. πŸ€“

If you find this useful, let me know in the comment section below!

And if you are interested in getting emails when I release new posts sign up for the newsletter!

You can also check the whole project at my public repository. πŸ”—

See you next time! πŸ€“

5 3 votes
Article Rating
Subscribe
Notify of
guest
3 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jose
jose
3 months ago

Awesome stuff, good job πŸ™‚

Robert
Robert
1 month ago

Doesn’t work with quaternions

James
James
26 days ago

Thank you!