Module_Notice.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using TMPro;
  6. namespace SC.XR.Unity
  7. {
  8. [RequireComponent(typeof(CameraFollower))]
  9. public class Module_Notice : MonoBehaviour
  10. {
  11. private static Module_Notice instance;
  12. public static Module_Notice getInstance
  13. {
  14. get
  15. {
  16. if (instance == null)
  17. {
  18. instance = Instantiate(Resources.Load<GameObject>("prefabs/Module_Notice")).GetComponent<Module_Notice>();
  19. }
  20. return instance;
  21. }
  22. }
  23. public string _mainText = "";
  24. public string _minorText = "";
  25. public FollowType _isFollow = FollowType.False;
  26. [Range(0.1f, 10.0f)]
  27. public float _distance = 1.2f;
  28. public float _durationTime = 3f;
  29. public NoticeType _type = NoticeType.None;
  30. public AlignmentType _anchorType = AlignmentType.Center;
  31. private List<Image> _imageList;
  32. public List<Image> ImageList
  33. {
  34. get
  35. {
  36. if (_imageList == null)
  37. {
  38. _imageList = new List<Image>(GetComponentsInChildren<Image>());
  39. }
  40. return _imageList;
  41. }
  42. }
  43. private List<TextMeshProUGUI> _textMeshProUGUIList;
  44. public List<TextMeshProUGUI> TextMeshProUGUIList
  45. {
  46. get
  47. {
  48. if (_textMeshProUGUIList == null)
  49. {
  50. _textMeshProUGUIList = new List<TextMeshProUGUI>(GetComponentsInChildren<TextMeshProUGUI>());
  51. }
  52. return _textMeshProUGUIList;
  53. }
  54. }
  55. private CameraFollower _follower;
  56. public CameraFollower _Follower
  57. {
  58. get
  59. {
  60. if (_follower == null)
  61. {
  62. _follower = GetComponentInChildren<CameraFollower>();
  63. }
  64. return _follower;
  65. }
  66. }
  67. private NoticeEffect _effect;
  68. public NoticeEffect _Effect
  69. {
  70. get
  71. {
  72. if (_effect == null)
  73. {
  74. _effect = GetComponentInChildren<NoticeEffect>();
  75. }
  76. return _effect;
  77. }
  78. }
  79. private void Init()
  80. {
  81. if (_textMeshProUGUIList == null)
  82. {
  83. _textMeshProUGUIList = new List<TextMeshProUGUI>(GetComponentsInChildren<TextMeshProUGUI>());
  84. }
  85. if (_imageList == null)
  86. {
  87. _imageList = new List<Image>(GetComponentsInChildren<Image>());
  88. }
  89. if (_follower == null)
  90. {
  91. _follower = GetComponentInChildren<CameraFollower>();
  92. }
  93. if (_effect == null)
  94. {
  95. _effect = GetComponentInChildren<NoticeEffect>();
  96. }
  97. }
  98. public void RefreshInfo()
  99. {
  100. Init();
  101. foreach (var item in TextMeshProUGUIList)
  102. {
  103. #if UNITY_EDITOR
  104. UnityEditor.Undo.RecordObject(item, " modify Property1");
  105. #endif
  106. }
  107. foreach (var item in ImageList)
  108. {
  109. #if UNITY_EDITOR
  110. UnityEditor.Undo.RecordObject(item, " modify Property2");
  111. #endif
  112. }
  113. SetMainText(_mainText);
  114. SetSubText(_minorText);
  115. SetIsFollow(_isFollow);
  116. SetDistance(_distance);
  117. SetDurationTime(_durationTime);
  118. SetIconTip(_type);
  119. }
  120. public void SetNoticeInfo(string mainString, string subString, NoticeType type = NoticeType.Warning, float distance = 0.8f, AlignmentType _anchorType = AlignmentType.Center, FollowType isFollower = FollowType.True)
  121. {
  122. SetMainText(mainString);
  123. SetSubText(subString);
  124. SetIsFollow(isFollower);
  125. SetDistance(distance);
  126. SetIconTip(type);
  127. SetTextAnchor(_anchorType);
  128. }
  129. public void SetNoticeInfo(string mainString, string subString)
  130. {
  131. SetMainText(mainString);
  132. SetSubText(subString);
  133. }
  134. private void OnEnable()
  135. {
  136. // StartNotice();
  137. foreach (var item in ImageList)
  138. {
  139. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  140. }
  141. foreach (var item in TextMeshProUGUIList)
  142. {
  143. item.color = new Color(item.color.r, item.color.g, item.color.b, 0);
  144. }
  145. }
  146. public void StartNotice()
  147. {
  148. RefreshInfo();
  149. if (_Effect != null)
  150. {
  151. _Effect.enabled = true;
  152. }
  153. }
  154. public void StartNotice(float time)
  155. {
  156. SetDurationTime(time);
  157. if (_Effect != null && !_Effect.enabled)
  158. {
  159. _Effect.enabled = true;
  160. }
  161. }
  162. public void StopNotice()
  163. {
  164. if (_Effect != null && _Effect.enabled)
  165. {
  166. _Effect.enabled = false;
  167. }
  168. }
  169. private void OnDisable()
  170. {
  171. _textMeshProUGUIList = null;
  172. _follower = null;
  173. _effect = null;
  174. }
  175. private void SetTextAnchor(AlignmentType anchorType )
  176. {
  177. foreach (var item in TextMeshProUGUIList)
  178. {
  179. switch (anchorType)
  180. {
  181. case AlignmentType.Left:
  182. item.alignment = TextAlignmentOptions.Left;
  183. break;
  184. case AlignmentType.Center:
  185. item.alignment = TextAlignmentOptions.Center;
  186. break;
  187. case AlignmentType.Right:
  188. item.alignment = TextAlignmentOptions.Right;
  189. break;
  190. default:
  191. break;
  192. }
  193. }
  194. }
  195. private void SetMainText(string str)
  196. {
  197. foreach (var item in TextMeshProUGUIList)
  198. {
  199. if (item.gameObject.name == "MainTip" && str != null)
  200. {
  201. item.text = str;
  202. }
  203. }
  204. }
  205. private void SetSubText(string str)
  206. {
  207. foreach (var item in TextMeshProUGUIList)
  208. {
  209. if (item.gameObject.name == "MinorTip" && str != null)
  210. {
  211. item.text = str;
  212. }
  213. }
  214. }
  215. private void SetIsFollow(FollowType isFollow)
  216. {
  217. if (isFollow== FollowType.True)
  218. {
  219. if (_Follower?.enabled == false)
  220. {
  221. _Follower.enabled = true;
  222. }
  223. }
  224. else if(isFollow == FollowType.False)
  225. {
  226. if (_Follower?.enabled == true)
  227. {
  228. _Follower.enabled = false;
  229. }
  230. }
  231. }
  232. private void SetDistance(float distance)
  233. {
  234. if (_Follower != null)
  235. {
  236. _Follower.WindowDistance = distance;
  237. }
  238. }
  239. private void SetDurationTime(float time)
  240. {
  241. if (_Effect != null)
  242. {
  243. _Effect.effectDurtion = time;
  244. }
  245. }
  246. private void SetIconTip(NoticeType type)
  247. {
  248. foreach (var item in ImageList)
  249. {
  250. if (item.gameObject.name == "IconTip")
  251. {
  252. switch (type)
  253. {
  254. case NoticeType.None:
  255. item.enabled = false;
  256. break;
  257. case NoticeType.Warning:
  258. item.enabled = true;
  259. item.color =new Color(1,0,0,0);
  260. item.sprite = Resources.Load<Sprite>("Sprites/warning");
  261. break;
  262. case NoticeType.Normal:
  263. item.enabled = true;
  264. item.color = new Color(1, 1, 1, 0);
  265. item.sprite = Resources.Load<Sprite>("Sprites/Normal");
  266. break;
  267. default:
  268. break;
  269. }
  270. }
  271. }
  272. }
  273. List<string> mainstrs = new List<string>();
  274. List<string> substrs = new List<string>();
  275. Coroutine multiple;
  276. public void AddStrsList(string mainstr,string substr)
  277. {
  278. mainstrs.Add(mainstr);
  279. substrs.Add(substr);
  280. }
  281. public void StartMultipleNotice(float time)
  282. {
  283. if (mainstrs != null && substrs !=null)
  284. {
  285. multiple = StartCoroutine(MultipleNotice(time, mainstrs, substrs));
  286. }
  287. }
  288. public void StopMultipleNotice()
  289. {
  290. if (multiple !=null)
  291. {
  292. mainstrs.Clear();
  293. substrs.Clear();
  294. StopCoroutine(multiple);
  295. }
  296. }
  297. IEnumerator MultipleNotice(float time,List<string> mainstrs,List<string> substrs)
  298. {
  299. int count =Mathf.Min(mainstrs.Count, substrs.Count);
  300. for (int i = 0; i < count; i++)
  301. {
  302. yield return null;
  303. SetMainText(mainstrs[i]);
  304. SetSubText(substrs[i]);
  305. StartNotice(time);
  306. yield return new WaitWhile(()=>_Effect.isEnable);
  307. }
  308. yield break;
  309. }
  310. }
  311. }