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

How to find the UI button gameObject calling specific function with onClick

Discussion in 'UGUI & TextMesh Pro' started by kauaiBlake, Nov 19, 2014.

  1. kauaiBlake

    kauaiBlake

    Joined:
    Feb 28, 2013
    Posts:
    36
    Hi there,

    I am working on a project that is fairly large, and coming in new to it, and its using the new Unity UI system. So I am trying to figure out the games logic and flow, and I come across functions in the code base that are called from UI buttons onClick event. Thing is I want to find which button is calling it, to fully understand the project I am working on, and there my issue comes up.

    I have lots of buttons, and there is no way to search in the editor for that function name (ie myFunction() ) that I can see. It seems I have to click each button in the project (pain as there are lots of them), and inspect each one till I find the right button with the function specified in the onclick paramters in the inspector. This is a pain for this larger project.

    So, am I missing something? Is there a way to quickly find the button via a search, that is calling my function? I know some said in IRC that maybe I could write an editor script to do this, but frankly thats also a PITA. Would be nice if this search capability was built into the editor.

    Thanks,
    Blake
     
    awsapps likes this.
  2. Senshi

    Senshi

    Joined:
    Oct 3, 2010
    Posts:
    557
    I don't think there's an easy way of doing this, but perhaps in your case even a simple list representation would help untangle the logic a bit? If so, maybe you could just make a quick MonoBehavior and print out the results, like so:

    Code (csharp):
    1. //AllOnClicks.cs
    2. Button[] buttons = (Button[])FindObjectsOfType<Button>();
    3. foreach(Button btn in buttons){
    4.     int ec = btn.onClick.GetPersistentEventCount();
    5.     Debug.LogWarning("Button " + btn + " (" + ec + " onClick delegates): ");
    6.     for(int i=0; i<ec; i++){
    7.         Debug.Log("onClick " + i + ": " + btn.onClick.GetPersistentTarget(i) + "." + btn.onClick.GetPersistentMethodName(i) + "()");
    8.     }
    9. }
    Sadly I didn't find a way to print out the inspector-assigned variables this way though.
     
  3. Fattie

    Fattie

    Joined:
    Jul 5, 2012
    Posts:
    476
  4. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'm back from holidays...

    Blake, did you still need help with this?
     
  5. awsapps

    awsapps

    Joined:
    Jun 15, 2021
    Posts:
    74
    It's a little primitive, but you can search the name of the method in your scene.unity file.
    For each match, search the parent GameObject -> m_Name.

    For example, in the following code, the GoToMenu method is bind on a button called GoBack.
     
    dev2X likes this.
  6. Nichathan

    Nichathan

    Joined:
    Mar 8, 2020
    Posts:
    23
    If you know the name of the GameObject calling your method, you can just use GameObject.Find("NameOfGameObject"), but this may not work if you have many GameObjects throughout many scenes with different names calling the same method which (would be odd indeed) in that case, you can try to pass an integer into the method that the button calls and then use a switch statement to capture the button.

    If there is a way to find which GameObject called a method programmatically, I have not found that method. I'm sure finding proper docs with proper explanations would help with that but these Unity docs seem incomplete compared to other doc files I've sifted through (Like React docs are usually nice with examples of best practices).
     
    chriseborn likes this.