This commit is contained in:
Gerard Gascón 2025-04-24 17:19:36 +02:00
commit 001bb14f16
951 changed files with 270074 additions and 0 deletions

View file

@ -0,0 +1,10 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu(fileName = "New Character", menuName = "Tools/Character")]
public class Dialogue : ScriptableObject{
public string characterName;
[TextArea] public string[] sentences;
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bd553cd05b84d614b998afe62e37c17c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,44 @@
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class DialogueSystem : MonoBehaviour{
public TextMeshProUGUI nameText;
public TextMeshProUGUI dialogueText;
public TMP_Animated dialogue;
public Queue<string> sentences;
// Start is called before the first frame update
void Start(){
sentences = new Queue<string>();
}
public void StartDialogue(Dialogue dialogue){
nameText.text = dialogue.characterName;
sentences.Clear();
foreach(string sentence in dialogue.sentences){
sentences.Enqueue(sentence);
}
DisplayNextSentence();
}
public void DisplayNextSentence(){
if(sentences.Count == 0){
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
dialogue.ReadText(sentence);
}
void EndDialogue(){
Debug.Log("End of conversation.");
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: caad12703fd5c3349acc637253734ac9
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DialogueTrigger : MonoBehaviour{
public Dialogue dialogue;
/// <summary> Start the dialogue specified on the inspector </summary>
public void TriggerDialogue(){
FindObjectOfType<DialogueSystem>().StartDialogue(dialogue);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f15c572386779eb4aa5d6e8471fba51d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,72 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
namespace TMPro{
[System.Serializable] public class TextRevealEvent : UnityEvent<char> { }
[System.Serializable] public class DialogueEvent : UnityEvent { }
public class TMP_Animated : TextMeshProUGUI{
float speed;
public TextRevealEvent onTextReveal;
public DialogueEvent onDialogueFinish;
public void ReadText(string newText){
text = string.Empty;
string[] subTexts = newText.Split('<', '>');
string displayText = "";
for (int i = 0; i < subTexts.Length; i++){
if (i % 2 == 0)
displayText += subTexts[i];
else if (!isCustomTag(subTexts[i].Replace(" ", "")))
displayText += $"<{subTexts[i]}>";
}
bool isCustomTag(string tag){
return tag.StartsWith("speed=") || tag.StartsWith("pause=");
}
text = displayText;
maxVisibleCharacters = 0;
StartCoroutine(Read());
IEnumerator Read(){
int subCounter = 0;
int visibleCounter = 0;
while(subCounter < subTexts.Length){
if(subCounter % 2 == 1){
yield return EvaluateTag(subTexts[subCounter].Replace(" ", ""));
}else{
while(visibleCounter < subTexts[subCounter].Length){
onTextReveal.Invoke(subTexts[subCounter][visibleCounter]);
visibleCounter++;
maxVisibleCharacters++;
yield return new WaitForSeconds(1f / speed);
}
visibleCounter = 0;
}
subCounter++;
}
yield return null;
WaitForSeconds EvaluateTag(string tag){
if (tag.Length > 0){
if (tag.StartsWith("speed=")){
speed = float.Parse(tag.Split('=')[1]);
}else if (tag.StartsWith("pause=")){
return new WaitForSeconds(float.Parse(tag.Split('=')[1]));
}
}
return null;
}
onDialogueFinish.Invoke();
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f713d84a3ae882945800780459e26170
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: