using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System;
/// <summary>
/// 此脚本是能够将文本字符串随着时间打字或褪色显示。
/// </summary>
[RequireComponent(typeof(Text))]
[AddComponentMenu("UGUI Writer Effect")]
public class WriterEffect : MonoBehaviour
{
public UnityEvent myEvent;
//打字速度
public int charsPerSecond = 3;
// public AudioClip mAudioClip; // 打字的声音,不是没打一个字播放一下,开始的时候播放结束就停止播放
private bool isActive = false;
private float timer;
private string words;
private Text mText;
void Start()
{
if (myEvent == null)
myEvent = new UnityEvent();
mText = GetComponent<Text>();
words = mText.text;
mText.text = string.Empty;
timer = 0;
isActive = true;
charsPerSecond = Mathf.Max(1, charsPerSecond);
}
void ReloadText()
{
words = GetComponent<Text>().text;
mText = GetComponent<Text>();
}
public void OnStart()
{
ReloadText();
isActive = true;
}
void OnStartWriter()
{
if (isActive)
{
try
{
mText.text = words.Substring(0, (int)(charsPerSecond * timer));
timer += Time.deltaTime;
}
catch (Exception)
{
OnFinish();
}
}
}
void OnFinish()
{
isActive = false;
timer = 0;
GetComponent<Text>().text = words;
try
{
myEvent.Invoke();
}
catch (Exception)
{
Debug.Log("问题");
}
}
void Update()
{
OnStartWriter();
}
}
UGUI 打字机效果
最新推荐文章于 2024-07-23 18:36:32 发布