VPDPersonal / UnityFastTools

1 stars 0 forks source link

UIToolkit attribute for the handler method #4

Open paulpach opened 3 weeks ago

paulpach commented 3 weeks ago

As an alternative to #3, what if I could put an attribute on the handler. For example, if I want to handle the ClickEvent on a button named "OK", I would do something like this:

public partial class MyWindowController: MonoBehavior {
     [UIHandler("OK",)]
     public void OnClicked(ClickEvent evt) {
         // runs when the visual element OK raises the event ClickEvent
     }
}

It would generate code like this:

[RequireComponent(typeof(UIDocument))]
public partial class MyWindowController: MonoBehavior {

     public void SubscribeUIHandler() {
          TryGetComponent(out UIDocument document);
          var root = document.root;
          root.Q("OK").RegisterCallback<ClickEvent>(OkClicked);
     }
}
VPDPersonal commented 3 weeks ago

Also a good solution. We will add both options so that there is a choice.