using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Text; /// /// 教学提示UI控制 /// public class LearningUIController : MonoBehaviour { private static LearningUIController _instance; public static LearningUIController Instance { get { if (_instance == null) { Debug.Log("未实例化LearningUIController"); } return _instance; } } public Canvas _canvas; public Image _frameImg;//边框 public Image _backImg;//背景 public Text _showText;//提示框 private StringBuilder _stringBuilder = new StringBuilder(); private string _stringInfo; public RectTransform _maskRectTrans;//遮罩层 public Vector2 _closeSize; public Vector2 _openSize; public Vector2 _maskCloseSize; public Vector2 _maskOpenSize; public float _moveTime = 1; private float _moveSpeed; private float _maskMoveSpeed; private RectTransform _frameRectTrans; private bool _open = false; private bool _moving = false; private bool _textShowing = false; private char[] _textChar; private int _currentTextCount; public delegate void UIEvent(); public UIEvent OpenFinished; public UIEvent CloseFinished; private void Awake() { _instance = this; } // Use this for initialization void Start() { _frameRectTrans = _frameImg.GetComponent(); _moveSpeed = Vector3.Distance(_openSize, _closeSize) / _moveTime; _maskMoveSpeed = Vector3.Distance(_maskOpenSize, _maskCloseSize) / _moveTime; Init(); } // Update is called once per frame void Update() { UIMoving(); ShowText(); if (Input.GetKeyDown(KeyCode.Space)) { SetLearnText("测试参数设置", !_open); } } public void Init() { _frameRectTrans.sizeDelta = _closeSize; _maskRectTrans.sizeDelta = _maskCloseSize; _open = true; } public void SetLearningState(bool state) { if (!_open) { _canvas.gameObject.SetActive(true); } _open = state; _moving = true; } public void SetLearnText(string text, bool state) { if (!_open) { _canvas.gameObject.SetActive(true); } SetText(text); _open = state; _moving = true; } /// /// 边框控制 /// public void UIMoving() { if (!_moving) return; _frameRectTrans.sizeDelta = Vector2.MoveTowards(_frameRectTrans.sizeDelta, _open ? _openSize : _closeSize, _moveSpeed * Time.deltaTime); _maskRectTrans.sizeDelta = Vector2.MoveTowards(_maskRectTrans.sizeDelta, _open ? _maskOpenSize : _maskCloseSize, _maskMoveSpeed * Time.deltaTime); if (_frameRectTrans.sizeDelta == (_open ? _openSize : _closeSize)) { _moving = false; _canvas.gameObject.SetActive(_open); if (!_open && CloseFinished != null) { CloseFinished(); } else if (_open && OpenFinished != null) { OpenFinished(); } } } /// /// 设置显示文本 /// /// public void SetText(string text) { if (text == _stringInfo) return; _stringInfo = text; _textShowing = true; _stringBuilder = new StringBuilder(); _textChar = _stringInfo.ToCharArray(); _currentTextCount = 0; } void ShowText() { if (!_textShowing) return; _stringBuilder.Append(_textChar[_currentTextCount]); _showText.text = _stringBuilder.ToString(); _currentTextCount++; if (_currentTextCount == (_textChar.Length)) { _textShowing = false; } } }