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

相关推荐
郝学胜-神的一滴2 小时前
游戏设计、原型与开发 26:四种循环
开发语言·数据结构·游戏·游戏引擎·游戏策划
两水先木示2 小时前
【Unity】刮刮乐案例-RenderTexture实现思路
unity·游戏引擎
学术小李11 小时前
用虚幻引擎(UE)打开Obj模型
游戏引擎·虚幻
zhongben_keji11 小时前
APP WebView 混合开发体系:通信协议、加载优化与安全治理的工程落地
ui·ci/cd·自动化
张不无-Unity12 小时前
Unity的四种灯光Directional、Point、Spot 和 Area
unity·游戏引擎
fatCatLA12 小时前
UI模拟器使用手册
单片机·嵌入式硬件·ui
蓝悦无人机14 小时前
Figma组件库搭建教程(实战篇)
前端·ui·figma
zhongben_keji15 小时前
APP 无障碍与适老化改造工程实践:语义、视觉、交互三层落地
ui·ci/cd·自动化
汉得数字平台2 天前
【汉得飞码】构建接口与UI自动化测试全链路,让验证反馈持续驱动交付
自动化测试·人工智能·ui
淡海水2 天前
02-02-原理篇-Scriban模板引擎原理深度解析
unity·excel·游戏开发·luban