Search Unity

Using UI Button.onClick.AddListener() vs assigning functions in Editor

Discussion in 'UGUI & TextMesh Pro' started by Sailendu, May 13, 2019.

  1. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
    Hello,

    Can someone guide me on this?
    I have around 500 buttons in my scene inside various UI panels. Will there be any performance difference if I use the Button.onClick.AddListener() using a C# script to assign functions to all the buttons vs if I assign all the functions in the editor? The Button.onClick.AddListener() using script is more convenient for me, because dragging all the required game objects and choosing functions in the editor seems like too much work. So let's say I assign all the functions in a Start() method, will I get any performance problem during the Start() method? Will it crash the game as there are more than 500 buttons? Please someone guide me on this, as I could not find anything about this by searching.
     
    CandraWi, wanrclhnyd and happypudding like this.
  2. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    I know this post is REALLY old but I am actually seeking the same answer as Sailendu does.
    I don't know whether its been dropped by now or maybe you found a solution, but if you do know an answer, could you please share it?
     
  3. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
  4. Sailendu

    Sailendu

    Joined:
    Jul 23, 2009
    Posts:
    254
    The link you posted has some nice info.
    As no one answered my question, I used Invoke() to divide the calls of AddListener(). I divided the buttons into groups of 150 and called Invoke() with 0.5 seconds interval. Like below:

    Code (CSharp):
    1. void Start()
    2. {
    3.     SetupButtonsA();
    4.     Invoke("SetupButtonsB", 0.5f);
    5.     Invoke("SetupButtonsC", 1.0f);
    6. }
    7.  
    8. void SetupButtonsA()
    9. {
    10.     // First group of 150 buttons...
    11. }
    12.  
    13. void SetupButtonsB()
    14. {
    15.     // Second group of 150 buttons...
    16. }
    17.  
    18. void SetupButtonsC()
    19. {
    20.     // And so on...
    21. }
    I don't know if this is necessary to give this 0.5 seconds gap between these calls, or if it has some bad effects, but it worked for me and I did not face any crash or issue till now. You can try decreasing the time gap if you want, I did not try. 1 second is good enough for my project. Hope it helps.
     
  5. RoeeHerzovich

    RoeeHerzovich

    Joined:
    Apr 12, 2020
    Posts:
    103
    Well, you work with hundreds of buttons according to what you said. I never dealt with it, I used AddListener() only for UI buttons which I only have several of... But glad I could help :)
     
  6. starfoxy

    starfoxy

    Joined:
    Apr 24, 2016
    Posts:
    184
    Funny, I am looking for exactly this answer as well. I would surmise that having a bunch of listeners running (whether the UI is being displayed or not) - is not ideal for performance in any case.

    Here's what I decided to do (this is a script I have on a canvas child object):

    Code (CSharp):
    1.  
    2. public Button myButton;
    3. void OnEnable(){
    4.         myButton.onClick.AddListener(clickTheButton);
    5. }
    6. void OnDisable(){
    7.         myButton.onClick.RemoveListener(clickTheButton);
    8. }
    So when I activate/deactivate my canvas, these events fire and add/remove the listeners.

    Testing this, it seems to work. Anyone spot any problems with my methodology?

    ---- update ----

    I should note that (when showing/hiding my canvas) SetActive(true)/SetActive(false) on the canvas component does not fire OnEnable/OnDisable on the canvas children. I have to SetActive(true)/SetActive(false) on the gameobject that the canvas component is attached to.

    Any issues with my approach here?
     
    Last edited: May 25, 2021