So here’s the code for the dialogue system. But i don’t know how to implement it to show once all i know is that i have to implement something about playerprefs.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace DialogueSystem
{
public class DialogueBaseClass : MonoBehaviour
{
public bool finished { get; protected set; }
protected IEnumerator WriteText(string input, Text textHolder, Color textColor, Font textFont, float delay, AudioClip sound, float delayBetweenLines)
{
textHolder.color = textColor;
textHolder.font = textFont;
for (int i = 0; i < input.Length; i++)
{
textHolder.text += input[i];
SoundManager.instance.PlaySound(sound);
yield return new WaitForSeconds(delay);
}
yield return new WaitUntil(() => Input.GetMouseButton(0));
finished = true;
}
}
}
using UnityEngine;
using System.Collections;
namespace DialogueSystem
{
public class DialogueHolder : MonoBehaviour
{
public static DialogueHolder instance;
public bool dialogueEnd;
private IEnumerator dialogueSeq;
private void Awake()
{
dialogueSeq = dialogueSequence();
StartCoroutine(dialogueSeq);
}
private void Update()
{
if (Input.GetKey(KeyCode.Space))
{
Deactivate();
gameObject.SetActive(false);
}
}
private IEnumerator dialogueSequence()
{
for (int i = 0; i < transform.childCount; i++)
{
Deactivate();
transform.GetChild(i).gameObject.SetActive(true);
yield return new WaitUntil(() => transform.GetChild(i).GetComponent<DialogueLine>().finished);
}
gameObject.SetActive(false);
}
private void Deactivate()
{
for (int i = 0; i < transform.childCount; i++)
{
transform.GetChild(i).gameObject.SetActive(false);
}
dialogueEnd = true;
}
}
}
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace DialogueSystem
{
public class DialogueLine : DialogueBaseClass
{
private Text textHolder;
[Header("Text")]
[SerializeField] private string input;
[SerializeField] private Color textColor;
[SerializeField] private Font textFont;
[Header("Time Parameters")]
[SerializeField] private float delay;
[SerializeField] private float delayBetweenLines;
[Header("Sound")]
[SerializeField] private AudioClip sound;
[Header("Character Image")]
[SerializeField] private Sprite characterSprite;
[SerializeField] private Image imageHolder;
private IEnumerator lineAppear;
private void Awake()
{
textHolder = GetComponent<Text>();
textHolder.text = "";
imageHolder.sprite = characterSprite;
imageHolder.preserveAspect = true;
}
private void Start()
{
lineAppear = WriteText(input, textHolder, textColor, textFont, delay, sound, delayBetweenLines);
StartCoroutine(lineAppear);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
if (textHolder.text != input)
{
StopCoroutine(lineAppear);
textHolder.text = input;
}
else
finished = true;
}
}
}
}