LearningUIController.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System.Text;
  6. /// <summary>
  7. /// 教学提示UI控制
  8. /// </summary>
  9. public class LearningUIController : MonoBehaviour
  10. {
  11. private static LearningUIController _instance;
  12. public static LearningUIController Instance
  13. {
  14. get
  15. {
  16. if (_instance == null)
  17. {
  18. Debug.Log("未实例化LearningUIController");
  19. }
  20. return _instance;
  21. }
  22. }
  23. public Canvas _canvas;
  24. public Image _frameImg;//边框
  25. public Image _backImg;//背景
  26. public Text _showText;//提示框
  27. private StringBuilder _stringBuilder = new StringBuilder();
  28. private string _stringInfo;
  29. public RectTransform _maskRectTrans;//遮罩层
  30. public Vector2 _closeSize;
  31. public Vector2 _openSize;
  32. public Vector2 _maskCloseSize;
  33. public Vector2 _maskOpenSize;
  34. public float _moveTime = 1;
  35. private float _moveSpeed;
  36. private float _maskMoveSpeed;
  37. private RectTransform _frameRectTrans;
  38. private bool _open = false;
  39. private bool _moving = false;
  40. private bool _textShowing = false;
  41. private char[] _textChar;
  42. private int _currentTextCount;
  43. public delegate void UIEvent();
  44. public UIEvent OpenFinished;
  45. public UIEvent CloseFinished;
  46. private void Awake()
  47. {
  48. _instance = this;
  49. }
  50. // Use this for initialization
  51. void Start()
  52. {
  53. _frameRectTrans = _frameImg.GetComponent<RectTransform>();
  54. _moveSpeed = Vector3.Distance(_openSize, _closeSize) / _moveTime;
  55. _maskMoveSpeed = Vector3.Distance(_maskOpenSize, _maskCloseSize) / _moveTime;
  56. Init();
  57. }
  58. // Update is called once per frame
  59. void Update()
  60. {
  61. UIMoving();
  62. ShowText();
  63. if (Input.GetKeyDown(KeyCode.Space))
  64. {
  65. SetLearnText("测试参数设置", !_open);
  66. }
  67. }
  68. public void Init()
  69. {
  70. _frameRectTrans.sizeDelta = _closeSize;
  71. _maskRectTrans.sizeDelta = _maskCloseSize;
  72. _open = true;
  73. }
  74. public void SetLearningState(bool state)
  75. {
  76. if (!_open)
  77. {
  78. _canvas.gameObject.SetActive(true);
  79. }
  80. _open = state;
  81. _moving = true;
  82. }
  83. public void SetLearnText(string text, bool state)
  84. {
  85. if (!_open)
  86. {
  87. _canvas.gameObject.SetActive(true);
  88. }
  89. SetText(text);
  90. _open = state;
  91. _moving = true;
  92. }
  93. /// <summary>
  94. /// 边框控制
  95. /// </summary>
  96. public void UIMoving()
  97. {
  98. if (!_moving) return;
  99. _frameRectTrans.sizeDelta = Vector2.MoveTowards(_frameRectTrans.sizeDelta,
  100. _open ? _openSize : _closeSize, _moveSpeed * Time.deltaTime);
  101. _maskRectTrans.sizeDelta = Vector2.MoveTowards(_maskRectTrans.sizeDelta,
  102. _open ? _maskOpenSize : _maskCloseSize, _maskMoveSpeed * Time.deltaTime);
  103. if (_frameRectTrans.sizeDelta == (_open ? _openSize : _closeSize))
  104. {
  105. _moving = false;
  106. _canvas.gameObject.SetActive(_open);
  107. if (!_open && CloseFinished != null)
  108. {
  109. CloseFinished();
  110. }
  111. else if (_open && OpenFinished != null)
  112. {
  113. OpenFinished();
  114. }
  115. }
  116. }
  117. /// <summary>
  118. /// 设置显示文本
  119. /// </summary>
  120. /// <param name="text"></param>
  121. public void SetText(string text)
  122. {
  123. if (text == _stringInfo) return;
  124. _stringInfo = text;
  125. _textShowing = true;
  126. _stringBuilder = new StringBuilder();
  127. _textChar = _stringInfo.ToCharArray();
  128. _currentTextCount = 0;
  129. }
  130. void ShowText()
  131. {
  132. if (!_textShowing) return;
  133. _stringBuilder.Append(_textChar[_currentTextCount]);
  134. _showText.text = _stringBuilder.ToString();
  135. _currentTextCount++;
  136. if (_currentTextCount == (_textChar.Length))
  137. {
  138. _textShowing = false;
  139. }
  140. }
  141. }