Skip to content

Instantly share code, notes, and snippets.

@LotteMakesStuff
Created January 17, 2017 00:17
Show Gist options
  • 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;
}
}
@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