Search Unity

Best way to manage inheritance

Discussion in 'UI Toolkit' started by Sylmerria, Mar 8, 2019.

  1. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hi UI team,

    I have a problem which is maybe solved with the new UI. if not, I love to have feedback or work around.

    I have a class ActionManager which has an array of Action.
    Action is an base class.
    I have many real action ( ActionA, ActionB, ActionC, etc ) which are stock in the array Action from ActionManager.

    Currently, when my UI editor drawer comes to ActionManager, I switch from SerializeProperty world to object world.
    I take all actions and create for each a new serialiazeobject and a drawer and render it.

    It's not beauty and not safe but that works.

    So what do you think of that ?

    Thanks
     
  2. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    375
    Hi Sylmerria,

    Like IMGUI, UIElements has the required facilities to draw properties of a SerializedObject, so going either way should not be a problem on that front. Here's two ideas that could help you deal with your class hierarchy:
    a) Inherit your actions from ScriptableObject and draw the UI with EditorGUILayout.ObjectField
    b) Use a visitor pattern instead of instantiating multiple different drawers.

    Let me know if this helps
     
  3. Sylmerria

    Sylmerria

    Joined:
    Jul 2, 2012
    Posts:
    369
    Hi alexandre

    I don't know visitor pattern seems interesting. But it forces me to replace the Action[] by ActionA[], ActionB[] and order in Action[] is very important so I don't think I can do that :(

    Thanks for your answer
     
  4. AlexandreT-unity

    AlexandreT-unity

    Unity Technologies

    Joined:
    Feb 1, 2018
    Posts:
    375
    It should not force you to split your array (although there could be performance benefits by doing so). Basically, your visitor would call an "Accept" method defined in the Action base class, and then in the specialized actions, you can override the method and call Visit[A, B or C] on the visitor, which would then perform the required drawing for that type of action.
     
    Lu4e likes this.