Search Unity

Question Issues with Inheritance in ScriptableObject Editors

Discussion in 'Scripting' started by errur, Apr 28, 2021.

  1. errur

    errur

    Joined:
    Apr 15, 2017
    Posts:
    5
    I've noticed some seemingly weird behaviour relating to custom editors that I can't really make sense of.

    I have an Editor Script toolset that includes a Component Copier, which in turn includes various copier classes.
    Each copier has a SettingsContainer ScriptableObject inside of it, which inherits from SettingsContainerBase.
    Each settings container has it's own custom Editor that it uses to draw the settings interface.

    Every time I select an object while the Inspector is open and mouse over my EditorWindow, the inherited fields from the SettingsContainerBase class seem to briefly disappear inside the Editor.
    This only happens if I select a new object and if the inspector is open and in view.

    Here's a short video showing what I mean


    In the example "Remove All Before Copying" and "Create Game Objects" are fields inherited from BaseSettingsContainer.

    Is there a limit on how many editors can be active at one time? Are they getting garbage collected?
    What's going on?

    Here's some of my code:

    ComponentCopierBase.cs
    Code (CSharp):
    1. public abstract class ComponentCopierBase : IComponentCopier
    2. {
    3.     public ComponentCopierBase()
    4.     {
    5.         Settings = this.GetOrCreateSettingsContainer();
    6.     }
    7.  
    8.     public void DrawUI(params GUILayoutOption[] options)
    9.     {
    10.         //Draw settings here
    11.         if(Settings == null || !UIDefs.ExpandSettings)
    12.             return;
    13.  
    14.         UIHelpers.DrawIndented(EditorGUI.indentLevel + 1, () =>
    15.         {
    16.             Settings?.DrawUI();
    17.         });
    18.     }
    19. }
    CopierSettingsContainerBase.cs
    Code (CSharp):
    1. public abstract class CopierSettingsContainerBase : SettingsContainerBase
    2. {
    3.     protected virtual bool ShowRemoveAll { get; } = true;
    4.     protected virtual bool ShowCreateGameObjects { get; } = true;
    5.  
    6.     [ConditionalHide(nameof(ShowRemoveAll))]
    7.     public bool removeAllBeforeCopying = false;
    8.  
    9.     [ConditionalHide(nameof(ShowCreateGameObjects))]
    10.     public bool createGameObjects = false;
    11.  
    12.     // This needs to be here to put a space before the settings of the object that inherits us
    13.     [ConditionalHide(nameof(ShowCreateGameObjects), nameof(ShowRemoveAll))]
    14.     [SerializeField]UISpacer _spacer;
    15.  
    16.     public override void DrawUI()
    17.     {
    18.         base.DrawUI();
    19.     }
    20. }
    SettingsContainerBase.cs
    Code (CSharp):
    1. public class SettingsContainerBase : ScriptableObject, ISettingsContainer
    2. {
    3.     public void Awake()
    4.     {
    5.         Editor.OnInspectorGUINoScriptField();   //Try to draw so it initializes and opens instantly later
    6.     }  
    7.  
    8.     public virtual void DrawUI()
    9.     {
    10.         Editor?.OnInspectorGUI();
    11.     }      
    12.  
    13.     protected Editor _editor;
    14. }
    Full code can be found here https://github.com/rurre/PumkinsAvatarTools/tree/dev-overhaul/