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

相关推荐
测试开发技术20 小时前
AI 测试提效 | 告别手工写脚本,分享我的 Playwright + Skill 批量生成 UI 自动化脚本方案
自动化测试·人工智能·ui·自动化·agent·skill·ai测试
淡海水20 小时前
12-01-YooAsset工程化-Unity资源管理规范
unity·c#·游戏引擎·yooasset
吾儿良辰1 天前
Flutter 迎来重大变革:Material 设计系统拆分,迈向更开放的 UI 框架
javascript·flutter·ui
打妖妖灵滴哪吒1 天前
IOS模态窗口的作用与设计原则
前端·ui
BuHuaX1 天前
U3D抖微小游戏开发流程 (一)
unity·c#·游戏引擎·游戏策划
打妖妖灵滴哪吒1 天前
前端开发/UI设计师-APP中如何进行可发现性设计
前端·ui
_ZHOURUI_H_1 天前
Unity MyFramework 用法说明(二十三):使用 PrefabPoolManager 复用预设实例
unity·游戏引擎·游戏开发·游戏ui·手游开发
玛艾露贝2 天前
Browser-Use在UI自动化测试中的应用
ui
冰凌糕2 天前
Unity3D Shader 法线与基础光照
unity
brave_zhao2 天前
Axure RP 9模板的使用
ui·axure·photoshop