【发布时间】:2019-12-18 01:33:57
【问题描述】:
我制作了一个对话脚本,这样我就可以在不同的 NPC 上显示句子。 我还添加了一个继续按钮,以便从一个句子到另一个句子。
问题是由于某种原因,如果我在播放完所有句子后进行交互,最后一个对话框会卡住并继续重播。
我想要重播我添加到 NPC 上的所有句子。
我还想问是否有办法不使用继续按钮?并在一定时间后让文本自行输出。就像真正的对话一样。
如您所见,我在右侧添加了行
打印的第一行
打印第二行
第三行打印
每次我交互时都开始将最后一行背靠背放置除非我点击按钮,因为那样它就会清除。但它仍然每次都重播最后一句话,而不是回到第一句话。
对话脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class Dialogue2 : MonoBehaviour
{
public GameObject dialogBox; // Attach ui image to this
public Text npcname; // Attach UI text gameObject
public Text dialogText; // Attach UI text gameObject
public string NPC; // What is he/she called?
public string[] sentences;
public bool playerInRange; // Is the Player in range?
public float typingSpeed;
private int index;
public GameObject continueButton;
public AudioSource SignUpSfx;
public AudioSource SignDownSfx;
void Update()
{
if(dialogText.text == sentences[index]){
continueButton.SetActive(true);
}
// Player in range and E is hit
if(Input.GetKeyDown(KeyCode.E) && playerInRange)
{
if(dialogBox.activeInHierarchy)
{
dialogBox.SetActive(false);
SignDownSfx.Play();
}
else
{
dialogBox.SetActive(true);
StopAllCoroutines();
StartCoroutine(Type());
npcname.text = NPC;
SignUpSfx.Play();
}
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if(other.CompareTag("Entity"))
{
playerInRange = true;
}
}
private void OnTriggerExit2D(Collider2D other)
{
if(other.CompareTag("Entity") && dialogBox.activeInHierarchy == true)
{
SignDownSfx.Play();
}
if(other.CompareTag("Entity"))
{
playerInRange = false;
dialogBox.SetActive(false);
}
}
IEnumerator Type(){
foreach(char letter in sentences[index].ToCharArray()){
dialogText.text += letter;
yield return new WaitForSeconds(typingSpeed);
}
}
public void NextSentence(){
continueButton.SetActive(false);
if(index < sentences.Length - 1){
index++;
dialogText.text = "";
StartCoroutine(Type());
} else {
dialogText.text = "";
continueButton.SetActive(false);
}
}
}
【问题讨论】:
-
您应该在
else块内重置index吗? -
@Johnny 如果我在 else 块中重置索引,我会收到以下错误“返回 void,return 关键字后面不能跟对象表达式”