Unity对话系统,可实现场景中的多个对话

问题描述 投票:0回答:1

基本上我的玩家将走过场景,在某些特定点他将与触发对话的对撞机发生碰撞,它实际上是一个文本框,显示一些提示以传递到下一个级别(我知道这不完全是对话,但我希望你能理解)。所以我使用了 Brackets 的这个教程来构建对话系统。问题是,如果我必须在场景中放置多个点,它将不起作用,它只会对我碰撞的第一个点起作用,或者它将显示我尚未碰撞的其他点的另一个文本。我还没有在 Youtube 上找到关于场景中多个对话的其他教程。 教程链接

ColliderDialogue.cs 附加到 Canvas 和点碰撞器:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColliderDialogue : MonoBehaviour
{
    public GameObject DialogueBox;
    DialogueManager dialogueManager;
    DialogueTrigger dialogueTrigger;
    public bool PlayingDialogue = false;

    private void Awake()
    {
        dialogueManager = FindObjectOfType<DialogueManager>();
        dialogueTrigger = FindObjectOfType<DialogueTrigger>();
    }

    // Update is called once per frame
    private void Start()
    {
        DialogueBox.SetActive(false);
    }

    private void Update()
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            dialogueManager.DisplayNextSentence();
        }
        if(dialogueManager.playingDialogue == true)
        {
            PlayingDialogue = true;
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            DialogueBox.SetActive(true);
            dialogueTrigger.TriggerDialogue();
            Destroy(gameObject);
        }
    }

DialogueManager.cs 附加到空游戏对象“DialogueManager”:

    using System.Collections;
using System.Collections.Generic;
using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class DialogueManager : MonoBehaviour
{
    public GameObject DialogueBox;
    public TextMeshProUGUI nameText;
    public TextMeshProUGUI dialogueText;
    private Queue<string> sentences;

    public bool playingDialogue = false;

    public bool endDialogueBox = false;
    void Start()
    {
        sentences = new Queue<string>();
    }

    public void StartDialogue (Dialogue dialogue)
    {
        playingDialogue=true;

        nameText.text = dialogue.name;

        sentences.Clear();

        foreach (string sentence in dialogue.sentences)
        {
            sentences.Enqueue(sentence);
        }

        Time.timeScale = 0;

        DisplayNextSentence();
    }

    public void DisplayNextSentence()
    {
        if(sentences.Count == 0)
        {
            EndDialogue();
            return;
        }

        string sentence = sentences.Dequeue();
        StopAllCoroutines();
        StartCoroutine(TypeSentence(sentence));
    }

    IEnumerator TypeSentence(string sentence)
    {
        dialogueText.text = "";
        foreach (char letter in sentence.ToCharArray())
        {
            dialogueText.text += letter;
            yield return null;
        }

    }

    public void EndDialogue()
    {
        Debug.Log("End of conversation");
        DialogueBox.SetActive(false);
        Time.timeScale = 1;
        endDialogueBox = true;
    }

}

对话.cs:

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[System.Serializable]
public class Dialogue 
{
    public string name;
    [TextArea(3, 10)]
    public string[] sentences;
}

**DialogueTrigger.cs attached to point collider:**

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DialogueTrigger : MonoBehaviour
{
    public Dialogue dialogue;

    public void TriggerDialogue()
    {
        FindObjectOfType<DialogueManager>().StartDialogue(dialogue);
        
    }
}
c# unity-game-engine 2d
1个回答
0
投票

我认为你的主要问题在于你的 ColliderDialogue 类,其中你的DialogueTrigger 由 FindObjectOfType 函数填充,但该函数根据你的场景返回一个实例,如果你有多个实例,它可能在你的所有场景中都是相同的对象,并且确保进入 DialogueManager 的数据将是相同的,因此我建议您的变量DialogueTrigger应该使用检查器填充,使其公开,或者添加[SerializeField](如果您需要它私有),然后拖放正确的一个。

© www.soinside.com 2019 - 2024. All rights reserved.