Hello! I wanted to share some of my work starting today! You will learn how to create a simple dialogue system with Unity Coroutines and ScriptableObjects.
Create a ScriptableObject on Project folder using Create > ScriptableObjects > DialogueSystem
Make it single system:
// Make sure to drag the scriptable object here!
[SerializeField]
private DialogueSystem dialogueSystem;
//...
void Start()
{
StartCoroutine(StartDialogue);
}
private IEnumerator StartDialogue()
{
// Get first dialog
var firstDialog = dialogueSystem.contexts[0].dialogue;
// Or loop through
for(int i=0;i<dialogueSystem.contexts.length;i++)
{
var dialogue = dialogueSystem.contexts[i].dialogue;
yield return new WaitForSeconds(dialogueSystem.contexts[i].contextReadTime);
}
// ...
}
Or if you want to create sequences, make it an array:
[SerializeField]
private DialogueSystem[] dialogueSystems;
//...
int currentSequence=0;
void Start()
{
StartCoroutine(StartDialogue());
}
private IEnumerator StartDialogue()
{
// Get sprite for talking person
var talkerSprite = dialogueSystem[currentSequence].talkerSprite;
// Loop through
for(int i=0;i<dialogueSystem[currentSequence].contexts.length;i++)
{
var dialogue = dialogueSystem[currentSequence].contexts[i].dialogue;
yield return new WaitForSeconds(dialogueSystem[currentSequence].contexts[i].contextReadTime);
}
// ...
}
If you want to divide sprites for each dialogue, simply move public Sprite talkerSprite
inside Context
class.
Check out the full example here.