TylerTemp / SaintsField

A Unity Inspector extension tool focusing on script fields inspector enhancement
MIT License
154 stars 9 forks source link

`[SaintsRow]` should supports `SerializeReference` #80

Open alaa13212 opened 2 weeks ago

alaa13212 commented 2 weeks ago

I'm trying to use [SerializeReference] with [ReferencePicker] to render inspector for something similar to this

public interface IRefInterface { }

[Serializable]
public class RefClass: IRefInterface
{
    [LayoutGroup("Base", ELayout.Horizontal)]
    public int a;
    public int b;
    public int c;
}

// Usage
[SerializeReference, ReferencePicker]
public IRefInterface item;

Notice how I need a Layout in the subclass. This doesn't work unless I use SaintsRow.

[SerializeReference, ReferencePicker, SaintsRow]
public IRefInterface item;

Now layout renders correctly. Only if the reference is assigned. When the reference is null SaintsRow drawer throw the following exception:

NullReferenceException: Object reference not set to an instance of an object
SaintsField.Editor.Utils.ReflectUtils.GetSelfAndBaseTypes (System.Object target) (at ./Library/PackageCache/today.comes.saintsfield@9d9f91f96f/Editor/Utils/ReflectUtils.cs:14)
SaintsField.Editor.SaintsEditor.GetRenderers (System.Collections.Generic.IReadOnlyDictionary`2[TKey,TValue] serializedPropertyDict, UnityEditor.SerializedObject serializedObject, System.Object target) (at ./Library/PackageCache/today.comes.saintsfield@9d9f91f96f/Editor/SaintsEditor.cs:241)
SaintsField.Editor.Playa.SaintsRowAttributeDrawer.CreatePropertyGUI (UnityEditor.SerializedProperty property) (at ./Library/PackageCache/today.comes.saintsfield@9d9f91f96f/Editor/Playa/SaintsRowAttributeDrawer.cs:179)
UnityEditor.UIElements.PropertyField.Reset (UnityEditor.SerializedProperty newProperty) (at <50e19b0ed93a4256b2e454b4acec2d48>:0)
UnityEditor.UIElements.PropertyField.Reset (UnityEditor.UIElements.SerializedPropertyBindEvent evt) (at <50e19b0ed93a4256b2e454b4acec2d48>:0)
UnityEditor.UIElements.PropertyField.ExecuteDefaultActionAtTarget (UnityEngine.UIElements.EventBase evt) (at <50e19b0ed93a4256b2e454b4acec2d48>:0)
UnityEngine.UIElements.CallbackEventHandler.HandleEvent (UnityEngine.UIElements.EventBase evt) (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEngine.UIElements.CallbackEventHandler.HandleEventAtCurrentTargetAndPhase (UnityEngine.UIElements.EventBase evt) (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetPhase (UnityEngine.UIElements.EventBase evt) (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEngine.UIElements.CallbackEventHandler.HandleEventAtTargetAndDefaultPhase (UnityEngine.UIElements.EventBase evt) (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.SendBindingEvent[TEventType] (TEventType evt, UnityEngine.UIElements.VisualElement target) (at <28a6e5fe38a74a678a32cfa433f4f24d>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindPropertyRelative (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedProperty parentProperty) (at <28a6e5fe38a74a678a32cfa433f4f24d>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.BindTree (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <28a6e5fe38a74a678a32cfa433f4f24d>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.ContinueBinding (UnityEngine.UIElements.VisualElement element, UnityEditor.SerializedProperty parentProperty) (at <28a6e5fe38a74a678a32cfa433f4f24d>:0)
UnityEditor.UIElements.Bindings.SerializedObjectBindingContext.Bind (UnityEngine.UIElements.VisualElement element) (at <28a6e5fe38a74a678a32cfa433f4f24d>:0)
UnityEditor.UIElements.Bindings.DefaultSerializedObjectBindingImplementation.BindProperty (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedObject obj) (at <28a6e5fe38a74a678a32cfa433f4f24d>:0)
UnityEditor.UIElements.BindingExtensions.BindProperty (UnityEngine.UIElements.IBindable field, UnityEditor.SerializedObject obj) (at <50e19b0ed93a4256b2e454b4acec2d48>:0)
SaintsField.Editor.Core.SaintsPropertyDrawer.OnAwakeUiToolKitInternal (UnityEditor.SerializedProperty property, UnityEngine.UIElements.VisualElement containerElement, System.Object parent, System.Collections.Generic.IReadOnlyList`1[T] saintsPropertyDrawers) (at ./Library/PackageCache/today.comes.saintsfield@9d9f91f96f/Editor/Core/SaintsPropertyDrawer.cs:1914)
SaintsField.Editor.Core.SaintsPropertyDrawer+<>c__DisplayClass38_0.<CreatePropertyGUI>b__2 () (at ./Library/PackageCache/today.comes.saintsfield@9d9f91f96f/Editor/Core/SaintsPropertyDrawer.cs:985)
UnityEngine.UIElements.VisualElement+SimpleScheduledItem.PerformTimerUpdate (UnityEngine.UIElements.TimerState state) (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEngine.UIElements.TimerEventScheduler.UpdateScheduledEvents () (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEngine.UIElements.UIElementsUtility.UnityEngine.UIElements.IUIElementsUtility.UpdateSchedulers () (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEngine.UIElements.UIEventRegistration.UpdateSchedulers () (at <7ec34a9fe0b043f08800d3612cbf36c3>:0)
UnityEditor.RetainedMode.UpdateSchedulers () (at <28a6e5fe38a74a678a32cfa433f4f24d>:0)
TylerTemp commented 2 weeks ago

SaintsRow does not work with SerializeReference because it's designed to work with serialized field/property. SerializeReference is internally draw by Unity (which is not the same process as serialized field/property) which does not expose drawing API for extending.

I'll see if it's possible to let SaintsRow support it.