VRDialogMenu.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Events;
  6. using UnityEngine.UI;
  7. public class VRDialogMenu : MonoBehaviour
  8. {
  9. public static VRDialogMenu CreateDialog()
  10. {
  11. if (VRDialogMenu.m_Instance == null)
  12. {
  13. VRDialogMenu.m_Instance = UnityEngine.Object.FindObjectOfType<VRDialogMenu>();
  14. }
  15. return VRDialogMenu.m_Instance;
  16. }
  17. private void Start()
  18. {
  19. if (VRDialogMenu.m_Instance != null)
  20. {
  21. }
  22. VRDialogMenu.m_Instance = this;
  23. base.gameObject.SetActive(false);
  24. }
  25. public void CloseDialog()
  26. {
  27. this.m_UICanvasFade.FadeOut();
  28. VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.MainMenu);
  29. VRCanvasManager.Instance.ClearCanvasStack();
  30. }
  31. public void OpenDialog(string message, params VRDialogMenu.DialogButton[] dialogButtons)
  32. {
  33. VRCanvasManager.Instance.OpenVRCanvas(VRCanvasManager.VRCanvasType.Dialog);
  34. base.gameObject.SetActive(true);
  35. this.m_TextMessage.text = message;
  36. for (int i = 0; i < this.m_ButtonArray.Count; i++)
  37. {
  38. this.m_ButtonArray[i].SetActive(false);
  39. }
  40. for (int j = 0; j < dialogButtons.Length; j++)
  41. {
  42. VRDialogMenu.DialogButton dialogButton = dialogButtons[j];
  43. GameObject gameObject;
  44. if (this.m_ButtonArray.Count > j)
  45. {
  46. gameObject = this.m_ButtonArray[j].gameObject;
  47. }
  48. else
  49. {
  50. gameObject = UnityEngine.Object.Instantiate<GameObject>(this.m_PrefabDialogButton);
  51. this.m_ButtonArray.Add(gameObject);
  52. }
  53. gameObject.SetActive(true);
  54. Button component = gameObject.GetComponent<Button>();
  55. Text componentInChildren = gameObject.GetComponentInChildren<Text>();
  56. Image image = gameObject.GetComponent<Image>();
  57. component.onClick.RemoveAllListeners();
  58. if (dialogButton.isImage)
  59. {
  60. componentInChildren.enabled = false;
  61. string path = this.m_uGUIAtlasPath + dialogButton.text;
  62. base.StartCoroutine(this.Coroutine_Load<Sprite>(path, delegate(ResourceRequest req)
  63. {
  64. image.sprite = (req.asset as Sprite);
  65. }));
  66. }
  67. else
  68. {
  69. componentInChildren.text = dialogButton.text;
  70. componentInChildren.enabled = true;
  71. string path2 = this.m_uGUIAtlasPath + "vr_frame02.png";
  72. base.StartCoroutine(this.Coroutine_Load<Sprite>(path2, delegate(ResourceRequest req)
  73. {
  74. image.sprite = (req.asset as Sprite);
  75. }));
  76. }
  77. component.onClick.AddListener(dialogButton.action);
  78. RectTransform component2 = gameObject.GetComponent<RectTransform>();
  79. if (!dialogButton.isFreePosition)
  80. {
  81. component2.SetParent(this.m_ParentButtonArea, false);
  82. }
  83. else
  84. {
  85. component2.SetParent(base.gameObject.transform, false);
  86. component2.anchorMin = Vector2.one * 0.5f;
  87. component2.anchorMax = Vector2.one * 0.5f;
  88. component2.anchoredPosition3D = dialogButton.anchoredPosition3D;
  89. component2.sizeDelta = dialogButton.size;
  90. }
  91. }
  92. }
  93. public void OpenDialog(string message, VRDialogMenu.TYPE_STYLE style, UnityAction<VRDialogMenu.TYPE_STYLE> callback)
  94. {
  95. List<VRDialogMenu.DialogButton> list = new List<VRDialogMenu.DialogButton>();
  96. if ((style & VRDialogMenu.TYPE_STYLE.NO) != (VRDialogMenu.TYPE_STYLE)0)
  97. {
  98. list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_no", delegate
  99. {
  100. callback(VRDialogMenu.TYPE_STYLE.NO);
  101. }));
  102. }
  103. if ((style & VRDialogMenu.TYPE_STYLE.YES) != (VRDialogMenu.TYPE_STYLE)0)
  104. {
  105. list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_yes", delegate
  106. {
  107. callback(VRDialogMenu.TYPE_STYLE.YES);
  108. }));
  109. }
  110. if ((style & VRDialogMenu.TYPE_STYLE.CANCEL) != (VRDialogMenu.TYPE_STYLE)0)
  111. {
  112. list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_cancel", delegate
  113. {
  114. callback(VRDialogMenu.TYPE_STYLE.CANCEL);
  115. }));
  116. }
  117. if ((style & VRDialogMenu.TYPE_STYLE.OK) != (VRDialogMenu.TYPE_STYLE)0)
  118. {
  119. list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_ok", delegate
  120. {
  121. callback(VRDialogMenu.TYPE_STYLE.OK);
  122. }));
  123. }
  124. if ((style & VRDialogMenu.TYPE_STYLE.TV) != (VRDialogMenu.TYPE_STYLE)0)
  125. {
  126. list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_tv", delegate
  127. {
  128. callback(VRDialogMenu.TYPE_STYLE.TV);
  129. }));
  130. }
  131. if ((style & VRDialogMenu.TYPE_STYLE.CD) != (VRDialogMenu.TYPE_STYLE)0)
  132. {
  133. list.Add(new VRDialogMenu.DialogButton(true, "vr_dialog_cd", delegate
  134. {
  135. callback(VRDialogMenu.TYPE_STYLE.CD);
  136. }));
  137. }
  138. this.OpenDialog(message, list.ToArray());
  139. }
  140. private IEnumerator Coroutine_Load<T>(string path, UnityAction<ResourceRequest> callback) where T : UnityEngine.Object
  141. {
  142. ResourceRequest res = Resources.LoadAsync<T>(path);
  143. while (!res.isDone)
  144. {
  145. yield return null;
  146. }
  147. callback(res);
  148. yield break;
  149. }
  150. private static VRDialogMenu m_Instance;
  151. [SerializeField]
  152. private UICanvasFade m_UICanvasFade;
  153. [SerializeField]
  154. private Text m_TextMessage;
  155. [SerializeField]
  156. private RectTransform m_ParentButtonArea;
  157. [SerializeField]
  158. private GameObject m_PrefabDialogButton;
  159. private List<GameObject> m_ButtonArray = new List<GameObject>();
  160. private string m_uGUIAtlasPath = "SceneVRCommunication/Tablet/Sprite/System UI/";
  161. public class DialogButton
  162. {
  163. public DialogButton(bool IsImage, string ButtonText, UnityAction ButtonAction)
  164. {
  165. this.isImage = IsImage;
  166. this.text = ButtonText;
  167. this.action = ButtonAction;
  168. }
  169. public DialogButton(bool IsImage, string ButtonText, UnityAction ButtonAction, Vector3 AnchoredPosition3D, Vector2 Size)
  170. {
  171. this.isImage = IsImage;
  172. this.text = ButtonText;
  173. this.action = ButtonAction;
  174. this.anchoredPosition3D = AnchoredPosition3D;
  175. this.size = Size;
  176. this.isFreePosition = true;
  177. }
  178. public bool isImage;
  179. public string text = "?";
  180. public UnityAction action;
  181. public Vector3 anchoredPosition3D = Vector3.zero;
  182. public Vector2 size = Vector2.one;
  183. public bool isFreePosition;
  184. }
  185. public enum TYPE_STYLE
  186. {
  187. YES = 1,
  188. NO,
  189. OK = 4,
  190. CANCEL = 8,
  191. TV = 16,
  192. CD = 32
  193. }
  194. }