UnityEngine.UI

601 阅读2分钟

#Class Button

  • Source Code :
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace UnityEngine.UI
{
    [AddComponentMenu("UI/Button", 30)]
    public class Button : Selectable, IPointerClickHandler, IEventSystemHandler, ISubmitHandler
    { // Class Button通过实现接口的 
        protected Button();   //  构造函数 

        public ButtonClickedEvent onClick { get; set; }  // UnityEvent事件 ,响应鼠标点击 ;

        public virtual void OnPointerClick(PointerEventData eventData);
        public virtual void OnSubmit(BaseEventData eventData);

        public class ButtonClickedEvent : UnityEvent
        {
            public ButtonClickedEvent();
        }
    }
}

对源代码的解析 :

Canvas中的Buttton控件 ,在C#脚本中被定义为类Button ;

Button继承了接口IPointerClickHandler,但是并没有直接实现 OnPointerClick ,而是把其实现为虚方法,从而子类就必须实现它(定义其回调函数体) ;

  • Properties - onClick 当你按下Button时 , 便触发了这个UnityEvent(点击事件) ,从而执行与此相关的回调函数 ;onClick属性指向了一个ButtonClickedEvent类型的对象 ,用于定义Unity引擎在响应Button被点击时的规范 ,通过AddListener方法来设置响应函数 ;

示例 :

using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;

 public class ClickExample : MonoBehaviour
 {
     public Button yourButton;

     void Start()
     {
         Button btn = yourButton.GetComponent<Button>();
         btn.onClick.AddListener(TaskOnClick);  
     }

     void TaskOnClick()
     {
         Debug.Log("You have clicked the button!");
     }
 }
  • Methods - OnPointerClick(PointerEventData) 调用被点击对象上所有已存在的 IPointerClickHandlers . 通过IPointClickHandlers也可以记录与点击事件相关的信息 , 也可以用于判断是鼠标上的哪一个按键被点击了(左,中,右)。上述所有功能的前提:场景中必须有一个EventSystem ;

示例 :

//Attatch this script to a Button GameObject
using UnityEngine;
using UnityEngine.EventSystems;

public class Example : MonoBehaviour, IPointerClickHandler
{    // IPointerClickHandler提供了OnPointerClick(PointerEventData pointerEventData) ;
    //Detect if a click occurs
    public void OnPointerClick(PointerEventData pointerEventData)
    {
            //Use this to tell when the user right-clicks on the Button
        if (pointerEventData.button == PointerEventData.InputButton.Right)
        {
            //Output to console the clicked GameObject's name and the following message. You can replace this with your own actions for when clicking the GameObject.
            Debug.Log(name + " Game Object Right Clicked!");
        }

        //Use this to tell when the user left-clicks on the Button
        if (pointerEventData.button == PointerEventData.InputButton.Left)
        {
            Debug.Log(name + " Game Object Left Clicked!");
        }
    }
}

每一个鼠标点击事件都会产生一个PointerEventData类型的对象 ,储存了与点击相关的各种信息数据 ;

PointerEventData.InputButton是一个枚举类型 ,记录了鼠标的左、中、右键 ;

PointerEventData类的属性button,是PointerEventData.InputButton类型的,记录了是鼠标的哪一个键按下了 ;

### 通过上述两种途径都可以对鼠标点击做出响应 。区别在于通过 onClick.AddListener()仅可以设置一个用于响应事件的回调函数 ,而通过 OnPointerClick(PointerEventData pointerEventData)的参数可以获得相关的点击信息(储存在pointEventData里),例如鼠标是否还在移动、指针坐标等等。所以前者可以看成是后者的功能简化版 ;获得更多关于OnPointerClick(PointerEventData pointerEventData)的是使用说明:http://docs.unity3d.com/Packages/com.unity.ugui@1.0/api/UnityEngine.EventSystems.PointerEventData.html