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

相关推荐
少云清1 天前
【UI自动化测试】6_TPshop项目实战 _allure测试报告
ui
少云清1 天前
【UI自动化测试】3_TPshop项目实战 _一个测试用例脚本编写过程
ui·测试用例
Jasmine8391 天前
OpenClaw部署太难? Codex全流程零编码实现浏览器UI自动化
人工智能·测试工具·ui
不如摸鱼去1 天前
uni-app 组件库 Wot UI 的 AI 友好型编程指南
人工智能·ui·uni-app
应用市场1 天前
无人机动态推力分配与倾斜纠正原理详解
游戏引擎·无人机·cocos2d
大Mod_abfun1 天前
AntdUI教程#1ChatList交互(vb.net)
服务器·前端·ui·交互·antdui·聊天框
少云清1 天前
【UI自动化测试】2_IOS自动化测试 _使用模拟器
ui·ios
带娃的IT创业者1 天前
UI 交互难题攻克:遮挡、弹窗、动态加载
ui·交互·文件上传·浏览器自动化·playwright·ui 交互·元素遮挡
weiyvyy1 天前
无人机嵌入式开发实战-安全机制与应急处理
人工智能·嵌入式硬件·安全·机器人·游戏引擎·无人机·信息化