🖨
整体思路
把剧本放在文本文件中
支持的文件格式如👇
是以指定某个字符进行切割
一般都是换行字符 👉(‘ \n ‘)(请留意是单引号)
并再指定一个空的列表把每个切割好的字符串排进此列表
然后再来个内部循环
把将列表的字符串每个字打印出来
以协程控制打印速度
初始版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
| using System; using System.Collections; using System.Collections.Generic; using System.Net.Mime; using UnityEngine; using UnityEngine.UI;
public class DialogSystem : MonoBehaviour { public TextAsset dialogsFile; public int index; private Transform textObject; private Text dialog; private List<string> textList = new List<string>();
private void Awake() { InitTextFile(dialogsFile); }
private void OnEnable() { 🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈 dialog.text = textList[index]; index++; 🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈 }
void Start() {
}
void Update()
{ if (Input.GetKeyDown(KeyCode.J) && index == textList.Count) { gameObject.SetActive(false); index = 0; return; } if (Input.GetKeyDown(KeyCode.J)) { 🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈 dialog.text = textList[index]; index++; 🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈🍈 } } void InitTextFile(TextAsset dialogsFile) {
textList.Clear(); index = 0; textObject = transform.GetChild(0); dialog = textObject.GetComponent<Text>(); var lineData = dialogsFile.text.Split('\n'); foreach (var line in lineData) { textList.Add(line); }
} }
|
进阶 StartCoroutine()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
| using System; using System.Collections; using System.Collections.Generic; using System.Net.Mime; using UnityEngine; using UnityEngine.UI;
public class DialogSystem : MonoBehaviour { public TextAsset dialogsFile; public float playTextSpeed; public int index; private Transform textObject; private Text dialog; private List<string> textList = new List<string>(); private bool notTyping;
private void Awake() { InitTextFile(dialogsFile); }
private void OnEnable() { notTyping = true; StartCoroutine(PlayText()); }
void Update()
{ if (Input.GetKeyDown(KeyCode.X) && index == textList.Count) { gameObject.SetActive(false); index = 0; return; }
if (Input.GetKeyDown(KeyCode.X) && notTyping) { StartCoroutine(PlayText()); } }
void InitTextFile(TextAsset dialogsFile) { textList.Clear(); index = 0; textObject = transform.GetChild(0); dialog = textObject.GetComponent<Text>(); var lineData = dialogsFile.text.Split('\n'); foreach (var line in lineData) { textList.Add(line); } }
IEnumerator PlayText() { notTyping = false; dialog.text = "";
switch (textList[index].Trim()) { case "A": print("A"); index++; break;
case "B": print("B"); index++; break; }
for (int i = 0; i < textList[index].Length; i++) { dialog.text += textList[index][i]; yield return new WaitForSeconds(playTextSpeed); }
index++; notTyping = true; } }
|
最终版
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| using System; using System.Collections; using System.Collections.Generic; using System.Net.Mime; using UnityEngine; using UnityEngine.UI;
public class DialogSystem : MonoBehaviour { public TextAsset dialogsFile; public float playTextSpeed; public int index; private Transform textObject; private Text dialog; private List<string> textList = new List<string>(); private bool notTyping, cancelTyping;
private void Awake() { InitTextFile(dialogsFile); }
private void OnEnable() { notTyping = true; StartCoroutine(PlayText()); }
void Update()
{ if (Input.GetKeyDown(KeyCode.X) && index == textList.Count) { gameObject.SetActive(false); index = 0; return; }
#region //直接显示完字
if (Input.GetKeyDown(KeyCode.X)) { if (notTyping && !cancelTyping) { StartCoroutine(PlayText()); } else if (!notTyping) { cancelTyping = !cancelTyping; } }
#endregion }
void InitTextFile(TextAsset dialogsFile) { textList.Clear(); index = 0; textObject = transform.GetChild(0); dialog = textObject.GetComponent<Text>(); var lineData = dialogsFile.text.Split('\n'); foreach (var line in lineData) { textList.Add(line); } }
IEnumerator PlayText() { notTyping = false; dialog.text = "";
switch (textList[index].Trim()) { case "A": print("A"); index++; break;
case "B": print("B"); index++; break; }
#region While
int letter = 0; while (!cancelTyping && letter < textList[index].Length) { dialog.text += textList[index][letter]; yield return new WaitForSeconds(playTextSpeed); letter++; } dialog.text = textList[index]; cancelTyping = false; #endregion
index++; notTyping = true; } }
|
请留意:输入设备 👉 Both