我的游戲中有一個對話系統,我最近發現了如何通過使用協程來逐個添加對話的字母。我想跳過點擊螢屏時添加字母影片,然后立即完成對話。
我這里有一個協程,它回圈遍歷句子或字串的字母,并將其顯示在我的對話文本物件中。我找不到用于立即完成協程的特定關鍵字。有沒有一種方法可以跳過或立即完成協程,以便在單擊對話或單擊螢屏時立即完成對話?
顯示對話和協程的代碼:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using System;
public class InformantDialogue : MonoBehaviour
{
// NPC DATA
private Informant informantJson;
[SerializeField] TextAsset characterData;
// DIALOG UI
[SerializeField] GameObject dialogBox, article;
[SerializeField] TextMeshProUGUI dialogue;
// DIALOG CYCLE VARIABLES
private bool clickEnable;
private int dialogId, multiDialogCycle;
public int progress;
string[] multiDialog;
private void OnEnable()
{
setJSON();
loadCharacterData();
}
void Start()
{
dialogId = 1;
}
void Update()
{
if (Input.GetMouseButtonDown(0) && clickEnable == true)
{
Debug.Log(dialogId);
if (multiDialog.Length > 1)
{
if (multiDialogCycle == multiDialog.Length - 1)
{
closeDialog();
progressDialog();
}
else
{
multiDialogCycle ;
loadCharacterData();
}
}
else
{
closeDialog();
progressDialog();
}
}
}
public void loadCharacterData()
{
// DIALOGUE
multiDialog = getIDialog(dialogId).dialog_message.Split('#');
if (multiDialogCycle == 4)
{
article.SetActive(true);
}
if (multiDialog.Length == 1)
{
//dialogue.text = getIDialog(dialogId).dialog_message;
StopAllCoroutines();
StartCoroutine(TypeSentence(getIDialog(dialogId).dialog_message));
}
else if (multiDialogCycle < multiDialog.Length)
{
//dialogue.text = multiDialog[multiDialogCycle];
StopAllCoroutines();
StartCoroutine(TypeSentence(multiDialog[multiDialogCycle]));
clickEnable = true;
}
}
// INFORMANT DIALOGUE GETTER
public InformantDialog getIDialog(int dialogId)
{
foreach (InformantDialog dialog in informantJson.informant_dialogs)
{
if (dialog.id == dialogId)
{
return dialog;
}
}
return informantJson.informant_dialogs[0];
}
IEnumerator TypeSentence(string sentence)
{
dialogue.text = "";
foreach(char letter in sentence.ToCharArray())
{
dialogue.text = letter;
//yield return null;
yield return new WaitForSeconds(0.01f);
}
}
private void showDialogue()
{
dialogBox.SetActive(true);
}
private void closeDialog()
{
dialogBox.SetActive(false);
clickEnable = false;
multiDialogCycle = 0;
}
private void setJSON()
{
if (progress == 0)
{
characterData = Resources.Load<TextAsset>("JSON/Informant");
}else if (progress == 1)
{
characterData = Resources.Load<TextAsset>("JSON/Informant1");
}
else if (progress == 2)
{
characterData = Resources.Load<TextAsset>("JSON/Informant2");
} else
{
clickEnable = false;
}
informantJson = JsonUtility.FromJson<Informant>(characterData.text);
}
private void progressDialog()
{
if (dialogId == informantJson.informant_dialogs.Length)
{
dialogId = 0;
progress ;
}
dialogId ;
}
}
uj5u.com熱心網友回復:
IEnumerator TypeSentence(string sentence)
{
boolThatStartFalseAndWillBeTrueWhenPlayerClickOnScreen = false;
dialogue.text = "";
foreach(char letter in sentence.ToCharArray())
{
if (boolThatStartFalseAndWillBeTrueWhenPlayerClickOnScreen)
{
dialogue.text = sentence;
break;
}
dialogue.text = letter;
//yield return null;
yield return new WaitForSeconds(0.01f);
}
}
uj5u.com熱心網友回復:
我已經將我的方法添加到協同程式中。我更喜歡保留協程的句柄,并在協程結束時將其置空。因此你可以只重啟那個協程。FinishTypingSentence()停止它并將整個文本設定在應有的位置。
string typedSentence;
private IEnumerator _typeSentenceCoroutine;
//Caching this will save some memory allocation.
WaitForSeconds waitFor01Seconds = new WaitForSeconds(0.01f);
void StartTypingSentence()
{
if (_typeSentenceCoroutine != null) StopCoroutine(_typeSentenceCoroutine);
_typeSentenceCoroutine = TypeSentence();
StartCoroutine(_typeSentenceCoroutine);
}
public void FinishTypingSentence()
{
if (_typeSentenceCoroutine != null) StopCoroutine(_typeSentenceCoroutine);
_typeSentenceCoroutine = null;
dialogue.text = typedSentence;
}
IEnumerator TypeSentence()
{
dialogue.text = "";
foreach (char letter in typedSentence.ToCharArray())
{
dialogue.text = letter;
yield return waitFor01Seconds;
}
_typeSentenceCoroutine = null;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/533588.html
標籤:C#unity3d协程
