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
需要注意添加好命名空间。

UGUI还支持很多事件比如鼠标经过,离开,按下等,UGUI提供了一个EventTrigger 组件

Unity游戏开发——UnityUGUI——Button动态绑定事件_第1张图片

这个也可以通过代码获取到

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




你可能感兴趣的:(Unity游戏开发——UnityUGUI——Button动态绑定事件)