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! ?

4.9 14 votes
Article Rating
Subscribe
Notify of
guest
13 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
jose
jose
4 years ago

Awesome stuff, good job 🙂

Robert
Robert
4 years ago

Doesn’t work with quaternions

James
James
4 years ago

Thank you!

Kanin Temsrisuk
Kanin Temsrisuk
4 years ago

Thank you!

Jose
Jose
4 years ago

Works good, buty can you make it to work with text area? thanks!

Paul
Paul
3 years ago

Works great. Directions could be clearer that the first file, the “ReadOnlyAttribute” class file doesn’t go in the Editor directory.

daniel Zastrow
daniel Zastrow
Reply to  Paul
3 years ago

THANK you

Bent
Bent
Reply to  Paul
3 years ago

Saved me! Thanks.

Lewis
Lewis
3 years ago

Awesome so useful thank you so much you saved me so much time!!!

David MB
David MB
2 years ago

This was really useful – thank you!

Haoyuan
Haoyuan
2 years ago

Very helpful, thanks mate

Mike
Mike
1 year ago

Thanks!

13
0
Would love your thoughts, please comment.x
()
x