对话类---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
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);
}
}
}
}
原文地址:https://www.cnblogs.com/xiaoahui/p/10224818.html