【UGUI】Unity4.6 UI按鈕綁定事件(四)

閱讀這段Unity3d的官方文檔我們會發現Unity4.6 UI,有一種更簡單的方式來監聽Button按鈕的點擊,MouseIn鼠標滑入,MouseOut鼠標滑出等事件,那就是我們我們可以通過實現各個事件的接口類來自定義事件。

三、通過MonoBehaviour 來實現事件類接口來實現事件的監聽

第一步:通過Hierarchy面板創建button(詳細參考Unity4.6 UI按鈕綁定事件(一))

第二步:創建一個名爲EventHandler的腳本,代碼如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class EventHandler : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IDragHandler
{
public void OnPointerClick(PointerEventData eventData)
{
if (eventData.pointerId == -1)
Debug.Log("Left Mouse Clicked");
if (eventData.pointerId == -2)
Debug.Log("Right Mouse Clicked");
}
 
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Pointer Enter");
}
 
public void OnPointerExit(PointerEventData eventData)
{
Debug.Log("Pointer Exit");
}
 
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Pointer Down");
}
 
public void OnDrag(PointerEventData eventData)
{
Debug.Log("Dragged");
}
}

第三步:將腳本綁定到Button對象上(詳細參考Unity4.6 UI按鈕綁定事件(三)中圖片介紹)

然後運行,我們就能看到各個事件被實現了

Unity4.6 UI(UGUI)如何判斷UI元素被點擊時是鼠標哪個按鍵,上面代碼中我們可以根據eventData.pointerId來監聽是我們按下的是鼠標左鍵還是右鍵。

通過前面幾部分學習我們已經實現對Unity4.6 UI新的UI系統如何綁定事件做了大概講解,但是弊端明顯,就是每個UI元素都創建一個MonoBehavior來進行監聽各個事件,顯然這樣做不行,下面我們來學習下利用Delegate和Event來做一個通用類UIEventListener來處理事件(不瞭解Delegate和Event的童鞋請自行谷歌搜索觀察者模式),好了不廢話了,下面貼上代碼

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
 
/// <summary>
/// 定義事件代理
/// </summary>
/// <param name="gb"></param>
public delegate void UIEventProxy(GameObject gb);
 
/// <summary>
/// 鼠標點擊事件
/// </summary>
public event UIEventProxy OnClick;
 
/// <summary>
/// 鼠標進入事件
/// </summary>
public event UIEventProxy OnMouseEnter;
 
/// <summary>
/// 鼠標滑出事件
/// </summary>
public event UIEventProxy OnMouseExit;
 
public void OnPointerClick(PointerEventData eventData)
{
if (OnClick != null)
OnClick(this.gameObject);
}
 
public void OnPointerEnter(PointerEventData eventData)
{
if (OnMouseEnter != null)
OnMouseEnter(this.gameObject);
}
 
public void OnPointerExit(PointerEventData eventData)
{
if (OnMouseExit != null)
OnMouseExit(this.gameObject);
}
}

 

下面我們來看下調用的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
 
// Use this for initialization
void Start()
{
Button btn = this.GetComponent<Button>();
UIEventListener btnListener = btn.gameObject.AddComponent<UIEventListener>();
btnListener.OnClick += delegate(GameObject gb)
{
Debug.Log(gb.name);
};
btnListener.OnMouseEnter += delegate(GameObject gb)
{
Debug.Log(gb.name);
};
btnListener.OnMouseExit += delegate(GameObject gb)
{
Debug.Log(gb.name);
};
}
}

關於Unity New UI中的按鈕綁定事件的介紹就到此結束,歡迎您的圍觀,不足之處,敬請留言。

發佈了6 篇原創文章 · 獲贊 11 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章