What is the quickest/best method for assigning OnClick events to buttons, when the canvas is a prefab that appears in every level? Can it be done in the Inspector or only via code?
What’s the best way to have the canvas prefab’s OnClick events use a script from a different object?
(All the events I want to call are in my pre-4.6 “Controller” prefab, and I don’t want to make the Canvas be a child of that: I tried it, but every time I add/delete/re-parent a button from the UI, it breaks the prefab instance, and then when I Apply, I seem to be losing level-specific custom values elsewhere in the Controller prefab.)
My inclination is to put a script on my canvas that uses GameObject.Find to locate my Controller prefab, and then write an extra function for each button: one attached to the canvas, called by OnClick, which then in turn calls the actual desired function from the Controller. Is there a more elegant way?
Buttons Events can generally only be set in the inspector if they reference an object that is a part of the same prefab. If you’re trying to set them to a different object, then you’ve pretty much got to set them up in a script. If you know in advance exactly what public functions you want to assign to each button, this shouldn’t be so hard to do. You just make a script that sits on the canvas that has a reference to every button. On start you get the controller, and then go through each button and set
Of course… the real best practice way to do it is to use an Inversion of Control setup like StrangeIoC… but explaining that is out of the scope of the forums.