public float showTime = 4;
private float showTimer;
// Start is called before the first frame update
void Start()
tipImage.SetActive(true);
dialogImage.SetActive(false);
showTimer = -1;
// Update is called once per frame
void Update()
showTimer -= Time.deltaTime;
if(showTimer<0)
tipImage.SetActive(true);
dialogImage.SetActive(false);
public void ShowDialog()
//调用PlayerControl脚本的标记,判断任务是否完成,完成则修改文本
if(PlayerControl.showflag==1)
text.text= "青蛙先生:\n任务已完成,你现在可以进入城堡了!";
showTimer = showTime;
dialogImage.SetActive(true);
tipImage.SetActive(false);
此外,还有两种显示方案
一、把NPC设置为触发器,当玩家走入预定区域,显示对话框
private void OnTriggerEnter2D(Collider2D other)
ShowDialog();
二、设置为不触发,但是玩家与NPC碰撞,则显示对话框
private void OnCollisionEnter2D(Collision2D collision)
ShowDialog();
附完整教程:
Unity2d Rubys Adventure 课程设计报告
https://blog.csdn.net/weixin_43673589