0

I'm creating UI elements on the fly from UXML templates, and attaching click events to generated buttons. Like this:

public VisualTreeAsset foobahTemplate;
VisualElement foobah = foobahTemplate.Instantiate();
foobah.Q<Button>(name: "foobahButton").clicked += () => { foobahClick("foo", "bah"); };

This works, and I'm guessing because of the += operator you can effectively bind multiple events this way to the same click. I quite like the logic there.

But how would I go about replacing a binding? I assume -= removes the same as += adds, but since the binding is of an anonymous function via the lambda syntax, I don't understand how you'd later pluck a particular event out to unbind?

For example, if doing this:

foobah.Q<Button>(name: "foobahButton").clicked += () => { foobahClick1("lorem", "ipsum"); };
foobah.Q<Button>(name: "foobahButton").clicked += () => { foobahClick2("dolor", "sit"); };
foobah.Q<Button>(name: "foobahButton").clicked += () => { foobahClick3("amet", "consectetur"); };

How would I later unbind the foobahclick2() call leaving foobahclick1() and foobahclick3() in place, or replace it with say, foobahClick2("adipiscing", "elit");?

Ric
  • 458
  • 1
  • 7
  • 23

0 Answers0