Hi all
I have dynamically Created a 5 buttons.I have no problem with it. In this i have a 4 players. if i click for each button . It should identify the player and it should do function.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ButtonsProg : MonoBehaviour {
public GameObject prefabButton;
public RectTransform ParentPanel;
public String k1="Player1",k2="Player2",k3="Player3";
public String chooseplayer;
public int playerselect;
// Use this for initialization
void Start () {
if(playerselect==1)
chooseplayer=k1;
if(playerselect==2)
chooseplayer=k2;
if(playerselect==3)
chooseplayer=k3;
for(int i = 0; i < 5; i++)
{
GameObject goButton = (GameObject)Instantiate(prefabButton);
goButton.transform.SetParent(ParentPanel, false);
goButton.transform.localScale = new Vector3(1, 1, 1);
Button tempButton = goButton.GetComponent<Button>();
int tempInt = i;
tempButton.onClick.AddListener(() => ButtonClicked(tempInt,chooseplayer));
}
}
void ButtonClicked(int buttonNo,string Character)
{
if(buttonNo==1 && Character=="player1")
Debug.Log("DO this functions");
if(buttonNo==2 && Character=="player1")
Debug.Log("DO this functions");
----
---
if(buttonNo==5 && Character=="player1")
Debug.Log("DO this functions");
for player2 function i have wrote like that.....
if(buttonNo==1 && Character=="player2")
Debug.Log("DO this functions");
if(buttonNo==2 && Character=="player2")
Debug.Log("DO this functions");
----
---
if(buttonNo==5 && Character=="player2")
Debug.Log("DO this functions");
}
for player 3 i have written the same code...........
}
i think the code is too long. HOW TO REDUCE THE ABOVE CODE. How can i optimize the code.....
thanks in advace...