I have created a simple test scriptable object (edit: tests show same behavior with custom serializable objects, that are NOT ScriptableObject derived):
public class TestScriptableObj : ScriptableObject {
public string title;
public Texture2D insignia;
}
And used it in a simple MonoBehavior:
public class Monotest : MonoBehaviour {
public TestScriptableObj tester;
void Reset () {
tester = TestScriptableObj.CreateInstance<TestScriptableObj>();
tester.title = "testTitle2";
}
}
I also created a property drawer for it. In order to make the insignia show the texture, like on a material inspector, I was unable to use EditorGUI.PropertyField, and needed to use EditorGUI.ObjectField, and specify the type as Texture2D.
[CustomPropertyDrawer(typeof(TestScriptableObj))]
public class TestScriptableObjectPropertyDrawer :PropertyDrawer{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
if (property.objectReferenceValue == null)
{
EditorGUI.HelpBox(position, "Null object reference ", MessageType.Error);
return;
}
EditorGUI.BeginProperty(position, label, property);
//since TestScriptableObj is a ScriptableObject derivation (a Unity class)- we need to do this backasswards stuff, and get the object from the property: FindPropertyRelative will return null
SerializedObject propObj = new SerializedObject(property.objectReferenceValue);
SerializedProperty title = propObj.FindProperty("title");
SerializedProperty insignia = propObj.FindProperty("insignia");
position.height = EditorGUIUtility.singleLineHeight; position.width -= 80;
EditorGUI.PropertyField(position, title, label);
position.x = position.xMax; position.width = 80; position.height = 80;
insignia.objectReferenceValue = EditorGUI.ObjectField(position, GUIContent.none, insignia.objectReferenceValue, typeof(Texture2D), true);
propObj.ApplyModifiedProperties();
EditorGUI.EndProperty();
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return 80f;
}
}
This works: kind-of. The displayed texture consistently fails to update properly. When it does eventual update, the time it takes to do so, is inconsistent, and seems to depend on what I do with the mouse. Before it updates, there is NO texture shown- just the field background.
What is causing the delayed update? Or, does nobody else experience these delays?
Edit/upadte:
NO!! Not this solution again!!
New main question is: What the heck? Surely users of my ScriptableObject asset are not required to specify a do-nothing-custom-editor for their classes that use it, do they?
Adding the following do-nothing script resolves the issue:
using UnityEditor;
[CustomEditor(typeof(Monotest))]
public class MonoTestEditor : Editor {}