Unity3D学习之----------------------------------UI控件回调挂载

来源:互联网 发布:55开 知乎 12.26 编辑:程序博客网 时间:2024/05/19 19:32

       本文将以游戏中最常见的Button为例介绍几种按钮触发回调的方式。




一、可视化创建及事件绑定

第一步:通过Hierarchy面板创建button,如图

ui-demo-1

第二步:创建一个脚本名为TestClick,并定义一个名为Click的public方法

?
1
2
3
4
5
6
7
8
9
10
using UnityEngine;
using System.Collections;
 
publicclass TestClick : MonoBehaviour {
 
publicvoid Click()
{
Debug.Log("Button Clicked");
}
}

第三步:选中Button ,Add Component选择TestClick

ui-demo-2

第四步:关联TestClick中的Click方法,如图

ui-demo-3      

ui-demo-4

然后运行,我们就能看到绑定的事件了




二、通过绑定脚本来绑定事件

首先,第一步,依旧在Hierarchy面板中创建一个Button

第二部,创建一个名为ClickHandler的脚本,代码如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
using System.Collections;
using UnityEngine.UI;
 
publicclass ClickHandler : MonoBehaviour
{
 
// Use this for initialization
void Start()
{
Button btn = this.GetComponent<Button>();
btn.onClick.AddListener(Onclick);
}
 
privatevoid Onclick()
{
Debug.Log("Button Clicked");
}
 
}

第三步,将ClickHandler绑定到Button对象上,如图



三.  统一的事件派发


我们会发现Unity4.6 UI新的UI系统中Button默认只提供了Onclick的调用方法,有时我们不仅仅想监听Button的Click事件,同时还需要监听MouseIn(鼠标进入事件)MouseOut(鼠标滑出事件).
这时我们就需要借助新的UI系统中EventTrigger脚本来实现,下面我们来学习一下,首先我们看一下如何通过EventTrigger来实现按钮点击Click事件.

第一步:通过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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
//需要EventTrigger脚本的支援
[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
publicclass EventHandler : MonoBehaviour
{
 
// Use this for initialization
voidStart()
{
 
Button btn = this.GetComponent<Button>();
UnityEngine.EventSystems.EventTrigger trigger = btn.gameObject.GetComponent<UnityEngine.EventSystems.EventTrigger>();
EventTrigger.Entry entry =new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;
entry.callback =new EventTrigger.TriggerEvent();
entry.callback.AddListener(Onclick);
 
trigger.delegates.Add(entry);
}
 
privatevoid Onclick(BaseEventData pointData)
{
Debug.Log("Button Clicked");
 
}
 
}

第三步,绑定EventHandler脚本到Button上

然后运行点击按钮就能看到运行的结果了。

如果我们想在Unity4.6 新的UI中实现MouseIn怎么办,很简单我们只需要修改下EventTriggerType.PointerClick事件类型改为EventTriggerType.PointerEnter,同理要监听MouseOut事件只需要修改为EventTriggerType.PointerExit

新的实现代码如下

?
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
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
 
//需要EventTrigger脚本的支援
[RequireComponent(typeof(UnityEngine.EventSystems.EventTrigger))]
publicclass EventHandler : MonoBehaviour
{
 
// Use this for initialization
voidStart()
{
 
Button btn = this.GetComponent<Button>();
UnityEngine.EventSystems.EventTrigger trigger = btn.gameObject.GetComponent<UnityEngine.EventSystems.EventTrigger>();
EventTrigger.Entry entry =new EventTrigger.Entry();
//鼠标点击事件
//entry.eventID = EventTriggerType.PointerClick;
//鼠标滑出事件
//entry.eventID = EventTriggerType.PointerExit;
//鼠标进入事件
entry.eventID = EventTriggerType.PointerEnter;
entry.callback =new EventTrigger.TriggerEvent();
entry.callback.AddListener(OnMouseEnter);
 
trigger.delegates.Add(entry);
}
 
privatevoid OnMouseEnter(BaseEventData pointData)
{
Debug.Log("Button Enter");
 
}
 
}




四、通过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;
publicclass EventHandler : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IDragHandler
{
publicvoid OnPointerClick(PointerEventData eventData)
{
if (eventData.pointerId == -1)
Debug.Log("Left Mouse Clicked");
if (eventData.pointerId == -2)
Debug.Log("Right Mouse Clicked");
}
 
publicvoid OnPointerEnter(PointerEventData eventData)
{
Debug.Log("Pointer Enter");
}
 
publicvoid OnPointerExit(PointerEventData eventData)
{
Debug.Log("Pointer Exit");
}
 
publicvoid OnPointerDown(PointerEventData eventData)
{
Debug.Log("Pointer Down");
}
 
publicvoid 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;
publicclass UIEventListener : MonoBehaviour, IPointerClickHandler, IPointerEnterHandler, IPointerExitHandler
{
 
/// <summary>
/// 定义事件代理
/// </summary>
/// <param name="gb"></param>
publicdelegate voidUIEventProxy(GameObject gb);
 
/// <summary>
/// 鼠标点击事件
/// </summary>
publicevent UIEventProxy OnClick;
 
/// <summary>
/// 鼠标进入事件
/// </summary>
publicevent UIEventProxy OnMouseEnter;
 
/// <summary>
/// 鼠标滑出事件
/// </summary>
publicevent UIEventProxy OnMouseExit;
 
publicvoid OnPointerClick(PointerEventData eventData)
{
if (OnClick != null)
OnClick(this.gameObject);
}
 
publicvoid OnPointerEnter(PointerEventData eventData)
{
if (OnMouseEnter != null)
OnMouseEnter(this.gameObject);
}
 
publicvoid 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;
 
publicclass 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);
};
}
}



0 0
原创粉丝点击