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 get the button name from the auto-generate button

Discussion in 'Immediate Mode GUI (IMGUI)' started by sarangge, Jul 30, 2015.

  1. sarangge

    sarangge

    Joined:
    Jul 29, 2015
    Posts:
    11
    i am doing auto-generate button. All the list gameobject is been list out as the name of the buttons. The number of the button depends on the the number of the gameobject.

    Question:
    What i need is, i need to make button listener for each of button. So, in my plan, i am thinking of getting the name of the button click, and it will do as what it suppose to do.




    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class ListObject : MonoBehaviour {
    7.     GameObject[] allObjects;
    8.     int posbutton = 40;
    9.     string listObject= "";
    10.     //private Button myButton;
    11.    
    12.     void Start ()
    13.     {
    14.         allObjects = UnityEngine.Object.FindObjectsOfType<GameObject> ();
    15.         Debug.Log ("Total Length: " + allObjects.Length.ToString ());
    16.         Debug.Log ("LIST OBJECT");
    17.  
    18.     }
    19.  
    20.     void OnGUI()
    21.     {
    22.         // Make a background box
    23.         GUI.Box(new Rect(10,10,200,25*allObjects.Length), "Loader Menu");
    24.  
    25.         //for loop to get the list object
    26.         for(int x=0; x<allObjects.Length; x++)
    27.         {
    28.             listObject= allObjects[x].name;
    29.  
    30.             //auto-generate button position
    31.             if(GUI.Button(new Rect(20,posbutton+(x*20),180,20), listObject))
    32.             {
    33.                 //button will do something
    34.                 buttonListener(buttonName);
    35.             }
    36.         }
    37.     }  
    38.  
    39.     void buttonListener(Button buttonName)
    40.     {
    41.         //if name==A, then do A
    42.         //else if name==B, then do B
    43.         //else, do C
    44.     }
    45. }


    p/s: if there is any other way out of doing this, please let me know. thank u in advance.
     
  2. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    Soemthing Like that ?
    Code (CSharp):
    1. void OnGUI()
    2.     {
    3.         GUI.Box(new Rect(10,10,200,25*allObjects.Length), "Loader Menu");
    4.         for(int x=0; x<allObjects.Length; x++)
    5.         {
    6.             listObject= allObjects[x].name;
    7.             if(GUI.Button(new Rect(20,posbutton+(x*20),180,20), listObject))
    8.             {
    9.                 buttonListener(listObject);
    10.                 OR
    11.                 buttonListener(allObjects[x]);
    12.             }
    13.         }
    14.     }
    15.     private void buttonListener(string name)
    16.     {
    17.        
    18.     }
    19.  
    20.     private void buttonListener(GameObject yourGameObject){
    21.  
    22.     }
     
    sarangge likes this.
  3. sarangge

    sarangge

    Joined:
    Jul 29, 2015
    Posts:
    11
    yes yes. this is want i want. it does work. cant believe how simple it is. thanks!