Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Best practices for assigning OnClicks when UI is a prefab?

Discussion in 'UGUI & TextMesh Pro' started by Morgan, Jan 20, 2015.

  1. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    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?

    Thanks in advance!
     
  2. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    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

    button.onClick.AddListener( controller.methodName );
     
  3. DanSuperGP

    DanSuperGP

    Joined:
    Apr 7, 2013
    Posts:
    408
    marc_tanenbaum likes this.
  4. Morgan

    Morgan

    Joined:
    May 21, 2006
    Posts:
    1,223
    Thanks! (IoC sounds interesting, but for now I'll avoid add-ons unless necessary.)

    AddListener makes good sense, or I may go with (JavaScript):

    function pauseGame() {
    controller.pauseGame();​
    }
    Etc. for each function the OnClicks need to call. Looks silly, but few steps!