Skip to content

Instantly share code, notes, and snippets.

@darktable
Created June 28, 2019 03:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darktable/7d0198cd183ab44bb68e1f86baf837f2 to your computer and use it in GitHub Desktop.
Save darktable/7d0198cd183ab44bb68e1f86baf837f2 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(Sprite))]
public class SpritePropertyDrawer : PropertyDrawer
{
public override float GetPropertyHeight(SerializedProperty property, GUIContent labelN)
{
if (property.objectReferenceValue != null)
{
return _texSize;
}
else
{
return base.GetPropertyHeight(property, labelN);
}
}
private const float _texSize = 70;
public override void OnGUI(Rect position, SerializedProperty prop, GUIContent label)
{
EditorGUI.BeginProperty(position, label, prop);
if (prop.objectReferenceValue != null)
{
position.width = EditorGUIUtility.labelWidth;
GUI.Label(position, prop.displayName);
position.x += position.width;
position.width = _texSize;
position.height = _texSize;
prop.objectReferenceValue = EditorGUI.ObjectField(position, prop.objectReferenceValue, typeof(Sprite), false);
}
else
{
EditorGUI.PropertyField(position, prop, true);
}
EditorGUI.EndProperty();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment