NibiruRemindBoxBase.cs 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace Nxr.Internal
  5. {
  6. public class NibiruRemindBoxBase : MonoBehaviour
  7. {
  8. //private GameObject box;
  9. [NonSerialized] public GameObject remindbox;
  10. private static bool isClose = false;
  11. private Text defaultText;
  12. private GameObject cameraObject;
  13. private GameObject tagImage;
  14. float time = 0;
  15. float timeEnd;
  16. public bool Showing()
  17. {
  18. return remindbox != null;
  19. }
  20. /// <summary>
  21. /// Initialization
  22. /// </summary>
  23. public bool Init(float timeend)
  24. {
  25. NibiruShutDownBox.Instance.ReleaseDestory();
  26. ReleaseDestory();
  27. time = 0;
  28. timeEnd = timeend;
  29. cameraObject = GameObject.Find("MainCamera");
  30. remindbox = Instantiate(Resources.Load<GameObject>("RemindBox/RemindBox"));
  31. remindbox.GetComponent<Canvas>().worldCamera = cameraObject.GetComponent<Camera>();
  32. // 保证UI显示在视角正前方
  33. Vector3 forward = cameraObject.transform.forward * 3;
  34. remindbox.transform.localPosition =
  35. cameraObject.transform.position +
  36. new Vector3(forward.x, forward.y, forward.z);
  37. remindbox.transform.localRotation = cameraObject.transform.rotation;
  38. defaultText = remindbox.GetComponentInChildren<Text>();
  39. return true;
  40. }
  41. /// <summary>
  42. /// Create the component of Image。
  43. /// </summary>
  44. /// <param name="name"></param>
  45. /// <param name="position"></param>
  46. public void Create(string name, Vector3 position, Vector2 size)
  47. {
  48. GameObject image = new GameObject(name, typeof(Image));
  49. //判断是否是滑块
  50. if (name.Equals("VolumeTag"))
  51. {
  52. tagImage = image;
  53. }
  54. image.GetComponent<Image>().sprite = Resources.Load<Sprite>("RemindBox/" + name);
  55. image.transform.SetParent(remindbox.transform);
  56. image.GetComponent<RectTransform>().sizeDelta = size;
  57. image.GetComponent<Image>().rectTransform.localPosition = position;
  58. image.GetComponent<Image>().rectTransform.localScale = new Vector3(1f, 1f, 1f);
  59. image.GetComponent<Image>().rectTransform.localRotation = Quaternion.identity;
  60. image.GetComponent<Image>().raycastTarget = false;
  61. }
  62. /// <summary>
  63. /// Create the component of Text。
  64. /// </summary>
  65. /// <param name="name"></param>
  66. /// <param name="context"></param>
  67. /// <param name="position"></param>
  68. public void Create(string name, string context, Vector3 position, Vector2 size)
  69. {
  70. GameObject text = new GameObject(name);
  71. text.AddComponent<Text>();
  72. Text mText = text.GetComponent<Text>();
  73. mText.font = defaultText.font;
  74. text.transform.SetParent(remindbox.transform);
  75. text.GetComponent<RectTransform>().sizeDelta = size;
  76. mText.alignment = defaultText.alignment;
  77. mText.rectTransform.localPosition = position;
  78. mText.rectTransform.localRotation = Quaternion.identity;
  79. mText.text = context;
  80. mText.fontSize = 50;
  81. mText.rectTransform.localScale = new Vector3(0.3f, 0.3f, 1f);
  82. mText.raycastTarget = false;
  83. }
  84. /// <summary>
  85. /// Create the component of Button。
  86. /// </summary>
  87. /// <param name="name"></param>
  88. /// <param name="position"></param>
  89. /// <param name="action"></param>
  90. public void Create(string name, Vector3 position, Vector2 size, NibiruRemindBoxEvent.RemindBoxEvent action)
  91. {
  92. GameObject quad =
  93. (GameObject) Instantiate(Resources.Load<GameObject>("RemindBox/Quad"), remindbox.transform);
  94. quad.name = name;
  95. quad.transform.localPosition = position;
  96. quad.transform.localRotation = Quaternion.identity;
  97. quad.transform.localScale = new Vector3(size.x, size.y, 1);
  98. quad.GetComponent<NibiruRemindBoxEvent>().handleRemindBox = action;
  99. quad.GetComponent<MeshRenderer>().material.color = new Color(0, 0, 0, 0f);
  100. // 怀疑是unity的bug,导致碰撞信息失效
  101. quad.GetComponent<BoxCollider>().enabled = false;
  102. quad.GetComponent<BoxCollider>().enabled = true;
  103. }
  104. /// <summary>
  105. /// Create tag and reset time.
  106. /// </summary>
  107. /// <param name="position"></param>
  108. /// <param name="size"></param>
  109. public void Create(Vector3 position, Vector2 size)
  110. {
  111. if (time >= timeEnd)
  112. {
  113. Image[] contents = remindbox.GetComponentsInChildren<Image>();
  114. foreach (Image child in contents)
  115. {
  116. child.color = new Color(255, 255, 255, 1);
  117. }
  118. Text[] context = remindbox.GetComponentsInChildren<Text>();
  119. foreach (Text child in context)
  120. {
  121. child.color = new Color(255, 255, 255, 1);
  122. }
  123. }
  124. time = 0;
  125. if (tagImage != null)
  126. {
  127. Destroy(tagImage);
  128. }
  129. Create("VolumeTag", position, size);
  130. }
  131. /// <summary>
  132. /// Close
  133. /// </summary>
  134. public void Close()
  135. {
  136. isClose = true;
  137. }
  138. public void ReleaseDestory()
  139. {
  140. if (remindbox != null)
  141. {
  142. Destroy(remindbox);
  143. }
  144. isClose = false;
  145. }
  146. /// <summary>
  147. /// FadeOut
  148. /// </summary>
  149. /// <param name="remindbox"></param>
  150. public void FadeOut(GameObject remindbox)
  151. {
  152. if (remindbox != null)
  153. {
  154. Image[] contents = remindbox.GetComponentsInChildren<Image>();
  155. foreach (Image child in contents)
  156. {
  157. child.color = new Color(255, 255, 255, child.color.a - 0.01f);
  158. }
  159. Text[] context = remindbox.GetComponentsInChildren<Text>();
  160. foreach (Text child in context)
  161. {
  162. child.color = new Color(255, 255, 255, child.color.a - 0.01f);
  163. }
  164. MeshRenderer[] meshRenderer = remindbox.GetComponentsInChildren<MeshRenderer>();
  165. foreach (MeshRenderer mr in meshRenderer)
  166. {
  167. mr.material.color = new Color(mr.material.color.r, mr.material.color.g, mr.material.color.b,
  168. mr.material.color.a - 0.01f);
  169. }
  170. if (context[0].color.a <= 0)
  171. {
  172. ReleaseDestory();
  173. time = 0;
  174. //清除原点选中效果
  175. NxrReticle mNxrReticle = NxrViewer.Instance.GetNxrReticle();
  176. if (mNxrReticle != null)
  177. {
  178. mNxrReticle.OnGazeExit(null, null);
  179. }
  180. }
  181. }
  182. }
  183. void Update()
  184. {
  185. if (isClose)
  186. {
  187. time += Time.deltaTime;
  188. if (time >= timeEnd)
  189. {
  190. FadeOut(remindbox);
  191. }
  192. }
  193. }
  194. }
  195. }