usingUnityEngine;usingUnityEngine.UI;public classUiMessage : MonoBehaviour
{publicButton ok;publicButton cancel;publicButton retry;///
///遮罩///
publicGameObject panel;publicText headText;publicText text;///
///弹出框///
privateGameObject messageBox;private voidAwake()
{
messageBox= gameObject.transform.GetChild(1).gameObject;
ok= messageBox.transform.Find("ok").GetComponent();
cancel= messageBox.transform.Find("cancel").GetComponent();
retry= messageBox.transform.Find("retry").GetComponent();
panel= gameObject.transform.Find("panel").gameObject;
text= messageBox.transform.Find("Text").GetComponent();
headText= messageBox.transform.GetChild(0).Find("head").GetComponent();//将提示框居中显示
messageBox.transform.position = new Vector3(Screen.width / 2 - messageBox.GetComponent().rect.width / 2,
Screen.height/ 2 + messageBox.GetComponent().rect.height / 2, 0);
init();
}private voidOnEnable()
{
init();
}private voidinit()
{
ok.onClick.AddListener(MessageBox.onClickOk);
cancel.onClick.AddListener(MessageBox.onClickCancel);
retry.onClick.AddListener(MessageBox.onClickRetry);
text.text=MessageBox.text;
headText.text=MessageBox.headText;//根据传递的参数,进行样式的显示
switch(MessageBox.dialogResult)
{caseDialogResult.Ok:
ok.gameObject.SetActive(true);
cancel.gameObject.SetActive(false);
retry.gameObject.SetActive(false);break;caseDialogResult.OKCancel:
ok.gameObject.SetActive(true);
cancel.gameObject.SetActive(true);
retry.gameObject.SetActive(false);break;caseDialogResult.RetryCancel:
ok.gameObject.SetActive(true);
cancel.gameObject.SetActive(true);
retry.gameObject.SetActive(true);break;caseDialogResult.YesNo:
ok.transform.GetChild(0).GetComponent().text = "是";
cancel.transform.GetChild(0).GetComponent().text = "否";
ok.gameObject.SetActive(true);
cancel.gameObject.SetActive(true);
retry.gameObject.SetActive(false);break;caseDialogResult.YesNoCancel:
ok.transform.GetChild(0).GetComponent().text = "是";
cancel.transform.GetChild(0).GetComponent().text = "否";
ok.gameObject.SetActive(true);
cancel.gameObject.SetActive(true);
retry.gameObject.SetActive(true);break;
}
}private voidUpdate()
{
panel.SetActive(MessageBox.type);
gameObject.SetActive(MessageBox.state);
}
}