I’m setting up a window with a bunch of buttons nested in a listView.
WWhen binding items to the listView I do the following :
arg1.Q<Button>("PrefabButton").clickable.clicked += () => OpenPrefab(prefab);
The issue is, as items in the list gets recycled and binded again, the clicked event is never cleaned, so it keeps on adding callbacks to the list.
Is there a way to remove callbacks from this event? I’ve tried using -= but it doesn’t seem to work, probably because the “prefab” variable is different on each bind.
How about using .userData for event handling?
Register click event in makeItems using the userData of button itself.
var button = your button
button.clickable.clicked += () => OpenPrefab(button.userData);
Change userData in bindItems.
1 Like
Hello,
when subscribing to a delegate with a Temporary function (...+= () =>...
), you can not unsubscribe that temp function.
Typically you pass in a stardard function:
//subscribe
arg1.Q<Button>("PrefabButton").clickable.clicked +=OpenPrefab;
...
void OpenPrefab()
{
//Get the correct prefab
}
...
//unsubscribe
arg1.Q<Button>("PrefabButton").clickable.clicked -=OpenPrefab;
typically when you run into issues, like yours above, it means you should re-structure your code.
1 Like
BinaryCats:
Hello,
when subscribing to a delegate with a Temporary function (...+= () =>...
), you can not unsubscribe that temp function.
Typically you pass in a stardard function:
//subscribe
arg1.Q<Button>("PrefabButton").clickable.clicked +=OpenPrefab;
...
void OpenPrefab()
{
//Get the correct prefab
}
...
//unsubscribe
arg1.Q<Button>("PrefabButton").clickable.clicked -=OpenPrefab;
typically when you run into issues, like yours above, it means you should re-structure your code.
okay that’s what I thought, thanks for the confirmation.
@Kichang-Kim : I didn’t thought of that, but that seems like a good way to adress my issues, thanks.