本文實例為大家分享了unity3d仿寫button面板事件綁定功能的具體代碼,供大家參考,具體內容如下
最近在做一個情節引導得項目。其中一個需求特點是:每一步都要顯示類似的信息,不同的是,每一次要去引導玩家玩的東西不同。比如:第一步需要顯示物體1,第二步需要顯示物體2,區別就是在相同的腳本調用不同的函數。我們不可能為了每一次不同的設置寫不同的腳本,如果一千多步,難道要寫一千多個腳本?突然想要unity 的ugui中button事件的綁定是一個好的解決方案。于是我上網搜索了button的源代碼。如下:
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
|
using system; using system.collections; using unityengine.events; using unityengine.eventsystems; using unityengine.serialization; namespace unityengine.ui { // button that's meant to work with mouse or touch-based devices. [addcomponentmenu( "ui/button" , 30)] public class button : selectable, ipointerclickhandler, isubmithandler { [serializable] /// <summary> /// function definition for a button click event. /// </summary> public class buttonclickedevent : unityevent {} // event delegates triggered on click. [formerlyserializedas( "onclick" )] [serializefield] private buttonclickedevent m_onclick = new buttonclickedevent(); protected button() {} /// <summary> /// unityevent that is triggered when the button is pressed. /// note: triggered on mouseup after mousedown on the same object. /// </summary> ///<example> ///<code> /// 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!"); /// } /// } ///</code> ///</example> public buttonclickedevent onclick { get { return m_onclick; } set { m_onclick = value; } } private void press() { if (!isactive() || !isinteractable()) return ; uisystemprofilerapi.addmarker( "button.onclick" , this ); m_onclick.invoke(); } /// <summary> /// call all registered ipointerclickhandlers. /// register button presses using the ipointerclickhandler. you can also use it to tell what type of click happened (left, right etc.). /// make sure your scene has an eventsystem. /// </summary> /// <param name="eventdata">pointer data associated with the event. typically by the event system.</param> /// <example> /// <code> /// //attatch this script to a button gameobject /// using unityengine; /// using unityengine.eventsystems; /// /// public class example : monobehaviour, ipointerclickhandler /// { /// //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!"); /// } /// } /// } /// </code> /// </example> public virtual void onpointerclick(pointereventdata eventdata) { if (eventdata.button != pointereventdata.inputbutton.left) return ; press(); } /// <summary> /// call all registered isubmithandler. /// </summary> /// <param name="eventdata">associated data with the event. typically by the event system.</param> /// <remarks> /// this detects when a button has been selected via a "submit" key you specify (default is the return key). /// /// to change the submit key, either: /// /// 1. go to edit->project settings->input. /// /// 2. next, expand the axes section and go to the submit section if it exists. /// /// 3. if submit doesn't exist, add 1 number to the size field. this creates a new section at the bottom. expand the new section and change the name field to “submit”. /// /// 4. change the positive button field to the key you want (e.g. space). /// /// /// or: /// /// 1. go to your eventsystem in your project /// /// 2. go to the inspector window and change the submit button field to one of the sections in the input manager (e.g. "submit"), or create your own by naming it what you like, then following the next few steps. /// /// 3. go to edit->project settings->input to get to the input manager. /// /// 4. expand the axes section in the inspector window. add 1 to the number in the size field. this creates a new section at the bottom. /// /// 5. expand the new section and name it the same as the name you inserted in the submit button field in the eventsystem. set the positive button field to the key you want (e.g. space) /// </remarks> public virtual void onsubmit(baseeventdata eventdata) { press(); // if we get set disabled during the press // don't run the coroutine. if (!isactive() || !isinteractable()) return ; dostatetransition(selectionstate.pressed, false ); startcoroutine(onfinishsubmit()); } private ienumerator onfinishsubmit() { var fadetime = colors.fadeduration; var elapsedtime = 0f; while (elapsedtime < fadetime) { elapsedtime += time.unscaleddeltatime; yield return null ; } dostatetransition(currentselectionstate, false ); } } } |
代碼其實挺簡單的,主要是自己new 一個新的unityevent,然后我們在外界調用。這個unityevent通過序列化顯示在面板上。我們可以通過兩種方式綁定事件。第一種就是在面板綁定,第二種就是addlistener添加事件。于是我們可以照貓畫虎擇取我們自己需要的部份寫我們自己需要的事件綁定。代碼如下:
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
|
using system.collections; using system.collections.generic; using unityengine; using unityengine.events; using unityengine.eventsystems; using unityengine.serialization; using system; namespace myevent { public class mycompotent : monobehaviour { [serializable] public class mycompontentevent:unityevent{ } [formerlyserializedas( "myevent" )] [serializefield] private mycompontentevent myevent = new mycompontentevent(); public mycompontentevent myevent { get { return myevent; } set { myevent = value; } } //執行綁定的事件 public void excuteevent() { myevent.invoke(); } } } |
我們將腳本掛到空物體上,效果如下:
使用方法如下例子:
我們自己寫一個步驟設置器,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
namespace myevent { public class stepcontrol : mycompotent { public string name; public int index; private void start() { //設置名字 //設置index //去做每一步應該設置得事情 excuteevent(); } } } |
我們在stepcontrol里設置相同的操作。不同的操作通過面板綁定讓excuteevent()去執行。使用unity自帶的消息事件會讓我們開發輕松很多。如果不使用unityevent,我們也可以在stepcontrol聲明一個我們自己的委托,但是去調用小的操作的時候會需要多寫一點代碼。比如 我們想讓某個物體隱藏,需要單獨寫一個腳本設置物體隱藏并且將函數綁定到此stepcontrol的委托上。而在unityevent里可以直接通過面板操作實現。
以上為我的博客內容。有錯誤歡迎指點!
今年一定要學會代碼重新建模+shader效果的初級編寫。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/qq_33994566/article/details/87690690