Skip to content

Instantly share code, notes, and snippets.

@LotteMakesStuff
Created January 17, 2017 00:17
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save LotteMakesStuff/c0a3b404524be57574ffa5f8270268ea to your computer and use it in GitHub Desktop.
Save LotteMakesStuff/c0a3b404524be57574ffa5f8270268ea to your computer and use it in GitHub Desktop.
ReadOnly property drawer for Unity~ Add a [ReadOnly] attribute to a property to make it show up as read only in the inspector
// NOTE DONT put in an editor folder
using UnityEngine;
public class ReadOnlyAttribute : PropertyAttribute { }
// NOTE put in a Editor folder
using UnityEngine;
using UnityEditor;
using System.Collections;
[CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
public class ReadOnlyPropertyDrawer : PropertyDrawer
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label);
GUI.enabled = true;
}
}
@MartinCapousek
Copy link

Hi, if you add following method it will fix problems that I battled few days ago :) with "read-olny"
structs:

public override float GetPropertyHeight( SerializedProperty property, GUIContent label )
{
	return EditorGUI.GetPropertyHeight( property, label, true );
}

@theGameLearner
Copy link

I spent last 2 months searching for a working code that did not hamper my gameplay. Thank You for making my work easier and more comfortable.

@ZiadJ
Copy link

ZiadJ commented Oct 13, 2018

There's no need to clutter things up with additional files. Just adding this code to my existing Utils.cs file did the trick for me:

    [AttributeUsage(AttributeTargets.Field, Inherited = true)]
    public class ReadOnlyAttribute : PropertyAttribute { }
#if UNITY_EDITOR
    [UnityEditor.CustomPropertyDrawer(typeof(ReadOnlyAttribute))]
    public class ReadOnlyAttributeDrawer : UnityEditor.PropertyDrawer
    {
        public override void OnGUI(Rect rect, UnityEditor.SerializedProperty prop, GUIContent label)
        {
            bool wasEnabled = GUI.enabled;
            GUI.enabled = false;
            UnityEditor.EditorGUI.PropertyField(rect, prop);
            GUI.enabled = wasEnabled;
        }
    }
#endif

@Yolo-Jerome
Copy link

@ZiadJ

clutter things up with additional files

It is actually better/cleaner coding to have one file for each class instead of having multiple classes in one single file ;)

@Yolo-Jerome
Copy link

Unfortunately it doesn't fully work for Lists:

[ReadOnly] public List<int> test = new List<int>(){ 1,2,3};

2019-04-02_1217

The most "dangerous" value to change is till available.


And it especially doesn't work for nested elements:

2019-04-02_1220

where the elements are of type

[Serializable]
public class TestClass
{
    public int num;
    public string text;
}

[ReadOnly] public List<TestClass> test = new List<TestClass>(){
    new TestClass(),
    new TestClass{num = 2, text = "Hi"},
    new TestClass{num = 3, text = "asd"}
};

also if it is not in a list like

[ReadOnly] public TestClass test = new TestClass();

@VPavliashvili
Copy link

Paste this code into ReadOnlyPropertyDrawer and it will fix, worked in my case btw.

`

public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
    GUI.enabled = false;
    EditorGUI.PropertyField(position, property, label, true);
    GUI.enabled = true;
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
    return EditorGUI.GetPropertyHeight(property, label, true);
}

`

Here is the inspector after modification
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment