Skip to content

Instantly share code, notes, and snippets.

@jringrose
Last active December 21, 2016 07:56
Show Gist options
  • Save jringrose/0f1d41f511c8baf272d8 to your computer and use it in GitHub Desktop.
Save jringrose/0f1d41f511c8baf272d8 to your computer and use it in GitHub Desktop.
Use this InspectorReadOnly attribute for public fields that you want to easily inspect in the editor without allowing modifications. InspectorReadOnlyDrawer needs to be inside an Editor folder.
using System;
using UnityEngine;
// Usage:
//
// [InspectorReadOnly]
// public float someValue = 5f;
//
public class InspectorReadOnly : PropertyAttribute {
}
using System;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(InspectorReadOnly))]
public class InspectorReadOnlyDrawer : PropertyDrawer {
override public float GetPropertyHeight(SerializedProperty property, GUIContent label){
return EditorGUI.GetPropertyHeight(property, label, true);
}
override public void OnGUI(Rect position, SerializedProperty property, GUIContent label){
GUI.enabled = false;
EditorGUI.PropertyField(position, property, label, true);
GUI.enabled = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment