Couldn’t get scriptableobject sprites to draw consistently in a property drawer. The default EditorGUI.ObjectField when large enough to show the icon would randomly flicker the sprite and render it with a delay after clicking in the inspector panel.
In the end I changed the height of the ObjectField so the default icon is not drawn and use a GUIStyle to render the sprite as a background. Using this method I can mash the arrow key down through my scriptableobjects and the sprites show instantly with now weirdness in the inspector.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomPropertyDrawer(typeof(Sprite))]
public class SpriteDrawer : PropertyDrawer {
private static GUIStyle s_TempStyle = new GUIStyle();
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var ident = EditorGUI.indentLevel;
EditorGUI.indentLevel = 0;
Rect spriteRect;
//create object field for the sprite
spriteRect = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
property.objectReferenceValue = EditorGUI.ObjectField(spriteRect, property.name, property.objectReferenceValue, typeof(Sprite), false);
//if this is not a repain or the property is null exit now
if (Event.current.type != EventType.Repaint || property.objectReferenceValue == null)
return;
//draw a sprite
Sprite sp = property.objectReferenceValue as Sprite;
spriteRect.y += EditorGUIUtility.singleLineHeight+4;
spriteRect.width = 64;
spriteRect.height = 64;
s_TempStyle.normal.background = sp.texture;
s_TempStyle.Draw(spriteRect, GUIContent.none, false, false, false, false);
EditorGUI.indentLevel = ident;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return base.GetPropertyHeight(property, label) + 70f;
}
}
Quick workaround: disable multi editing by pasting the following code on top of the OnGUI (public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)) method:
if (property.serializedObject.isEditingMultipleObjects)
{
GUI.Label(position, "Sprite multiediting not supported");
return;
}