首先,因为再pokemon里,进入对话框后是没法做其他操作的,而且全局只有一个,所以这里用单例模式。
为了设计的组件化,博主把对话界面设计成prefab,然后在对话时再实例化
为了按照次序显示对话,用列表存储对话内容
点击查看代码
public class DialogData
public static DialogData instance = null; //对话框单例
List<string> allShowTexts = new List<string>(); //需要显示的所有文本
public delegate void OnCloseDialog();
public OnCloseDialog onCloseDialog = null;
public static DialogData GetInstance()
if (instance == null)
instance = new DialogData();
return instance;
public static List<string> GetTextList()
return GetInstance().allShowTexts;
public static void SetTextList(List<string> textList)
GetInstance().allShowTexts = textList;
public static void CloseDialog()
if (GetInstance().onCloseDialog != null)
GetInstance().onCloseDialog();
GetInstance().ClearData();
public void ClearData()
GetInstance().allShowTexts = new List<string>();
onCloseDialog = null;
然后在创建和关闭的时候,需要截获和放开按键操作(这里的按键操作是博主自己实现的控制,因为比较简单就不放出了)
private void OnDisable()
SystemControl.SetBanMove(false);
LineShowFinish(); //当脚本在失活的时候,将数据进行重置
DialogData.CloseDialog();
private void OnEnable()
SystemControl.SetBanMove(true);
按次序显示用的是定时器,即在update中不断刷新,计算是否需要显示下一个字符。
为了方便判断【是否完整显示改行】,博主单独用一个函数封装了完整显示的操作(虽然这样会导致drawcall增加-_-||)
void OnStartWriter()
if (isActive)
timer += Time.deltaTime;
if (timer >= charsPerSecond)//判断计时器时间是否到达
lineShowFinall = false;
timer = 0;
currentPos++; //这里其实还可以做一个改良,可以检测一个input用户输入,如果输入了,则让currentPos = words.Length,这样可以实现按下按键,马上就显示完毕
myText.text = nowShowText.Substring(0, currentPos); //刷新文本显示内容
if (currentPos >= nowShowText.Length)
lineShowFinall = true;
LineShowFinish();
void LineShowFinish()
isActive = false;
myText.text = nowShowText;
最后,就是显示下一行。这里就很简单了,将下一行的数据填充到prefab里,如果没有下一行销毁自身即可
在pokemon中,右下角会显示一个弹跳的光标。这里我们用正弦函数控制位置即可
void OnIconJump()
nowPos.y = Mathf.Abs(Mathf.Sin(Time.fixedTime * Mathf.PI * HZ)) * zhenFu + startPos.y;
nextIcon.transform.position = nowPos;
Tip.cs(绑定在上面的prefab)
点击查看代码
public class Dialog : MonoBehaviour
float charsPerSecond = 0.05f; //打字时间间隔
string nowShowText; //现在显示的文字
List<string> allShowTexts = new List<string>(); //需要显示的所有文本
int nowShowIdx = -1; //当前显示到行数
bool showAllFinall = false; // 所有文本显示完毕
bool isActive = false; //判断是否开始输出
float timer; //计时器
Text myText; //获取身上的test脚本
int currentPos = 0; //当前打字位置
bool lineShowFinall = false; // 是否显示完当前语句
GameObject nextIcon; // 下一条的箭头
Vector3 startPos; //记录原位置
Vector2 nowPos; //简写运动变化的位置
float zhenFu = 0.2f; //振幅
float HZ = 1f; //频率
private void OnDisable()
SystemControl.SetBanMove(false);
LineShowFinish(); //当脚本在失活的时候,将数据进行重置
DialogData.CloseDialog();
private void OnEnable()
SystemControl.SetBanMove(true);
void Start()
Debug.Log("start dialogs");
allShowTexts = DialogData.GetTextList();
if (allShowTexts.Count <= 0)
showAllFinall = true;
GameObject.Destroy(gameObject, 0);
timer = 0;
isActive = true;
charsPerSecond = Mathf.Max(0.02f, charsPerSecond); //将最小的出字速度限制为0.02,也可以自行调整
myText = GameObject.Find("Dialog").GetComponent<Text>();
NextLineShowStart(); //开始显示第一行
nextIcon = GameObject.Find("Next");
startPos = nextIcon.transform.position;
nowPos = startPos;
void Update()
//点击确认或者取消键,马上完整显示
if (SystemControl.Confirm() || SystemControl.Cancel())
if (!lineShowFinall)
lineShowFinall = true;
LineShowFinish();
NextLineShowStart();
OnStartWriter();
OnIconJump();
void OnStartWriter()
if (isActive)
timer += Time.deltaTime;
if (timer >= charsPerSecond)//判断计时器时间是否到达
lineShowFinall = false;
timer = 0;
currentPos++; //这里其实还可以做一个改良,可以检测一个input用户输入,如果输入了,则让currentPos = words.Length,这样可以实现按下按键,马上就显示完毕
myText.text = nowShowText.Substring(0, currentPos); //刷新文本显示内容
if (currentPos >= nowShowText.Length)
lineShowFinall = true;
LineShowFinish();
void LineShowFinish()
isActive = false;
myText.text = nowShowText;
void NextLineShowStart()
timer = 0;
currentPos = 0;
nowShowIdx++;
if (nowShowIdx < allShowTexts.Count)
showAllFinall = false;
lineShowFinall = false;
nowShowText = allShowTexts[nowShowIdx];
isActive = true;
myText.text = "";
showAllFinall = true;
Debug.Log("close dialogs");
GameObject.Destroy(gameObject, 0);
return;
//Debug.Log("nowShowIdx: " + nowShowIdx + ", showAllFinall: " + showAllFinall);
/// <summary>
/// 箭头跳动
/// </summary>
void OnIconJump()
nowPos.y = Mathf.Abs(Mathf.Sin(Time.fixedTime * Mathf.PI * HZ)) * zhenFu + startPos.y;
nextIcon.transform.position = nowPos;
public class DialogData
public static DialogData instance = null; //对话框单例
List<string> allShowTexts = new List<string>(); //需要显示的所有文本
public delegate void OnCloseDialog();
public OnCloseDialog onCloseDialog = null;
public static DialogData GetInstance()
if (instance == null)
instance = new DialogData();
return instance;
public static List<string> GetTextList()
return GetInstance().allShowTexts;
public static void SetTextList(List<string> textList)
GetInstance().allShowTexts = textList;
public static void CloseDialog()
if (GetInstance().onCloseDialog != null)
GetInstance().onCloseDialog();
GetInstance().ClearData();
public void ClearData()
GetInstance().allShowTexts = new List<string>();
onCloseDialog = null;
void CreateDeveloperTips()
List<string> allShowTexts = new List<string>();
allShowTexts.Add("您好!欢迎来到aquam的游戏开发世界。");
allShowTexts.Add("下面介绍该游戏的操作方式。键盘的方向键控制人物的移动。");
allShowTexts.Add("Z键确认, X键取消。");
allShowTexts.Add("祝您游戏愉快!");
DialogData.SetTextList(allShowTexts);
DialogData.GetInstance().onCloseDialog = CloseDialogCB;
Instantiate(prefab, dialogPos, Quaternion.Euler(0, 0, 0));
isOpenTip = true;