TextusGames / UnitySerializedReferenceUI

The UI for Unity's SerealizedReference attribute. It allows to change the instance type of field right in editor.
MIT License
144 stars 22 forks source link

Inspector blank because of Unity Exception #9

Open MasterQuestMaster opened 3 years ago

MasterQuestMaster commented 3 years ago

I'm using Unity version 2020.3.11f1. I added the [SerializeReference] and [SerializeReferenceButton] (but same issue with Menu) to a list of objects in my ScriptableObject class. At first it works fine, but if you click away from the object and then try to display the inspector for that object again, it will display the object for a few miliseconds, and then the Inspector will turn blank.

Looks like this: image What it should look like: image

If I use "Reimport" on the affected Scriptable Object, it will work sometimes, like 1 in 5-10 tries. Reimporting the "SerializedReferenceUI" assets did not change anything. Usually, Unity does not show error messages for this, but I got it to show some error messages once.

Unfortunately, I didn't take a screenshot and I can't seem to reproduce the error messages currently, but it said something about Inspector and EditorWindow and it was an "Object reference not set to instance of an object" at "{some guid}:0". "Add Tooltip" function was bottom of the call stack, and "IsActiveEditControl" (or something similar) was the top.

Whyser commented 2 years ago

Hey @MasterQuestMaster did you find a fix for this? It's quite possibly related to my issue and is semi-caused by Unitys "new" ReordeableList (at least in my case).

You can try:

  1. Set the [NonReorderable] attribute on your List or
  2. Go into SerializeReferenceButtonAttributeDrawer.cs and change:
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        EditorGUI.BeginProperty(position, label, property);

        var labelPosition = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
        EditorGUI.LabelField(labelPosition, label);    

        var typeRestrictions = SerializedReferenceUIDefaultTypeRestrictions.GetAllBuiltInTypeRestrictions(fieldInfo);
        property.DrawSelectionButtonForManagedReference(position, typeRestrictions);

        EditorGUI.PropertyField(position, property, GUIContent.none, true);

        EditorGUI.EndProperty(); 
    } 

to:

    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        //EditorGUI.BeginProperty(position, label, property);

        var labelPosition = new Rect(position.x, position.y, position.width, EditorGUIUtility.singleLineHeight);
        EditorGUI.LabelField(labelPosition, label);    

        var typeRestrictions = SerializedReferenceUIDefaultTypeRestrictions.GetAllBuiltInTypeRestrictions(fieldInfo);
        property.DrawSelectionButtonForManagedReference(position, typeRestrictions);

        EditorGUI.PropertyField(position, property, GUIContent.none, true);

        //EditorGUI.EndProperty(); 
    } 

As you can see I comment out the EditorGUI.BeginProperty and EndProperty calls. I'm not very familiar at what exactly it does, but it seems to solve my issue. Maybe @TextusGames has a clue?

MasterQuestMaster commented 2 years ago

@Whyser No, I didn't find a fix for it. I ended up not using this package, and instead built a custom property drawer to emulate the functionality.