123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167 |
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System.Text;
- /// <summary>
- /// 教学提示UI控制
- /// </summary>
- 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<RectTransform>();
- _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;
- }
- /// <summary>
- /// 边框控制
- /// </summary>
- 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();
- }
- }
- }
- /// <summary>
- /// 设置显示文本
- /// </summary>
- /// <param name="text"></param>
- 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;
- }
- }
- }
|