public class GUIManager : MonoBehaviour {
private GameObject inputField;
public void LoginButtonClick()
{
Debug.Log("Login Button Clicked");
inputField
= CreateInputField
("Please Enter some text",
new Vector3
()); CreateButton
("Get Input Value",
new Vector3
(0
.0f,
-45f, 0
.0f
), PrintInputFieldValue
);
}
public GameObject CreateInputField(string baseText, Vector3 pos)
{
GameObject inputPrefab = Resources.Load<GameObject>("Prefabs/GUI/InputField");
GameObject input
= Instantiate
(inputPrefab,
new Vector3
(), transform
.rotation) as GameObject
; input.transform.position = pos;
SetTextToChiledLable(input, baseText);
return input;
}
public void CreateButton(string btnName, Vector3 pos, EventDelegate.Callback method)
{
GameObject buttonPrefab = Resources.Load<GameObject>("Prefabs/GUI/Button");
GameObject btn = Instantiate(buttonPrefab, pos, transform.rotation) as GameObject;
SetTextToChiledLable(btn, btnName);
btn
.GetComponent<UIButton
>().onClick.Add(new EventDelegate
(method
)); }
public void SetTextToChiledLable(GameObject parent, string text)
{
foreach(Transform child in parent.transform)
{
if (child.gameObject.name.Equals("Label"))
{
child.gameObject.GetComponent<UILabel>().text = text;
break;
}
}
}
public string GetTextFromChiledLable(GameObject parent)
{
foreach (Transform child in parent.transform)
{
if (child.gameObject.name.Equals("Label"))
{
return child.gameObject.GetComponent<UILabel>().text;
}
}
return "";
}
public void PrintInputFieldValue()
{
Debug.Log("Input field value was: " + inputField.GetComponent<UIInput>().value);
}
}