C# 为什么';What’没有播放所有的对话,而且从头到尾的顺序也不正确?

C# 为什么';What’没有播放所有的对话,而且从头到尾的顺序也不正确?,c#,unity3d,C#,Unity3d,为了进行测试,我在索引0和1处进行了两次对话。 我想让它播放第一个会话索引0,然后在播放完之后,开始播放索引1处的下一个会话 这是剧本的播放方法。第一个应逐个播放对话列表/数组,第二个应仅播放单个对话: 游戏对话和游戏对话 using System.Collections.Generic; using System.IO; using System.Linq; using UnityEditor; using UnityEngine; public class ConversationTrig

为了进行测试,我在索引0和1处进行了两次对话。 我想让它播放第一个会话索引0,然后在播放完之后,开始播放索引1处的下一个会话

这是剧本的播放方法。第一个应逐个播放对话列表/数组,第二个应仅播放单个对话:

游戏对话和游戏对话

using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;

public class ConversationTrigger : MonoBehaviour
{
    public List<Conversation> conversations = new List<Conversation>();
    public static List<int> conversationsToPlay = new List<int>();
    public bool conversationEnd;
    public GameObject canvas;
    public static int conversationIndex;

    private DialogueManager dialoguemanager;
    private string jsonPath;

    public void InitJsonPath()
    {
        jsonPath = Application.persistentDataPath + "/" + "Json.txt";
    }

    private void Start()
    {
        conversationIndex = 0;
        dialoguemanager = FindObjectOfType<DialogueManager>();
    }

    public IEnumerator PlayConversations()
    {
        canvas.SetActive(true);
        conversationEnd = false;
        var conversations = conversationsToPlay.ToArray(); // Copy the list
        conversationsToPlay.Clear(); // Immediately clear the original list

        for (int i = 0; i < conversations.Length; i++) // iterate over the array
        {
            // Now you also don't need to remove items anymore, 
            // since you already cleared the list
            yield return StartCoroutine(PlayConversation(conversations[i]));
        }
    }

    public IEnumerator PlayConversation(int index)
    {
        if (conversations.Count > 0 &&
            conversations[index].Dialogues.Count > 0)
        {
            for (int i = 0; i < conversations[index].Dialogues.Count; i++)
            {
                if (dialoguemanager != null)
                {
                    dialoguemanager.StartDialogue(conversations[index].Dialogues[i]);
                }

                while (DialogueManager.dialogueEnded == false)
                {
                    yield return null;
                }
            }

            conversationEnd = true;
            conversationIndex = index;
            canvas.SetActive(false);
            Debug.Log("Conversation Ended");
        }
    }

    public void SaveConversations()
    {
        string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
        File.WriteAllText(jsonPath, jsonTransform);
    }

    public void LoadConversations()
    {
        string jsonTransform = File.ReadAllText(jsonPath);
        conversations.Clear();
        conversations.AddRange(JsonHelper.FromJson<Conversation>(jsonTransform));
    }
}

但它首先开始播放索引1处的对话,然后只播放索引0处对话的一部分,然后重新开始,然后结束。

我认为您的问题与您实现
对话停止播放
方法的方式有关。每次调用该方法时,您都会启动一个新的协同程序,该程序将依次调用
PlayConversation
方法。这意味着,
ConversationToPlay
的每次调用都将在您传递的索引处播放对话,这就是它们重叠的原因

我能想到的最简单的解决方案是将协程的开始移动到
ConversationsToPlay
方法之外

大概是这样的:

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课堂游戏对话:单一行为
{
私有静态会话触发器会话触发器;
私有静态实例;
私人空间
{
conversationTrigger=GetComponent();
实例=此;
}
公共静态void AddConversationToPlay(int索引)
{
ConversationTrigger.conversationsToPlay.Add(索引);
}
公共静态无效StartPlayConversationsRoutine()
{
startcroutine(conversationTrigger.PlayConversations());
}
}
还有测试脚本

使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共课开始剪接场景:单一行为
{
公共德普霍菲尔德;
//每帧调用一次更新
无效更新()
{
如果(dephOfField.dephOfFieldFinished==true)
{
PlayConversations.AddConversationToPlay(0);
PlayConversations.AddConversationToPlay(1);
PlayConversations.StartPlayConversationsRoutine();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayConversations : MonoBehaviour
{
    private static ConversationTrigger conversationTrigger;
    private static PlayConversations instance;

    private void Awake()
    {
        conversationTrigger = GetComponent<ConversationTrigger>();
        instance = this;
    }

    public static void ConversationToPlay(int index)
    {
        ConversationTrigger.conversationsToPlay.Add(index);
        instance.StartCoroutine(conversationTrigger.PlayConversations());
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BeginningCutscene : MonoBehaviour
{
    public DepthOfField dephOfField;

    // Update is called once per frame
    void Update()
    {
        if (dephOfField.dephOfFieldFinished == true)
        {
            PlayConversations.ConversationToPlay(0);
            PlayConversations.ConversationToPlay(1);
        }
    }
}