
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.

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! ?
Awesome stuff, good job 🙂
Doesn’t work with quaternions
Thank you!
Thank you!
Works good, buty can you make it to work with text area? thanks!
Works great. Directions could be clearer that the first file, the “ReadOnlyAttribute” class file doesn’t go in the Editor directory.
THANK you
Saved me! Thanks.
Awesome so useful thank you so much you saved me so much time!!!
This was really useful – thank you!
<3
Very helpful, thanks mate
Thanks!