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中提前设置添加对话内容,将脚本挂载到对话组件上。

相关推荐
WiChP3 小时前
【V0.1B11】从零开始的2D游戏引擎开发之路
开发语言·游戏引擎
玖玥拾3 小时前
Cocos学习笔记:武器系统与数据驱动UI联动
游戏引擎·cocos2d
winlife_5 小时前
全程用 AI 做一款商业级手游 · EP1 地基:先搭框架层,不急着写玩法
unity·ai编程·游戏架构·mcp·框架设计·funplay
小贺儿开发6 小时前
Unity VideoPlayer 播放控制器
unity·编辑器·播放器·视频·工具·videoplayer·互动
辣香牛肉面7 小时前
Photoshop CC 2025新手入门教程
ui·photoshop
tang&9 小时前
【测试】Web页面UI自动化测试完全指南:8步通用测试框架
ui·测试
星栈独行12 小时前
Makepad、egui、Dioxus、Tauri:Rust GUI 到底怎么选
开发语言·后端·程序人生·ui·rust
skywalk816313 小时前
nginx的配置软件Nginx UI
运维·nginx·ui
め.14 小时前
UIFramework
ui·unity
fqbqrr15 小时前
2606d,用d语言构建游戏引擎
游戏引擎·d