Untitled
unknown
plain_text
4 years ago
3.1 kB
9
Indexable
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using Ink.Runtime;
public class DialogueManager : MonoBehaviour
{
    [Header("Params")]
    [SerializeField] private float typingSpeed = 0.04f;
    [SerializeField] private GameObject continueIcon;
    [Header("Dialogue UI")]
    [SerializeField] private GameObject dialoguePanel;
    [SerializeField] private TextMeshProUGUI dialogueText;
    private Story currentStory;
    public bool dialogueIsPlaying;
    public bool canContinueToNextLine = false;
    private Coroutine displayLineCoroutine;
    private static DialogueManager instance;
    private void Awake()
    {
        if (instance != null)
        {
            Debug.LogWarning("Encontrado mas de un dialogue manager en la escena");
        }
        instance = this;
    }
    public static DialogueManager GetInstance()
    {
        return instance;
    }
    private void Start()
    {
        dialogueIsPlaying = false;
        dialoguePanel.SetActive(false);
    }
    private void Update()
    {
        //return right away if dialogue isnt playing
        if (dialogueIsPlaying == false || canContinueToNextLine == false)
        {
            return;
        }
        if (Input.GetKeyDown(KeyCode.F))
        {
            Debug.Log("Manager");
            ContinueStory();
        }
        //handle continuing to the next line in the dialogue when submit is pressed
    }
    public void EnterDialogueMode(TextAsset inkJson)
    {
        currentStory = new Story(inkJson.text);
        dialoguePanel.SetActive(true);
        dialogueIsPlaying = true;
        ContinueStory();
    }
    private IEnumerator ExitDialogueMode()
    {
        yield return new WaitForSeconds(0.001f);
        dialogueIsPlaying = false;
        dialoguePanel.SetActive(false);
        dialogueText.text = "";
    }
    private void ContinueStory()
    {
        if (currentStory.canContinue)
        {
            if (displayLineCoroutine != null)
            {
                StopCoroutine(displayLineCoroutine);
            }
            displayLineCoroutine = StartCoroutine(DisplayLine(currentStory.Continue()));
        }
        else
        {
            StartCoroutine(ExitDialogueMode());
        }
    }
    public IEnumerator DisplayLine(string line)
    {
        //empty dialogue test
        dialogueText.text = "";
        canContinueToNextLine = false;
        continueIcon.SetActive(false);
        //display each letter one at a time
        foreach (char letter in line.ToCharArray())
        {
            if (Input.GetKeyDown(KeyCode.F))
            {
                dialogueText.text = line;
                break;
            }
            dialogueText.text += letter;
            yield return new WaitForSeconds(typingSpeed);
        }
        continueIcon.SetActive(true);
        canContinueToNextLine = true;
    }
}
Editor is loading...