Unity 简单角色对话UI脚本的编写(新版UI组件)

简单角色对话UI脚本的编写

新建UI,里边有两个文本组件一个按钮组件,一个展示名称,一个展示对话内容,按钮组件负责展示下一句对话。

csharp 复制代码
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];

    }

    public void Show()     
    {
        gameObject.SetActive(true);//显示对话框
    }
    public void Show(string name, string[] content)//调用方法获得对话
    { 
        nameTexe.text = name;
        contentlist=new List<string>();
        contentlist.AddRange(content);
        descriptionTexe.text = contentlist[0];
        
    }
    public void Hide() //关闭对话
    {
        gameObject.SetActive(false);
    }
    private void OnContinueButtonClick()
    {
        //调用对话列表,如果没有对话,窗口关闭
        contentIndex++;
        if (contentIndex >= contentlist.Count)
        {
            Hide();return;
        }
        descriptionTexe.text = contentlist[contentIndex];
    }

 
}

需要在U3D中提前设置添加对话内容,将脚本挂载到对话组件上。

相关推荐
传奇开心果编程11 小时前
【SwiftUI提高练中学】第13课 实时活动与灵动岛:ActivityKit 实战
学习·ui·ios·swiftui·swift
WarPigs16 小时前
Unity在Create菜单添加创建自定义脚本模板选项
unity
phltxy16 小时前
C 语言动态内存管理:从申请空间到安全释放
c语言·开发语言·ui
郝学胜-神的一滴19 小时前
C++ Templates 01:拨开迷雾,读懂C++模板经典著作
开发语言·c++·程序人生·游戏·游戏引擎·软件构建
淡海水20 小时前
02-01-原理篇-Mark-Sweep与变种算法
算法·unity·c#·游戏引擎·.net·gc
babywang021 小时前
Unity笔记--塔防游戏中的ECS架构:从原理到实战的全面解析
unity·ecs架构
李少兄1 天前
文档排版与数据处理:阿拉伯数字与罗马数字的转换
ui·xhtml·wps
狗凯之家源码网1 天前
网址导航系统源码评测:个性 UI 与轻量化架构实战
ui·架构·网址导航系统
传奇开心果编程1 天前
【SwiftUI入门练中学】第17课 通知与后台任务
学习·ui·ios·swiftui·swift
wshzd1 天前
LLM之Agent(107)|DeepSeek-Harness(十五)流式输出管道:从 StreamChunk 到 UI
人工智能·ui