對話類---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class word {
public int state;//對應的劇情狀態
public string[] sentence;//角色雙方說的每句話
public int[] roleOrder;//每句話對應的角色,好吧暫時用不上
}
//對話類,點擊I鍵觸發對話,每一句話會以每隔一小段時間片顯示一個字符的形式顯示,讀完該句話之后,再點擊I鍵跳轉到下一句,如果沒有句子了,那么
//結束對話,對話的相應句子映射到場景控制器類中,場景控制器根據當前的場景狀態設置對話框的顯示與否,並且將讀取的句子填充到對話框中
//句子列表表示不同的劇情狀態下的對話內容
//通過繼承對話類,對話的對象可以是npc,也可以是任何可以觸發對話的物體
public abstract class TalkManager:MonoBehaviour{
public Transform role01;//角色01
public Transform role02;//角色02
public int roleState=0;
public List<word> words;//句子
public float letterTime;//出現單詞的間隔時間
public int wordsIndex;//劇情對話的內容下標
public int sentenceIndex;//對話內容的句子的下標
public int letterIndex;//單詞的下標
public GameObject UI_text;
float time;
string currentSentence;//當前句子
private void OnTriggerStay2D(Collider2D collision)
{
if (SceneControl.state == 0 && Input.GetKeyDown(KeyCode.I)&&collision.tag.Equals("Player")) {
for (int i = 0; i < words.Count; i++) {
if (words[i].state == GameManager.taskState) {//查詢該角色的對話庫,獲取到與當前劇情對應的對話,並建立對話
roleState = 1;
SceneControl.state = 2;
sentenceIndex = 0;
wordsIndex = i;
letterIndex = 0;
time = Time.time;
role01 = collision.gameObject.transform;
InitBegin();
break;
}
}
}
}
protected void dialogue()
{
if (sentenceIndex < words[wordsIndex].sentence.Length)
{
//讀取當前句子的每一個單詞
loadLetters();
//如果當前句子已經讀完,當前對話沒有讀完還有下一句,那么讀取下一句
if (letterIndex == words[wordsIndex].sentence[sentenceIndex].Length && Input.GetKeyDown(KeyCode.I) && Time.time > time)
{
sentenceIndex += 1;
letterIndex = 0;
currentSentence = "";
}
}
else {//如果對話內容已經讀完,那么設置state為0
SceneControl.state = 0;
roleState = 0;
InitEnd();
}
}
//讀取當前句子的每一個字符
void loadLetters() {
if (letterIndex < words[wordsIndex].sentence[sentenceIndex].Length&&Time.time>time) {
currentSentence += words[wordsIndex].sentence[sentenceIndex][letterIndex];
//Debug.Log(currentSentence);
SceneControl.currentSentence = currentSentence;
letterIndex += 1;
time = Time.time + letterTime;
}
}
public abstract void InitBegin();
public abstract void InitEnd();
}
角色子類-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//繼承自對話類,可以靈活的對對話做出相應的操作
public class role01 : TalkManager {
GeneralPeopleController generalPeopleController;
public Transform attachedObj;//好吧,這里由於我建立的角色的一些物理上的程序缺陷,所以此類的物體不是作為某個對話對象的子物體,而是跟隨綁定
private void Start()
{
generalPeopleController = attachedObj.GetComponent<GeneralPeopleController>();
}
private void Update()
{
transform.position = attachedObj.position;
//對話過程
if (roleState == 1) {
dialogue();
}
}
public override void InitBegin()
{
generalPeopleController.horizonDir = (int)Mathf.Sign(role01.position.x - attachedObj.position.x);
generalPeopleController.SetRole(2);
Debug.Log(SceneControl.state);
}
public override void InitEnd()
{
generalPeopleController.SetRole(0);
roleState = 0;
Debug.Log(SceneControl.state);
}
}
場景控制類-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Serialization;
[System.Serializable]
public class WayPoint {
public int pathPoint;//路徑點標記
public Transform PointAnchor;//路徑點的位置
}
public class SceneControl : MonoBehaviour {
[SerializeField]
public static int state;//0正常,1改變場景,2交互
public static int pathPoint;//角色傳送到當前場景時對應通過的路徑點標記
public static string currentSentence;
public List<WayPoint> list = new List<WayPoint>();
public Transform player;
//對話框
public GameObject talkImg;
Text text;
//退出UI界面
public GameObject panelMenue;
private void Start()
{
foreach (WayPoint point in list) {
if (point.pathPoint == pathPoint && point.PointAnchor && player) {
player.position = point.PointAnchor.position;
break;
}
}
if(talkImg)//填充句子到對話板中
text = talkImg.GetComponentInChildren<Text>();
if (talkImg&&talkImg.activeInHierarchy) talkImg.SetActive(false);
}
void Update()
{
if (state == 2)
{
if (talkImg.activeInHierarchy == false) talkImg.SetActive(true);
text.text = currentSentence;
}
else {
if (talkImg&&talkImg.activeInHierarchy) talkImg.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.Escape)&&panelMenue) {
if (panelMenue.activeInHierarchy)
{
panelMenue.SetActive(false);
}
else {
panelMenue.SetActive(true);
}
}
}
}