首页 > 编程知识 正文

unity3d中实现ui,Unity中怎么给按钮挂代码

时间:2023-05-05 16:24:49 阅读:139275 作者:2345

一. UI渲染模式:

1、屏幕空间覆盖直接在原始三维场景上叠加2D场景

2 .在“Screen Space-Camera (指定相机)”UI上显示3D模型

3、World Space (指定摄像头) UI是游戏场景的一部分,是VR中常用的方式

二.弹出框

1、制作Image,改名为Windows,上传第一张图片

2、头像锯齿感很强,用精灵修正。 首先,新版本在Package Manager中下载

然后在图像视图面板中单击Sprite Editor

将边框更改为12

这样图像就会比没有锯齿感显示得更好

创建新脚本(SettingPop )。 脚本包含两种方法:弹出框的弹出窗口和表示非弹出窗口状态的Open )和Close )。

using System.Collections; using system.collections.generic; using UnityEngine; publicclasssettingpopup : mono behaviour { public void open () } { game object.set active (true ); } public void Close () gameobject.setactive ) false; (/startiscalledbeforethefirstframeupdatevoidstart ) (}/updateiscalledonceperframevoidupdate ) )在不修改UIController的情况下然后,在Start方法中调用SettingPopup的Close (),以避免在开始时出现弹出框。 然后,用OnOpenSetting ()方法设置settingPop.Open;

using System.Collections; using system.collections.generic; using UnityEngine; using UnityEngine.UI; public class uicontroller : mono behaviour { [ serialize field ] privatetextscorelabel; [ serialize field ] privatesettingpopupsettingpop; //startiscalledbeforethefirstframeupdatevoidstart () { settingPop.Close ); //updateiscalledonceperframevoidupdate () score label.text=time.realtimesincestartup.tostring ); } public void OnOpenSetting () { settingPop.Open ); } public void OnPointerDown () (debug.log ) (pointerdown ); 在Windows下创建新的Button,并将其放置在适当的位置

然后进行关联

UIController的UIController脚本的设置点拖动Windows形成关联

在要单击显示弹出框的按钮的上面的On Click () (事件中添加一个名为OnOpenSetting ) )的方法

然后在那个红色叉按钮处放入Close (方法)

选择方法时有两种类型的按钮:静态参数和动态字符串。 动态字符串输入时什么都提交; 静态参数只能发送预定义的一个字符串才能在代码中预声明字符串。 一般来说,使用动态字符串比较灵活

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。