Skip to content

Instantly share code, notes, and snippets.

@Lachee
Last active January 31, 2024 02:06
Show Gist options
  • Save Lachee/70285f7135a01157b70f1de4537a5333 to your computer and use it in GitHub Desktop.
Save Lachee/70285f7135a01157b70f1de4537a5333 to your computer and use it in GitHub Desktop.
Quickly open properties with a single button click
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(Object), true)]
public class QuickPropertyDrawer : PropertyDrawer
{
const float BUTTON_SIZE = 20f;
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
bool showButton = property.objectReferenceValue != null;
float buttonSize = showButton ? BUTTON_SIZE-2 : 0;
Rect propertyRect = new Rect(position.x, position.y, position.width - buttonSize, position.height);
EditorGUI.PropertyField(propertyRect, property, label);
Rect buttonRect = new Rect(propertyRect.xMax + 4, position.y + 1, (position.width - propertyRect.width), EditorGUIUtility.singleLineHeight + 1f);
if (showButton && GUI.Button(buttonRect, EditorGUIUtility.IconContent("_Popup"), EditorStyles.iconButton)) // remove showButton to make the button always visible
{
if (property.objectReferenceValue is Component component) // Opens the entire object instead of just the component (context awareness)
EditorUtility.OpenPropertyEditor(component.gameObject);
if (property.objectReferenceValue is Sprite sprite) // Opens the texture instead of just the sprite (not useful)
EditorUtility.OpenPropertyEditor(sprite.texture);
else // Default to just opening it regularly
EditorUtility.OpenPropertyEditor(property.objectReferenceValue);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment