当前位置: 首页 > news >正文

Unity 点击对话系统(含Demo)

点击对话系统

可实现点击物体后自动移动到物体附近,然后弹出对话框进行对话。
在这里插入图片描述
在这里插入图片描述

基于Unity 简单角色对话UI脚本的编写(新版UI组件)和Unity 关于点击不同物品移动并触发不同事件的结合体,有兴趣可以看一下之前文章。
下边代码为UI界面的公共脚本:

public class DialogueUI : MonoBehaviour
{private TextMeshProUGUI nameTexe;//获取名称的Text组件private TextMeshProUGUI descriptionTexe; //获取内容的Text组件private Button resumeButton;//继续对话的按钮public List<string> contentlist;//对话列表private int contentIndex = 0;//对话数组private void Start(){//获取组件nameTexe = transform.Find("NameText").GetComponent<TextMeshProUGUI>();descriptionTexe= transform.Find("ContentText").GetComponent<TextMeshProUGUI>();resumeButton = transform.Find("ResumeButton").GetComponent<Button>();resumeButton.onClick.AddListener(this.OnContinueButtonClick);descriptionTexe.text = contentlist[0];Hide();}public void Show()     {gameObject.SetActive(true);//显示对话框}public void Show(string name, string[] content)//调用方法获得对话{contentIndex = 0;nameTexe.text = name;contentlist=new List<string>();contentlist.AddRange(content);descriptionTexe.text = contentlist[0];Show();}public void Hide() //关闭对话{gameObject.SetActive(false);}private void OnContinueButtonClick(){//调用对话列表,如果没有对话,窗口关闭contentIndex++;if (contentIndex >= contentlist.Count){Hide();return;}descriptionTexe.text = contentlist[contentIndex];}}

下边为被点击物体挂载的脚本:

public class ItemObject : InteractionObject
{public string name;public string[] contenList;public DialogueUI dialogueUI;protected override void Interact(){print("我就是个东西!");dialogueUI.Show(name, contenList);}
}

然后是Player挂载的触发脚本:

public class PlayerRoutine : MonoBehaviour
{// Start is called before the first frame updateprivate NavMeshAgent PlayerAgent;void Start(){PlayerAgent = GetComponent<NavMeshAgent>();}// Update is called once per framevoid Update(){if (Input.GetMouseButtonDown(0)&&EventSystem.current.IsPointerOverGameObject()==false)//点击鼠标左键并且没有点击到IU组件{Ray ray=Camera.main.ScreenPointToRay(Input.mousePosition);//创建一条射线RaycastHit hit;bool isCollide = Physics.Raycast(ray, out hit);if (isCollide){if (hit.collider.tag == "Ground")//假如点击物体的标签为Ground{PlayerAgent.stoppingDistance = 0;//停止距离为0PlayerAgent.SetDestination(hit.point);//移动到点击位置}else if (hit.collider.tag == "NPC") //假如点击物体的标签为NPC{hit.collider.GetComponent<InteractionObject>().OnClick(PlayerAgent);//调用InteractionObject中的Onclick方法}else if (hit.collider.tag == "Item") {hit.collider.GetComponent<InteractionObject>().OnClick(PlayerAgent);}}}}
}

还有公共管理类脚本:

public class InteractionObject : MonoBehaviour
{private NavMeshAgent PlayerAgent;private bool haveinteracted=false;//用于判断方法已经调用public void OnClick(NavMeshAgent PlayerAgent) {this.PlayerAgent = PlayerAgent;PlayerAgent.stoppingDistance = 2;//寻路停止距离PlayerAgent.SetDestination(transform.position);//移动到寻路点haveinteracted = false;}private void Update(){if (PlayerAgent != null&&haveinteracted==false&&PlayerAgent.pathPending==false)//后边是判断路径是否计算完成{if (PlayerAgent.remainingDistance <= 2)//距离目标点的距离是否小于2米{Interact();//调用Interact()方法haveinteracted = true;}}}protected virtual void Interact() //便于重写{print("点到了NPC");}
}
http://www.lryc.cn/news/275144.html

相关文章:

  • vue接入高德地图
  • Linux的基本指令(5)
  • 华为商城秒杀时加密验证 device_data 的算法研究
  • Wrk压测发送Post请求的正确姿势
  • 【管理篇 / 登录】❀ 06. macOS下使用USB配置线登录 ❀ FortiGate 防火墙
  • linux系统shell语言的自动化交互
  • HarmonyOS ArkTS 三方库的基本使用(十六)
  • Spring boot封装rocket mq 教程
  • Java Swing手搓童年坦克大战游戏(I)
  • 【DevOps-04]】Operate阶段工具
  • 力扣2807.在链表中插入最大公约数
  • 开始刷Leetcode之前你需要知道的 - The basic is all you need
  • 【PostgreSQL】模式Schema
  • JavaScript实现的复杂功能:自动生成带水印的图片
  • 图神经网络|8.2 图卷积的计算基本方法
  • equals()与hashCode()方法详解
  • 六、基于Flask、Flasgger、marshmallow的开发调试
  • TypeScript 从入门到进阶之基础篇(三) 元组类型篇
  • 现代CPU的多种运行模式
  • Python PDF处理模块pypdf库详解
  • C++上位软件通过LibModbus开源库和西门子S7-1200/S7-1500/S7-200 PLC进行ModbusTcp 和ModbusRTU 通信
  • PLSQL Developer 15安装和oracle客户端安装
  • 【深度deepin】深度安装,jdk,tomcat,Nginx安装
  • 解决flask启动报错:ImportError: DLL load failed while importing _dukpy: 找不到指定的程序
  • 腾讯面试总结
  • 面向对象进阶(static关键字,继承,方法重写,super,this)
  • Blazor项目如何调用js文件
  • Windows11 - Ubuntu 双系统及 ROS、ROS2 安装
  • 深度学习(学习记录)
  • html5实现好看的个人博客模板源码