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

相关推荐
DaLiangChen2 小时前
Unity 实用工具:动态绘制物体边界包围盒(支持屏幕固定线宽)
unity·游戏引擎
张老师带你学2 小时前
Unity 食物 农产品相关
科技·游戏·unity·游戏引擎·模型
mxwin3 小时前
Unity Custom Interpolators与半透明阴影的原理与实战
unity·游戏引擎·shader
晴夏。3 小时前
UE5第三人称模板实现及相关引擎源码分析
unity·ue5·游戏引擎·ue
HAPPY酷3 小时前
解决 Unreal Engine 编译报错 MSB4018:三个核心排查方向
游戏引擎·虚幻
apollowing4 小时前
Avalonia UI 12.0.0 正式发布:架构演进和性能飞跃
ui·架构
oioihoii4 小时前
OpenClaw桌面 UI 自动化中的 Token 消耗问题几种可能的优化方向
运维·ui·自动化
qq_452396236 小时前
第九篇:《处理常见复杂UI组件:下拉框、弹窗、iframe、多窗口》
ui
深蓝海拓6 小时前
Qt:创建一套基于HSL颜色体系的颜色库
笔记·python·qt·学习·ui
for_ever_love__7 小时前
UI学习:反向传值(代理传值)深入学习
学习·ui·objective-c