using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ItemAction : MonoBehaviour
{
public camMouseLook mouselook;
public GameObject lockedRoomCamera;
public Camera playerCamera;
public GameObject navi;
public ConversationTrigger trigger;
public GameObject player;
private Vector3 playerposition;
private Quaternion playerrotation;
private bool torotate = false;
public void Init()
{
navi.transform.parent = null;
navi.transform.localScale = new Vector3(0.5f, 0.5f, 0.5f);
navi.transform.LookAt(lockedRoomCamera.transform);
PlayerController.disablePlayerController = true;
mouselook.enabled = false;
playerCamera.enabled = false;
lockedRoomCamera.SetActive(true);
torotate = true;
playerposition = player.transform.position;
playerrotation = player.transform.rotation;
}
private void Update()
{
if(torotate == true)
{
RotateRandom.RandomRotation(navi.transform);
PlayConversations.ConversationToPlay(3);
}
}
}
问题在于它多次调用此行:
PlayConversations.ConversationToPlay(3);
我不介意它会被调用很多次,但是我希望它只在示例索引3中播放一次。如果它开始播放索引,请记住在这种情况下它开始播放并播放了索引3,即使它会再次在Update over中调用此行,也不要再播放它。
这是PlayConversations脚本:
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());
}
}
还有ConversationTrigger脚本:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEditorInternal;
public class ConversationTrigger : MonoBehaviour
{
public List<Conversation> conversations = new List<Conversation>();
public static List<int> conversationsToPlay = new List<int>();
[HideInInspector]
public GameObject canvas;
[HideInInspector]
public bool conversationEnd;
[HideInInspector]
public static int conversationIndex;
private DialogueManager dialoguemanager;
private void Start()
{
conversationIndex = 0;
dialoguemanager = FindObjectOfType<DialogueManager>();
}
public IEnumerator PlayConversations()
{
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;
}
}
conversationIndex = index;
conversationEnd = true;
canvas.SetActive(false);
Debug.Log("Conversation Ended");
}
}
public void SaveConversations()
{
string jsonTransform = JsonHelper.ToJson(conversations.ToArray(), true);
File.WriteAllText(@"d:\json.txt", jsonTransform);
}
public void LoadConversations()
{
string jsonTransform = File.ReadAllText(@"d:\json.txt");
conversations.Clear();
conversations.AddRange(JsonHelper.FromJson<Conversation>(jsonTransform));
}
}
无论是在PlayConversations脚本中还是在ConversationTrigger中,使它在开始播放对话时都不会再次播放此对话。
问题是游戏中我正在玩对话的每个位置,在这种情况下,我需要使用标志来使其在开始玩后停止,但是在这种情况下,例如在对话进行时,我也在旋转对象,该对象应该在Update中并被多次调用:
RotateRandom.RandomRotation(navi.transform);
但是对话只能开始一次。
由于我没有看到在游戏中进行两次相同对话的原因,因此我希望该对话只能进行一次,而无需在每个位置使用标志。