【问题标题】:Dialogue keeps being stuck at last sentence对话一直卡在最后一句话
【发布时间】: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 关键字后面不能跟对象表达式”

标签: c# unity3d


【解决方案1】:

你就不能换个方式:

if(index < sentences.Length - 1){
        index++;
        dialogText.text = "";
        StartCoroutine(Type());
      } else {
        dialogText.text = "";
        continueButton.SetActive(false);

      }

为了这个?

if(index < sentences.Length - 1){
        index++;
        dialogText.text = "";
        StartCoroutine(Type());
      } else {
        dialogText.text = "";
        continueButton.SetActive(false);
        index = 0;
      }

【讨论】:

  • 最后一个问题。在我完成所有 2 或 3 句话后,如果我不按继续,它似乎不会重置。它要我按继续转到空白行。有没有办法随时重置对话?即使我把它放在第 1 行?
  • 什么意思?你能举个例子吗?
  • 嗯,发生的事情与上一张图片完全相同。如果我与 npc 交互,然后我再次按 e(交互)它会关闭对话框。但是当我再次交互时,它会背靠背复制第一个书面文本。我想要实现的是每次交互时(对话框打开和关闭)我希望线条重置回到开头
  • 对于这种情况,您需要在 Update 方法中重置索引。 if(dialogBox.activeInHierarchy) { index = 0; dialogBox.SetActive(false); SignDownSfx.Play(); }
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-16
  • 2017-10-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多