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

相关推荐
小邓是个人才呀4 小时前
第二章:Android常用UI控件
android·java·ui
专注VB编程开发20年6 小时前
在 VB6 中强制设置 Word 文档的纸张尺寸
ui·c#·word·vba·vb6
虾球xz7 小时前
游戏引擎学习第302天:使用精灵边界进行排序
c++·学习·算法·游戏引擎
虾球xz7 小时前
游戏引擎学习第297天:将实体分离到Z层中
c++·人工智能·学习·游戏引擎
Thomas_YXQ10 小时前
Unity3D序列化机制详解
java·开发语言·游戏·unity·游戏引擎
鸿蒙自习室12 小时前
鸿蒙UI开发——实现一个上拉抽屉效果
ui·华为·harmonyos·鸿蒙
bao_lanlan13 小时前
兰亭妙微 | 系统界面设计优化:让复杂信息更轻松被看懂
ui·adobe·设计模式·ux
虾球xz1 天前
游戏引擎学习第288天:继续完成Brains
c++·学习·游戏引擎
虾球xz1 天前
游戏引擎学习第299天:改进排序键 第二部分
c++·学习·算法·游戏引擎
Thomas_YXQ1 天前
Unity3D HUD UI性能优化方案
开发语言·ui·搜索引擎·性能优化·全文检索·unity3d