使用TimeLine中的ControlTime,绑定需要对话的预制体。通过继承ITimeControl的三个重写方法。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Timeline;
using UnityEngine.Playables;//TimeLine导演编辑
public class TalkMgr : MonoBehaviour,ITimeControl
{
int count = 0;
public PlayableDirector pp;
public GameObject talkobj;
string[] content = { "??????", "!!!!!!!" };
public void OnControlTimeStart()
{
if (Application.isPlaying)
{
talkobj.name = "Talk";
talkobj.transform.GetChild(1).GetComponent<Text>().text = "阿盛";
}
}
public void OnControlTimeStop()
{
if (Application.isPlaying)
{
if (count == 0)
{
pp.time = 22;
pp.Play();
count = 1;
}
}
}
public void SetTime(double time)
{
if (Application.isPlaying)
{
if ((int)time * 3 <= content[count].Length)
{
talkobj.transform.GetChild(0).GetComponent<Text>().text = conten t[count].Substring(0, (int)time * 3);
}
else
{
talkobj.transform.GetChild(0).GetComponent<Text>().text = conten t[count];
}
}
}
// Start is called before the first frame update
void Start()
{
}
}
三个方法
OnControlTimeStart()顾名思义也就是当TimeLine播放到IControlTime开始的时候执行调用的方法只调用一次与Start方法相同,
SetTime也就是在这段时间段中根据TimeLine 的播放走向而执行的,
OnControlTimeStop() 则是播放到最后结束时所调用的方法
而我在SetTime里写的则是通过判断字符串的大小来实现说话打字机的效果让说出去的话一个个字的进行显示。