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