Unity游戏开发——UnityUGUI——Button动态绑定事件
来源:互联网 发布:钢笔字 知乎 编辑:程序博客网 时间:2024/06/10 10:20
UGUI和NGUI一个作者写的所以很多东西基本类似,今天记录下UGUI动态绑定事件
UGUI支持的事件如下
Supported Events
The Eventsystem supports a number of events, and they can be customised further in user custom user written InputModules.
The events that are supported by the StandaloneInputModule and TouchInputModule are provided by interface and can be implemented on a MonoBehaviour by implementing the interface. If you have a valid EventSystem configured the events will be called at the correct time.
- IPointerEnterHandler - OnPointerEnter - Called when a pointer enters the object
- IPointerExitHandler - OnPointerExit - Called when a pointer exits the object
- IPointerDownHandler - OnPointerDown - Called when a pointer is pressed on the object
- IPointerUpHandler - OnPointerUp - Called when a pointer is released (called on the original the pressed object)
- IPointerClickHandler - OnPointerClick - Called when a pointer is pressed and released on the same object
- IInitializePotentialDragHandler - OnInitializePotentialDrag - Called when a drag target is found, can be used to initialise values
- IBeginDragHandler - OnBeginDrag - Called on the drag object when dragging is about to begin
- IDragHandler - OnDrag - Called on the drag object when a drag is happening
- IEndDragHandler - OnEndDrag - Called on the drag object when a drag finishes
- IDropHandler - OnDrop - Called on the object where a drag finishes
- IScrollHandler - OnScroll - Called when a mouse wheel scrolls
- IUpdateSelectedHandler - OnUpdateSelected - Called on the selected object each tick
- ISelectHandler - OnSelect - Called when the object becomes the selected object
- IDeselectHandler - OnDeselect - Called on the selected object becomes deselected
- IMoveHandler - OnMove - Called when a move event occurs (left, right, up, down, ect)
- ISubmitHandler - OnSubmit - Called when the submit button is pressed
- ICancelHandler - OnCancel - Called when the cancel button is pressed
常用的是动态实现Onclick事件
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;using UnityEngine.UI;using UnityEngine.Events;using UnityEngine.EventSystems;public class lv01 : MonoBehaviour { // Use this for initializationvoid Start () { Button btn = GameObject.Find("Button").GetComponent<Button>(); btn.onClick.AddListener(delegate () { this.GotoGameSence(); }); }// Update is called once per framevoid Update () {} public void GotoGameSence() { print("调整"); //pplication.LoadLevel(1); SceneManager.LoadScene(1); }}需要注意添加好命名空间。
UGUI还支持很多事件比如鼠标经过,离开,按下等,UGUI提供了一个EventTrigger 组件
这个也可以通过代码获取到
using System.Collections;using System.Collections.Generic;using UnityEngine;using UnityEngine.SceneManagement;using UnityEngine.UI;using UnityEngine.Events;using UnityEngine.EventSystems;public class lv01 : MonoBehaviour { // Use this for initializationvoid Start () { //Button btn = GameObject.Find("Button").GetComponent<Button>(); //btn.onClick.AddListener(delegate () //{ // this.GotoGameSence(); //}); var trigger = transform.gameObject.GetComponent<EventTrigger>(); if (trigger == null) trigger = transform.gameObject.AddComponent<EventTrigger>(); trigger.triggers = new List<EventTrigger.Entry>(); EventTrigger.Entry entry = new EventTrigger.Entry(); //entry.eventID = EventTriggerType.PointerDown;鼠标按下 entry.eventID = EventTriggerType.PointerEnter;//s鼠标经过 UnityAction<BaseEventData> callback = new UnityAction<BaseEventData>(OnButtonDown); entry.callback.AddListener(callback); trigger.triggers.Add(entry); }// Update is called once per framevoid Update () {} public void GotoGameSence() { print("调整"); //pplication.LoadLevel(1); SceneManager.LoadScene(1); } public void OnButtonDown(BaseEventData arg0) { Debug.Log("down"); }}
阅读全文
0 0
- Unity游戏开发——UnityUGUI——Button动态绑定事件
- Unity游戏开发——UnityUGUI打包图集
- Button动态绑定事件
- 动态绑定事件插件—livequery
- 动态绑定事件插件—livequery
- 动态绑定事件插件—livequery
- 动态绑定事件插件—livequery
- Unity开发—简单的RPG游戏
- Unity游戏开发——unity接入多渠道sdk
- 给button动态绑定click事件
- 6 动态事件绑定之 button测试
- innerHtml动态生成button并绑定事件
- Unity UGUI 动态添加Button.OnClick()事件
- DOM—事件绑定
- [unity] unity学习——弹球游戏
- Unity游戏开发——C#特性Attribute与自动化
- unity 5.x游戏开发指南——第一二三章
- Unity游戏开发—休闲类(有源代码)
- PHP错误异常处理详解
- Grid使用模板(template)出错 (备忘)
- 学习链接整理
- PHP安装,配置xdebuge,PhpStrom配置,自己写的
- Ubuntu14.04 安装opencv3.0中ippicv_linux_20141027.tgz终端命令窗口在线不能下载
- Unity游戏开发——UnityUGUI——Button动态绑定事件
- NET实现微信公共平台上传下载多媒体文件(转)
- vsftpd+pam+mysql
- spring boot整合redis
- Nodejs+Vuejs实现无限分类关系
- SEO新手必学:充分了解搜索引擎盈利模式
- 51单片机的中断系统(六)
- Use LCD Keyboard Shieid to show color sensor's data which received [1.2]
- Python购物流程结合ATM接口实现