对话类---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

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);
}
}

}

}

Unity角色对话的更多相关文章

  1. unity 角色换装

    unity角色换装的关键是更改角色部位上的物体的SkinnedMeshRenderer组件的属性: 更改mesh:mesh决定了部位的物体的外形,是主要的数据. 刷新骨骼:同一个部位下,不同的mesh ...

  2. Unity 角色复活和重新开始游戏

    作者写游戏完成的时候,还需要从新想下如何把游戏设置重新开始,角色如何复活. 一般大多数都会采用这种方式来代替游戏重新开始 Application.LoadLevel("xxx场景" ...

  3. Unity 角色移动贴墙行走

    直接贴上代码,旋转角色角度检测碰撞 Vector2 v2Normal = new Vector2(normal.x, normal.y); float fAngle = Vector2.SignedA ...

  4. Unity 角色场景传送功能

    传送触发器 using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine. ...

  5. unity 角色旋转

    using UnityEngine; using System.Collections; public class Triangle : MonoBehaviour { public float sp ...

  6. Unity角色残影特效

    残影特效在网上有很多例子,比如这个,我参考着自己整合了一下,算是整合了一个比较完整且特别简单易用的出来,只需要一个脚本挂上去无需任何设定就能用. 这里只针对SkinnedMeshRenderer的网格 ...

  7. unity assetStore 常用插件

    常用插件 20180723============= 教程类 =============<Mecanim Example Scenes > 官方示例场景<Surivial Shoot ...

  8. 通过XML文件实现人物之间的对话

    一.建立一个XML文档,放在项目中Assert/Resources/XML文件下 XML的内容如下: <?xml version="1.0" encoding="u ...

  9. unity对话代码

    这个是根据网上unity GUI打字机教程修改的 原教程是JS,我给改成了C#,然后增加了许多功能 这个教程能实现一段文字对话,有打字机显示效果,能写许多对话,能快进对话,总之现在RPG游戏里有的功能 ...

随机推荐

  1. rsync 数据备份+cron+mailx案例

    大家都知道数据非常重要的,需要经常备份,如果备份了,但无法恢复还原,那就证明你备份的很失败,所有当我们备份了数据需要检查是否备份完整,是否可用可恢复.以下为一个企业案例: 某公司里有一台Web服务器, ...

  2. C++设计模式 ==> 装饰(者)模式

    简介 装饰模式指的是在不必改变原类文件和使用继承的情况下,动态地扩展一个对象的功能.它是通过创建一个包装对象,也就是装饰来包裹真实的对象.装饰模式使用对象嵌套的思想,实现对一个对象动态地进行选择性的属 ...

  3. leaflet学习一 入门

    1从官网https://leafletjs.com/下载的Leaflet包含文件: leaflet.js - 简化版的 Leaflet JavaScript代码 leaflet-src.js - 这是 ...

  4. Volley源码分析(四)NetWork与ResponseDelivery工作原理

    这篇文章主要分析网络请求和结果交付的过程. NetWork工作原理 之前已经说到通过mNetWork.performRequest()方法来得到NetResponse,看一下该方法具体的执行流程,pe ...

  5. redis性能测试工具的使用

    在redis安装完成后会生成一个bin的目录,在这个目录中有一个 redis-benchmark 的文件脚本工具,通过执行这个工具进行redis的性能测试. bash #执行这个脚本后脚本会自动运行r ...

  6. php 去除数组中指定的值

    方法1: //去除值为"Cat"的元素 $a=array("a"=>"Dog","b"=>"Cat ...

  7. java字符串利用dom4j转 xml 且遍历

    1.因为转换的格式不是标准格式,所以有时候获得xml根目录后rootElement.attributes() 取不到想要的属性 所以需要通过迭代器来获取想要的值 public static void ...

  8. shiro实战系列(一)之入门实战

    一.什么是shiro? Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密.   Apache Shiro 的首要目标是易于使用和理解.安全有 ...

  9. 多Tomcat多JDK版本的Window服务添加配置方式

    本文所讲的配置方式只适合Windows系统,所牵扯的软件2个解压的jdk,2个解压的tomcat. 1.环境初始化 将两个jdk和tomcat解压到我们指定的目录下.修改两个tomcat的端口:必须修 ...

  10. PAT B1002 写出这个数

    读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字. 输入格式:每个测试输入包含1个测试用例,即给出自然数n的值.这里保证n小于10100. 输出格式:在一行内输出n的各位数字之和的每 ...