Search Unity

RegisterCallback for ChangeEvent - Dealing with enums

Discussion in 'UIElements' started by OtoriScorp, Jun 10, 2019.

  1. OtoriScorp

    OtoriScorp

    Joined:
    Mar 9, 2016
    Posts:
    28
    Hi all, I am hoping someone can help me out with registering a callback for when an enum field is changed.

    I have tried pretty much all listed control types listed on the UIElements manual with no luck, even tried RegisterCallback<ChangeEvent<EnumField>> and RegisterCallback<ChangeEvent<MyEnumType>>

    I've managed to capture all my other controls but this one has me beat!
     
  2. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    101
    Hello,

    That would be
    ChangeEvent<Enum>
    .

    One hint that the API is give you is the
    RegisterValueChangedCallback()
    extension method if the
    INotifyPropertyChange<T> i
    nterface. This should always give an indication of the correct type T that acts as the underlying value of controls.

    Hope this helps.
     
  3. OtoriScorp

    OtoriScorp

    Joined:
    Mar 9, 2016
    Posts:
    28
    Hi Antoine, thanks for the reply :)
    I had also tried using ChangeEvent<System.Enum> as well as all enumField types I could find - nothing I have tried has worked.
    All other controls I am able to hook into such as strings, ints etc - but any enum fields I can't seem to be able to register a callback.
     
    Last edited: Jun 11, 2019
  4. antoine-unity

    antoine-unity

    Unity Technologies

    Joined:
    Sep 10, 2015
    Posts:
    101
    Hello,

    One last thing to try if you are using 2019.2 already : from the main menu open "UI > UIElements Samples" and then select "Choice Fields > Enum". This displays an example of using the EnumField and specifically how to register the change event callback.

    If that doesn't help, can you create a simple script that exhibits your issue so we can look into it?

    Thanks
     
  5. OtoriScorp

    OtoriScorp

    Joined:
    Mar 9, 2016
    Posts:
    28
    Thanks for the reply Antoine.
    Also thankyou for the heads up about the UIElements window, that is very helpful!

    I created a new field and initialized it in the C# script in the same way the example was shown - and success! This worked!

    Previously, I created the field purely in the UXML file via a PropertyField and binding-path. Doing it this way did not register the callback, curiously though - it did for the other controls I have, just not enums.

    Would this be a bug I should report, or just by design?