unity教程:galgame對話框功能

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class GalgameMnagaer : MonoBehaviour
{
//對話信息
[SerializeField]
private List<string> body;
[SerializeField]
//當(dāng)前是第幾段對話
private int index = 0;
//文本組件
private Text text;
[SerializeField]
//所有人物
private Galgame_Player[] player;
//組件初始化
private void Awake()
{
text = this.transform.parent.GetChild(2).GetChild(0).GetChild(0).gameObject.GetComponent<Text>();
}
//對象初始化
private void OnEnable()
{
body = InitBody();
}
//初始化對話內(nèi)容
private List<string> InitBody()
{
string vs = File.ReadAllText("D:\\unity\\new unity\\MVC cs\\Assets\\cs\\Galgame\\body\\body.txt");
string[] vs1 = vs.Split('\n');
List<string> vs2=new List<string>();
for (int i = 0; i < vs1.Length; i++)
{
vs1[i].Trim();
if (!(vs1[i].Length <2 || (vs1[i][0] + vs1[i][1].ToString()) == "//"))
{
vs2.Add(vs1[i]);
}
}
return vs2;
}
/// <summary>
/// 執(zhí)行下一段對話
/// </summary>
public void LoadNextBody()
{
try
{
index++;
string[] vs = body[index - 1].Split('#');
StopAllCoroutines();
StartCoroutine(ShowBody(vs[0]));
SwitchPlayer(player[int.Parse(vs[1])], vs[2], vs[3], vs[4], vs[5]);
}
catch
{
StopAllCoroutines();
StartCoroutine(ShowBody("發(fā)生錯誤"));
}
}
/// <summary>
/// 顯示內(nèi)容
/// </summary>
/// <param name="body">需要顯示的內(nèi)容</param>
private IEnumerator ShowBody(string body)
{
var time = new WaitForSecondsRealtime(0.05f);
text.text = string.Empty;
for (int i = 0; i < body.Length; i++)
{
text.text += body[i];
yield return time;
}
text.text += " ▼";
}
/// <summary>
/// 切換人物信息
/// </summary>
public void SwitchPlayer(Galgame_Player player, string acc, string hair, string body, string face)
{
if (this.player.Length == 0)
{
StopAllCoroutines();
StartCoroutine(ShowBody("發(fā)生錯誤,找不到人物"));
return;
}
try
{
Debug.Log("acc" + acc + "hair" + hair + "body" + body + "face" + face);
player.SwitchExpression(acc, hair, body, face);
}
catch
{
StopAllCoroutines();
StartCoroutine(ShowBody("發(fā)生錯誤,人物填寫錯誤"));
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Galgame_Player : MonoBehaviour
{
/// <summary>
/// 裝飾
/// </summary>
public SpriteRenderer Acc;
public Sprite acc1;
/// <summary>
/// 頭發(fā)
/// </summary>
public SpriteRenderer Hair;
public Sprite hair1;
public Sprite hair2;
/// <summary>
/// 衣服
/// </summary>
public SpriteRenderer Body;
public Sprite body1;
public Sprite body2;
/// <summary>
/// 表情
/// </summary>
public SpriteRenderer Face;
public Sprite face1;
public Sprite face2;
public Sprite face3;
public Sprite face4;
/// <summary>
/// 切換人物信息
/// </summary>
public void SwitchExpression(string acc,string hair,string body,string face)
{
switch (acc)
{
case "1":
Acc.sprite = acc1;
break;
default:
Acc.sprite = null;
break;
}
switch (hair)
{
case "1":
Hair.sprite = hair1;
break;
case "2":
Hair.sprite = hair2;
break;
default:
Hair.sprite = hair1;
break;
}
switch (body)
{
case "1":
Body.sprite = body1;
break;
case "2":
Body.sprite = body2;
break;
default:
Body.sprite = body1;
break;
}
switch (face)
{
case "1":
Debug.Log("cs1");
Face.sprite = face1;
break;
case "2":
Debug.Log("cs2");
Face.sprite = face2;
break;
case "3":
Debug.Log("cs3");
Face.sprite = face3;
break;
case "4":
Debug.Log("cs4");
Face.sprite = face4;
break;
default:
Debug.Log("cs0");
Face.sprite = face1;
break;
}
}
}