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");    }}




原创粉丝点击