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

相关推荐
死也不注释6 小时前
【第零章编辑器开发与拓展】
unity·编辑器
死也不注释7 小时前
【第一章编辑器开发基础第二节编辑器布局_3GUI元素和布局大小(3/4)】
unity·编辑器
SEO-狼术12 小时前
DevExtreme Complete Crack,responsive UI widgets
ui
ui设计前端开发老司机16 小时前
从数据洞察到设计创新:UI前端如何利用数字孪生提升产品交互体验?
前端·ui·交互
KhalilRuan1 天前
Unity Demo-3DFarm详解-其二
unity·游戏引擎
死也不注释2 天前
【unity编辑器开发与拓展EditorGUILayoyt和GUILayoyt】
unity·编辑器·游戏引擎
前端开发与ui设计的老司机2 天前
从UI设计到数字孪生实战应用:构建智慧金融的风险评估与预警平台
ui·金融
小赵小赵福星高照~2 天前
iOS UI视图面试相关
ui·ios·面试
float_六七2 天前
Vue:构建高效UI的渐进式框架
javascript·vue.js·ui
Zillionnn2 天前
Unreal Engine 自动设置图像
游戏引擎·虚幻