12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class daziji : MonoBehaviour
- {
- public float charsPerSecond = 0.01f;//打字时间间隔
- private string words;//保存需要显示的文字
- private bool isActive = false;
- private float timer;//计时器
- private Text myText;
- private int currentPos = 0;//当前打字位置
- // Use this for initialization
- void Start()
- {
- Init();
- myText = GetComponent<Text>();
- words = myText.text;
- myText.text = "";//获取Text的文本信息,保存到words中,然后动态更新文本显示内容,实现打字机的效果
- }
- void Init()
- {
- timer = 0;
- isActive = true;
- charsPerSecond = Mathf.Max(0.01f, charsPerSecond);
-
- }
-
-
- // Update is called once per frame
- void Update()
- {
- OnStartWriter();
- }
- private void OnDisable()
- {
- Init();
- }
- public void StartEffect()
- {
- isActive = true;
- }
- /// <summary>
- /// 执行打字任务
- /// </summary>
- void OnStartWriter()
- {
- if (isActive)
- {
- timer += Time.deltaTime;
- if (timer >= charsPerSecond)
- {//判断计时器时间是否到达
- timer = 0;
- currentPos++;
- myText.text = words.Substring(0, currentPos);//刷新文本显示内容
- if (currentPos >= words.Length)
- {
- OnFinish();
- }
- }
- }
- }
- /// <summary>
- /// 结束打字,初始化数据
- /// </summary>
- void OnFinish()
- {
- isActive = false;
- timer = 0;
- currentPos = 0;
- myText.text = words;
- }
- }
|