Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question [Editor] Generic field for a specific type without SerializedProperty

Discussion in 'UI Toolkit' started by Homoludens_Julien, Jun 27, 2023.

  1. Homoludens_Julien

    Homoludens_Julien

    Joined:
    Jun 24, 2021
    Posts:
    1
    I want to display a field for a type T but it's content and value is not linked to any kind of SerializedObject
    Basicaly, in anEditorWindow, i have a dropdown that allow the user to select a type (Option can be float, Vector3, vector2, color, or even stuff with custom drawer) and i want it to display a bunch of field for that type bellow. That means a bunch of of FloatField when float is selected, Vector3Field with it's Vector3 and even the custom PropertyDrawer when it's nessesary.

    Given a type T, is there a way to create the right visual element to edit that type ?

    The behavior is close to what SerializedField does except SerializeField required a SerializedProperty, which doesn't exist in my case.
    To be clear, i don't care about the binding, as i need to handle change directly with callback and the value in these field are not really "linked" to anything.

    The simple option would be to go with a switch statement that link a type to a specific field but that seems like a mess and might get super complicated to handle PropertyDrawer.

    I was hopping for something along the line of InspectorElement.FillDefaultInspector?

    Does that exist ?
     
  2. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    7,821
    No. It sounds like you want the equivalent to a property field. In the property field we also have a big switch statement that creates a different field based on the serialized property type.
     
  3. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    620
    What about the Properties UI Package?
    Haven't used it myself but it seems to do what you want. Inspectors for generic non-serialized classes.

    https://docs.unity.cn/Packages/com.unity.properties.ui@2.0/manual/index.html

    Alternatively you can look into the Code of "EasyButtons" on GitHub. The dev creates procedural ScriptableObjects which are then used to draw an inspector.

    Or yea, option 3 would be to have a switch statement which is the most straight-forward way to do it (which means it's easy to understand and fix if there are any errors - and also very customizable).