Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Ways of modifying "auto-populated" PropertyField after CreateInspectorGUI()

Discussion in 'UI Toolkit' started by Buretto, Mar 5, 2020.

  1. Buretto

    Buretto

    Joined:
    Mar 23, 2015
    Posts:
    49
    For a custom inspector, I'm trying to figure out the best/simplest way to modify the elements right after
    CreateInspectorGUI()
    . The reason(s) for this is that
    CreateInspectorGUI()
    controls the bindings of the property fields I have set in uxml file. So for example, it is convenient that I can assign the
    bindingPath 
    of a property field to a serializeable class or struct, as the binding system will automatically populate it with the contained values. But if I wanted one of the contained value's enabled/disabled state to be controlled by a toggle/bool value, I can't do this in
    CreateInspectorGUI()
    as the property fields haven't been assigned yet (AFAICT the PropertyFields are created when it returns). It would be convenient if I could modify it by overriding a method such as
    AfterCreateInspectorGUI()
    , or something like that. Is there a proper way to do that?

    Right now I am adding all contained values as PropertyFields by hand within
    CreateInspectorGUI()
    , so I can set the state as they are added to the root; but this is not ideal as I may have, say, 15 fields in the class/struct and only need set the state of 1.
     
  2. uDamian

    uDamian

    Unity Technologies

    Joined:
    Dec 11, 2017
    Posts:
    1,231
    You can use the
    GeometryChangeEvent
    on your custom Editor's root element (the element returned from
    CreateInspectorGUI()
    ). That will always be called the first time the elements are layed out (and all following times when their size changes at all). I would recommend immediately unregistering from GeometryChangeEvent once you get it to avoid performance problems down the road. But at this point, all the UI should be created and if it has to be re-created, you can be sure this event will trigger again so you can redo your own custom links between elements.
     
    Buretto likes this.