UIConfirmPanel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Rokid.MRC
  6. {
  7. public enum ConfirmType
  8. {
  9. None,
  10. OneConfirm,
  11. TwoConfirmCancel,
  12. OneCancel,
  13. }
  14. //弹窗界面
  15. public class UIConfirmPanel : UIPanelBase
  16. {
  17. private Text txtTitle;
  18. private RectTransform rectConfirm;
  19. private Text txtButtonConfrim;
  20. private RectTransform rectCancel;
  21. private Text txtButtonCancel;
  22. private Text txtContent;
  23. private Action confirmCallBack;
  24. private Action cancelCallBack;
  25. private float XOffest = 170;
  26. private ConfirmType curConfirmType;
  27. public override void OnInit()
  28. {
  29. base.OnInit();
  30. txtTitle = transform.Find("DisplayView/TxtTittle").GetComponent<Text>();
  31. rectConfirm = transform.Find("DisplayView/BtnConfirm").GetComponent<RectTransform>();
  32. txtButtonConfrim = transform.Find("DisplayView/BtnConfirm/Text").GetComponent<Text>();
  33. rectConfirm.GetComponent<Button>().onClick.AddListener(() =>
  34. {
  35. if(confirmCallBack != null)
  36. {
  37. confirmCallBack();
  38. }
  39. gameObject.SetActive(false);
  40. });
  41. rectCancel = transform.Find("DisplayView/BtnCancel").GetComponent<RectTransform>();
  42. txtButtonCancel = transform.Find("DisplayView/BtnCancel/Text").GetComponent<Text>();
  43. rectCancel.GetComponent<Button>().onClick.AddListener(() =>
  44. {
  45. if(cancelCallBack != null)
  46. {
  47. cancelCallBack();
  48. }
  49. gameObject.SetActive(false);
  50. });
  51. txtContent = transform.Find("DisplayView/TxtInfo").GetComponent<Text>();
  52. //语言表
  53. txtButtonConfrim.text = LocalizationMgr.Instance.GetTextByKey("ButtonConfirm");
  54. txtButtonCancel.text = LocalizationMgr.Instance.GetTextByKey("ButtonCancel");
  55. }
  56. public override void OnOpen()
  57. {
  58. base.OnOpen();
  59. //TODO,暂时这样处理
  60. MessageCenter.AddMsgListener(GlobalDefine.SpaceBuildEnd, HandSpaceBuild);
  61. }
  62. public override void OnClose()
  63. {
  64. base.OnClose();
  65. MessageCenter.RemoveMsgListener(GlobalDefine.SpaceBuildEnd, HandSpaceBuild);
  66. StopAllCoroutines();
  67. }
  68. private void HandSpaceBuild(object val)
  69. {
  70. if(curConfirmType == ConfirmType.None)
  71. {
  72. gameObject.SetActive(false);
  73. }
  74. }
  75. public void SetContent(string title, string content, float closeTime, ConfirmType confirmType = ConfirmType.TwoConfirmCancel, Action confirmCall = null, Action cancelCall = null)
  76. {
  77. txtTitle.text = title;
  78. txtContent.text = content;
  79. curConfirmType = confirmType;
  80. switch(confirmType)
  81. {
  82. case ConfirmType.None:
  83. rectCancel.gameObject.SetActive(false);
  84. rectConfirm.gameObject.SetActive(false);
  85. break;
  86. case ConfirmType.OneConfirm:
  87. rectCancel.gameObject.SetActive(false);
  88. rectConfirm.gameObject.SetActive(true);
  89. rectConfirm.anchoredPosition = new Vector2(0, rectConfirm.anchoredPosition.y);
  90. break;
  91. case ConfirmType.TwoConfirmCancel:
  92. rectCancel.gameObject.SetActive(true);
  93. rectConfirm.gameObject.SetActive(true);
  94. rectConfirm.anchoredPosition = new Vector2(XOffest, rectConfirm.anchoredPosition.y);
  95. rectCancel.anchoredPosition = new Vector2(-XOffest, rectConfirm.anchoredPosition.y);
  96. break;
  97. case ConfirmType.OneCancel:
  98. rectCancel.gameObject.SetActive(true);
  99. rectConfirm.gameObject.SetActive(false);
  100. rectCancel.anchoredPosition = new Vector2(0, rectConfirm.anchoredPosition.y);
  101. break;
  102. default:
  103. break;
  104. }
  105. confirmCallBack = confirmCall;
  106. cancelCallBack = cancelCall;
  107. if(closeTime != 0)
  108. {
  109. StartCoroutine(WaitAndClose(closeTime));
  110. }
  111. }
  112. private IEnumerator WaitAndClose(float time)
  113. {
  114. yield return new WaitForSeconds(time);
  115. UIManager.Instance.ClosePanel(UIType.Confirm);
  116. }
  117. }
  118. }