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.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class ListObject : MonoBehaviour {
GameObject[] allObjects;
int posbutton = 40;
string listObject= "";
//private Button myButton;
void Start ()
{
allObjects = UnityEngine.Object.FindObjectsOfType<GameObject> ();
Debug.Log ("Total Length: " + allObjects.Length.ToString ());
Debug.Log ("LIST OBJECT");
}
void OnGUI()
{
// Make a background box
GUI.Box(new Rect(10,10,200,25*allObjects.Length), "Loader Menu");
//for loop to get the list object
for(int x=0; x<allObjects.Length; x++)
{
listObject= allObjects[x].name;
//auto-generate button position
if(GUI.Button(new Rect(20,posbutton+(x*20),180,20), listObject))
{
//button will do something
buttonListener(buttonName);
}
}
}
void buttonListener(Button buttonName)
{
//if name==A, then do A
//else if name==B, then do B
//else, do C
}
}
p/s: if there is any other way out of doing this, please let me know. thank u in advance.