Unity游戏开发——UnityUGUI——Button动态绑定事件

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 initialization
	void Start () {
        Button btn = GameObject.Find("Button").GetComponent<Button>();
        btn.onClick.AddListener(delegate ()
        {
            this.GotoGameSence();
        });
    }
	
	// Update is called once per frame
	void 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 initialization
	void 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 frame
	void Update () {
		
	}
    public void GotoGameSence()
    {
        print("调整");
        //pplication.LoadLevel(1);
        SceneManager.LoadScene(1);
    }
    public void OnButtonDown(BaseEventData arg0)
    {
        Debug.Log("down");
    }
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值