Skip to content

Instantly share code, notes, and snippets.

@tjukic
Created June 25, 2019 10:44
Show Gist options
  • Save tjukic/7fa57c2771fc86b04fa3f86dff672618 to your computer and use it in GitHub Desktop.
Save tjukic/7fa57c2771fc86b04fa3f86dff672618 to your computer and use it in GitHub Desktop.
sprite preview
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