using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
/// <summary>
/// 此脚本是能够将文本字符串随着时间打字或褪色显示。
/// </summary>
/// 此脚本是能够将文本字符串随着时间打字或褪色显示。
/// </summary>
[RequireComponent(typeof(Text))]
[AddComponentMenu("Typewriter Effect")]
public class TypewriterEffect : MonoBehaviour
{
#region 逐个出字
public UnityEvent myEvent;
public string writepoem;
public string write;
public string poemlast;
[HideInInspector]
public int shiindex;
private int writeindex;
private int writeindexpoem;
private int poemlastindex;
// public AudioClip mAudioClip; // 打字的声音,不是没打一个字播放一下,开始的时候播放结束就停止播放
private bool isActive = false;
private int timer;
private string words;
private Text mText;
private int lastindex=0;
private bool teachershowshi=true;
#endregion
public float alphaindex;
private string originColor;
private float alpha;
private float lastalpha;
void Start()
{
#region 逐个出字
if (myEvent == null)
myEvent = new UnityEvent();
words = GetComponent<Text>().text;
GetComponent<Text>().text = string.Empty;
timer =0;
mText = GetComponent<Text>();
writeindexpoem = words.IndexOf(writepoem);
writeindex = words.IndexOf(write);
poemlastindex = words.IndexOf(poemlast);
#endregion
originColor = ColorToHex(GetComponent<Text>().color);
}
[AddComponentMenu("Typewriter Effect")]
public class TypewriterEffect : MonoBehaviour
{
#region 逐个出字
public UnityEvent myEvent;
public string writepoem;
public string write;
public string poemlast;
[HideInInspector]
public int shiindex;
private int writeindex;
private int writeindexpoem;
private int poemlastindex;
// public AudioClip mAudioClip; // 打字的声音,不是没打一个字播放一下,开始的时候播放结束就停止播放
private bool isActive = false;
private int timer;
private string words;
private Text mText;
private int lastindex=0;
private bool teachershowshi=true;
#endregion
public float alphaindex;
private string originColor;
private float alpha;
private float lastalpha;
void Start()
{
#region 逐个出字
if (myEvent == null)
myEvent = new UnityEvent();
words = GetComponent<Text>().text;
GetComponent<Text>().text = string.Empty;
timer =0;
mText = GetComponent<Text>();
writeindexpoem = words.IndexOf(writepoem);
writeindex = words.IndexOf(write);
poemlastindex = words.IndexOf(poemlast);
#endregion
originColor = ColorToHex(GetComponent<Text>().color);
}
public void OnStart()
{
isActive = true;
}
void OnStartWriter()
{
if (isActive)
{
try
{
if (timer > writeindexpoem)
{
if (timer <writeindex)
{
mText.text = words.Substring(0, writeindex - 1) + "<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(writeindex - 1, 1) + "</color>" + "</size>";
alpha += alphaindex;
if (alpha >= 255)
{
timer = writeindex;
mText.text = words.Substring(0, timer)+"</size>";
alpha = 0;
}
}
else
{
if (timer >= poemlastindex)
{
mText.text = words.Substring(0, poemlastindex) + "<color=#" + originColor + Alpha(alpha) + ">"+words.Substring(poemlastindex, 1) + "</color>"+ "</size>";
alpha += alphaindex;
if (alpha >= 255)
{
timer = words.Length;
mText.text = words.Substring(0, timer-1);
alpha =255;
timer +=1;
}
}
else
{
mText.text = words.Substring(0, timer)+"<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(timer, 1) + "</color>" + "</size>";
alpha += alphaindex;
if (alpha >= 255)
{
timer += 1;
mText.text = words.Substring(0, timer) + "</size>";
alpha = 0;
}
}
}
}
else
{
mText.text = words.Substring(0, timer) + "<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(timer, 1) + "</color>";
alpha += alphaindex;
if (alpha >=255)
{
timer += 1;
mText.text = words.Substring(0, timer);
alpha =0;
}
}
shiindex = timer;
}
catch (Exception)
{
OnFinish();
}
}
}
{
if (isActive)
{
try
{
if (timer > writeindexpoem)
{
if (timer <writeindex)
{
mText.text = words.Substring(0, writeindex - 1) + "<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(writeindex - 1, 1) + "</color>" + "</size>";
alpha += alphaindex;
if (alpha >= 255)
{
timer = writeindex;
mText.text = words.Substring(0, timer)+"</size>";
alpha = 0;
}
}
else
{
if (timer >= poemlastindex)
{
mText.text = words.Substring(0, poemlastindex) + "<color=#" + originColor + Alpha(alpha) + ">"+words.Substring(poemlastindex, 1) + "</color>"+ "</size>";
alpha += alphaindex;
if (alpha >= 255)
{
timer = words.Length;
mText.text = words.Substring(0, timer-1);
alpha =255;
timer +=1;
}
}
else
{
mText.text = words.Substring(0, timer)+"<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(timer, 1) + "</color>" + "</size>";
alpha += alphaindex;
if (alpha >= 255)
{
timer += 1;
mText.text = words.Substring(0, timer) + "</size>";
alpha = 0;
}
}
}
}
else
{
mText.text = words.Substring(0, timer) + "<color=#" + originColor + Alpha(alpha) + ">" + words.Substring(timer, 1) + "</color>";
alpha += alphaindex;
if (alpha >=255)
{
timer += 1;
mText.text = words.Substring(0, timer);
alpha =0;
}
}
shiindex = timer;
}
catch (Exception)
{
OnFinish();
}
}
}
void OnFinish()
{
isActive = false;
timer =0;
GetComponent<Text>().text = words;
try
{
myEvent.Invoke();
}
catch (Exception)
{
Debug.Log("问题");
}
}
{
isActive = false;
timer =0;
GetComponent<Text>().text = words;
try
{
myEvent.Invoke();
}
catch (Exception)
{
Debug.Log("问题");
}
}
void Update()
{
if (teachershowshi)
{
OnStartWriter();
}
}
{
if (teachershowshi)
{
OnStartWriter();
}
}
private string ColorToHex(Color color)
{
int Val = 0;
Val |= Mathf.RoundToInt(color.r * 255f) << 24;
Val |= Mathf.RoundToInt(color.g * 255f) << 16;
Val |= Mathf.RoundToInt(color.b * 255f) << 8;
Val |= Mathf.RoundToInt(color.a * 255f);
return Val.ToString("x8").Substring(0, 6); //返回值去掉alpha值
}
{
int Val = 0;
Val |= Mathf.RoundToInt(color.r * 255f) << 24;
Val |= Mathf.RoundToInt(color.g * 255f) << 16;
Val |= Mathf.RoundToInt(color.b * 255f) << 8;
Val |= Mathf.RoundToInt(color.a * 255f);
return Val.ToString("x8").Substring(0, 6); //返回值去掉alpha值
}
private string Alpha (float number)
{
int a = 0;
a |= Mathf.RoundToInt(number);
return a.ToString("x8").Substring(6, 2);
}
}
{
int a = 0;
a |= Mathf.RoundToInt(number);
return a.ToString("x8").Substring(6, 2);
}
}