Recently, I learned the dialogue system in Unity according to the online tutorial, and recorded some key points as a reference. More functions can be added on this basis in the future.
1.UI settings.
The dialog box consists of text and image under a panel. The render mode of canvas is recommended to be set to World Space, because it is convenient to set the position of the dialog box at will
2.TextAsset
TextAsset is a format that Unity uses to import text files. When you drag a text file into your project, it will be converted into a text resource. Supported text formats are:
· .txt,.html,.htm,.xml,.bytes,.json,.csv
A useful property in TextAsset is TextAsset.Text. This property is of type string and is used to access all text in TextAsset.
3.DialogueSystem script construction
public Text text; public Image Image; public TextAsset textAsset; List<string> textList = new List<string>(); int index = 0; public float time; bool textFinished; bool cancelTyping; public Sprite face1, face2;
To put it simply, the implementation of the basic dialog box requires the above elements, that is, the complete text content textAsset to be displayed, the content textList that can be displayed in each dialog box, and the avatars to be switched, face1, face2, etc.
The most basic dialogue system needs to realize:
- The content in textAsset is displayed in segments according to people, and the avatar is switched according to the speaker
- Fast word by word display of text content
- Players can skip the second item by pressing the key and immediately display the complete sentence to be said by the current character
The contents in TextAsset are generally edited in the following format:
A
XXX,XXXXXXXXX
B
XXXXXXXXXXXXXXXX!
Therefore, during initialization, the text content is divided and saved in textList by the following methods:
void GetTextFromFile(TextAsset file) { textList.Clear(); index = 0; var linedata = file.text.Split('\n'); for (int i = 0; i < linedata.Length; i++) { textList.Add(linedata[i]); } }
The word by word appearance of the second point above is realized by a coprocessor. The specific code is as follows. The bool variable textFinished is used to control that only one relevant coprocessor is running at the same time, and the float variable time controls the speed of text display:
IEnumerator SetTextUI(float time) { textFinished = false; text.text = ""; switch (textList[index]) { case "A": Image.sprite = face1; index++; break; case "B": Image.sprite = face2; index++; break; } int letter = 0; while(!cancelTyping && letter <textList[index].Length -1){ text.text += textList[index][letter]; letter++; yield return new WaitForSeconds(time); } text.text = textList[index]; cancelTyping = false; index++; textFinished = true; }
The third point is to skip word by word display and directly display the complete content through the bool variable cancelTyping
void Update() { if (Input.GetKeyDown(KeyCode.Space) && index == textList.Count) { index = 0; gameObject.SetActive(false); return; } if (Input.GetKeyDown(KeyCode.Space)) { if (textFinished && !cancelTyping) { StartCoroutine(SetTextUI(time)); } else if (!textFinished) { cancelTyping = !cancelTyping; } } }
cancelTyping is false by default. When textFinished is false, that is, when the collaboration is in progress, change cancelTyping to true to interrupt the collaboration.
The above functions are only basic functions. You can also add functions such as pressing the key to directly exit the dialogue window as needed.