1.UGUI中支持的事件
MSDN链接:https://docs.unity3d.com/Manual/SupportedEvents.html
参考博文:http://blog.csdn.net/lyh916/article/details/44570503
2. 实现事件有两种方式:
a).编辑器:添加EventTrigger组件
b).代码:继承EventTrigger实现所需要的事件
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
using
System.Collections;
using
System.Collections.Generic;
using
UnityEngine;
using
UnityEngine.EventSystems;
public
class
CubeState : EventTrigger{
//====================================================================================
//其它系列:
//OnSubmit、OnCancel、OnDrop、OnMove、OnScroll
//====================================================================================
//PC平台下按Enter键
public
override
void
OnSubmit(BaseEventData eventData)
{
Debug.Log(
"调用OnSubmit..."
);
}
//PC平台下按ESC键
public
override
void
OnCancel(BaseEventData eventData)
{
Debug.Log(
"调用OnCancel..."
);
}
//鼠标松开时
public
override
void
OnDrop(PointerEventData eventData)
{
Debug.Log(
"调用OnDrop..."
);
}
//物体在移动时(未测试)
public
override
void
OnMove(AxisEventData eventData)
{
Debug.Log(
"调用OnMove..."
);
}
//鼠标滚轮滚动的时候(未测试)
public
override
void
OnScroll(PointerEventData eventData)
{
Debug.Log(
"调用OnScroll..."
);
}
//====================================================================================
//Select系列:
//OnSelect、OnUpdateSelected、OnDeselect
//====================================================================================
//当被选中的时候调用一次
public
override
void
OnSelect(BaseEventData eventData)
{
Debug.Log(
"调用OnSelect..."
);
}
//在选中的时候每帧都会被调用
public
override
void
OnUpdateSelected(BaseEventData eventData)
{
//Debug.Log("调用OnUpdateSelected...");
}
//物体从选中状态到取消选中状态时会被调用
public
override
void
OnDeselect(BaseEventData eventData)
{
Debug.Log(
"调用OnDeselect..."
);
}
//====================================================================================
//Pointer系列:
//执行先后顺序:
//OnPointerDown>OnPointerEnter>OnPointerClick>OnPointerUp>OnPointerExit
//====================================================================================
//初始化拖拽时
public
override
void
OnInitializePotentialDrag(PointerEventData eventData)
{
Debug.Log(
"调用OnInitializePotentialDrag..."
);
}
//指针刚开始点击之后点击之后第一时间时
public
override
void
OnPointerEnter(PointerEventData eventData)
{
Debug.Log(
"调用OnPointerEnter..."
);
}
//在同一个物体上点击
public
override
void
OnPointerClick(PointerEventData eventData)
{
Debug.Log(
"调用OnPointerClick..."
);
}
//鼠标抬起
public
override
void
OnPointerUp(PointerEventData eventData)
{
Debug.Log(
"调用OnPointerUp..."
);
}
//鼠标按下
public
override
void
OnPointerDown(PointerEventData eventData)
{
Debug.Log(
"调用OnPointerDown..."
);
}
//结束的时候
public
override
void
OnPointerExit(PointerEventData eventData)
{
Debug.Log(
"调用OnPointerExit..."
);
}
//====================================================================================
//拖拽系列:
//OnBeginDrag、OnDrag、OnEndDrag
//====================================================================================
//开始拖拽
public
override
void
OnBeginDrag(PointerEventData eventData)
{
Debug.Log(
"调用OnBeginDrag..."
);
//Debug.Log("pointerPress:"+eventData.pointerPress);
//Debug.Log("pressPosition;" + eventData.pressPosition);
ChangePositionByDrag(eventData);
}
//拖拽过程中
public
override
void
OnDrag(PointerEventData eventData)
{
//Debug.Log("调用OnDrag...");
ChangePositionByDrag(eventData);
}
//结束拖拽
public
override
void
OnEndDrag(PointerEventData eventData)
{
Debug.Log(
"调用OnEndDrag..."
);
ChangePositionByDrag(eventData);
}
//拖过拖拽变更自身坐标
void
ChangePositionByDrag(PointerEventData eventData) {
var
rt = gameObject.GetComponent<RectTransform>();
Vector3 globalMousePos;
if
(RectTransformUtility.ScreenPointToWorldPointInRectangle(rt, eventData.position, eventData.pressEventCamera,
out
globalMousePos))
{
rt.position = globalMousePos;
}
}
}
|
3.将事件究极封装起来
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
using
UnityEngine;
using
System.Collections;
using
UnityEngine.EventSystems;
public
class
EventTriggerListener : EventTrigger {
#region 变量
//带参数是为了方便取得绑定了UI事件的对象
public
delegate
void
PointerEventDelegate(GameObject go, PointerEventData eventData);
public
delegate
void
BaseEventDelegate(GameObject go, BaseEventData eventData);
public
delegate
void
AxisEventDelegate(GameObject go, AxisEventData eventData);
public
event
PointerEventDelegate onPointerEnter;
public
event
PointerEventDelegate onPointerExit;
public
event
PointerEventDelegate onPointerDown;
public
event
PointerEventDelegate onPointerUp;
public
event
PointerEventDelegate onPointerClick;
public
event
PointerEventDelegate onInitializePotentialDrag;
public
event
PointerEventDelegate onBeginDrag;
public
event
PointerEventDelegate onDrag;
public
event
PointerEventDelegate onEndDrag;
public
event
PointerEventDelegate onDrop;
public
event
PointerEventDelegate onScroll;
public
event
BaseEventDelegate onUpdateSelected;
public
event
BaseEventDelegate onSelect;
public
event
BaseEventDelegate onDeselect;
public
event
AxisEventDelegate onMove;
public
event
BaseEventDelegate onSubmit;
public
event
BaseEventDelegate onCancel;
#endregion
public
static
EventTriggerListener GetListener(GameObject go)
{
EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
if
(listener ==
null
) listener = go.AddComponent<EventTriggerListener>();
return
listener;
}
#region 方法
public
override
void
OnPointerEnter(PointerEventData eventData)
{
if
(onPointerEnter !=
null
) onPointerEnter(gameObject, eventData);
}
public
override
void
OnPointerExit(PointerEventData eventData)
{
if
(onPointerExit !=
null
) onPointerExit(gameObject, eventData);
}
public
override
void
OnPointerDown(PointerEventData eventData)
{
if
(onPointerDown !=
null
) onPointerDown(gameObject, eventData);
}
public
override
void
OnPointerUp(PointerEventData eventData)
{
if
(onPointerUp !=
null
) onPointerUp(gameObject, eventData);
}
public
override
void
OnPointerClick(PointerEventData eventData)
{
if
(onPointerClick !=
null
) onPointerClick(gameObject, eventData);
}
public
override
void
OnInitializePotentialDrag(PointerEventData eventData)
{
if
(onInitializePotentialDrag !=
null
) onInitializePotentialDrag(gameObject, eventData);
}
public
override
void
OnBeginDrag(PointerEventData eventData)
{
if
(onBeginDrag !=
null
) onBeginDrag(gameObject, eventData);
}
public
override
void
OnDrag(PointerEventData eventData)
{
if
(onDrag !=
null
) onDrag(gameObject, eventData);
}
public
override
void
OnEndDrag(PointerEventData eventData)
{
if
(onEndDrag !=
null
) onEndDrag(gameObject, eventData);
}
public
override
void
OnDrop(PointerEventData eventData)
{
if
(onDrop !=
null
) onDrop(gameObject, eventData);
}
public
override
void
OnScroll(PointerEventData eventData)
{
if
(onScroll !=
null
) onScroll(gameObject, eventData);
}
public
override
void
OnUpdateSelected(BaseEventData eventData)
{
if
(onUpdateSelected !=
null
) onUpdateSelected(gameObject, eventData);
}
public
override
void
OnSelect(BaseEventData eventData)
{
if
(onSelect !=
null
) onSelect(gameObject, eventData);
}
public
override
void
OnDeselect(BaseEventData eventData)
{
if
(onDeselect !=
null
) onDeselect(gameObject, eventData);
}
public
override
void
OnMove(AxisEventData eventData)
{
if
(onMove !=
null
) onMove(gameObject, eventData);
}
public
override
void
OnSubmit(BaseEventData eventData)
{
if
(onSubmit !=
null
) onSubmit(gameObject, eventData);
}
public
override
void
OnCancel(BaseEventData eventData)
{
if
(onCancel !=
null
) onCancel(gameObject, eventData);
}
#endregion
}
|
调用:
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
|
using
UnityEngine;
using
System.Collections;
using
UnityEngine.EventSystems;
public
class
TestUI3 : MonoBehaviour {
private
GameObject button;
private
GameObject image;
void
Start ()
{
button = GameObject.Find(
"Button"
);
image = GameObject.Find(
"Image"
);
EventTriggerListener.GetListener(button).onPointerClick += Click;
EventTriggerListener.GetListener(image).onDrag += Drag;
}
public
void
Click(GameObject go)
{
Debug.Log(go.name);
}
public
void
Drag(GameObject go)
{
Debug.Log(go.name);
}
}
|