激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - C# - Unity3D仿寫Button面板事件綁定功能

Unity3D仿寫Button面板事件綁定功能

2022-03-10 14:11qq_33994566 C#

這篇文章主要為大家詳細介紹了Unity3D仿寫Button面板事件綁定功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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();
  }
 }
}

我們將腳本掛到空物體上,效果如下:

Unity3D仿寫Button面板事件綁定功能

使用方法如下例子:

我們自己寫一個步驟設置器,代碼如下:

?
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里可以直接通過面板操作實現。

Unity3D仿寫Button面板事件綁定功能

以上為我的博客內容。有錯誤歡迎指點!

今年一定要學會代碼重新建模+shader效果的初級編寫。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/qq_33994566/article/details/87690690

延伸 · 閱讀

精彩推薦
  • C#C#裁剪,縮放,清晰度,水印處理操作示例

    C#裁剪,縮放,清晰度,水印處理操作示例

    這篇文章主要為大家詳細介紹了C#裁剪,縮放,清晰度,水印處理操作示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    吳 劍8332021-12-08
  • C#C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    C# 實現對PPT文檔加密、解密及重置密碼的操作方法

    這篇文章主要介紹了C# 實現對PPT文檔加密、解密及重置密碼的操作方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下...

    E-iceblue5012022-02-12
  • C#Unity3D實現虛擬按鈕控制人物移動效果

    Unity3D實現虛擬按鈕控制人物移動效果

    這篇文章主要為大家詳細介紹了Unity3D實現虛擬按鈕控制人物移動效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一...

    shenqingyu060520232410972022-03-11
  • C#WPF 自定義雷達圖開發實例教程

    WPF 自定義雷達圖開發實例教程

    這篇文章主要介紹了WPF 自定義雷達圖開發實例教程,本文介紹的非常詳細,具有參考借鑒價值,需要的朋友可以參考下...

    WinterFish13112021-12-06
  • C#深入解析C#中的交錯數組與隱式類型的數組

    深入解析C#中的交錯數組與隱式類型的數組

    這篇文章主要介紹了深入解析C#中的交錯數組與隱式類型的數組,隱式類型的數組通常與匿名類型以及對象初始值設定項和集合初始值設定項一起使用,需要的...

    C#教程網6172021-11-09
  • C#C#通過KD樹進行距離最近點的查找

    C#通過KD樹進行距離最近點的查找

    這篇文章主要為大家詳細介紹了C#通過KD樹進行距離最近點的查找,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    帆帆帆6112022-01-22
  • C#C#實現XML文件讀取

    C#實現XML文件讀取

    這篇文章主要為大家詳細介紹了C#實現XML文件讀取的相關代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下...

    Just_for_Myself6702022-02-22
  • C#C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題實例

    這篇文章主要介紹了C#設計模式之Visitor訪問者模式解決長隆歡樂世界問題,簡單描述了訪問者模式的定義并結合具體實例形式分析了C#使用訪問者模式解決長...

    GhostRider9502022-01-21
主站蜘蛛池模板: 欧美性受xxxx白人性爽 | 国产精品久久久久久久不卡 | 久久久久久久久久久久久久国产 | 美女久久久久久久久 | 久久精品国产亚洲7777小说 | 国产超碰人人爽人人做人人爱 | 中文在线观看www | 911网站大全在线观看 | 成人免费一区二区三区视频网站 | 午夜视频在线看 | 激情国产视频 | 91一区二区三区久久久久国产乱 | 杏美月av | 精品国产一区二区三区免费 | 日韩视频在线不卡 | 国产成人高清成人av片在线看 | 久久中出| 午夜激情视频免费 | 免费一级毛片在线播放视频 | 久久福利在线 | 免费三级大片 | 免费毛片视频播放 | 欧美成人精品一区 | 91网视频 | 1级毛片在线观看 | 国产在线看一区 | 黄色网址在线视频 | 欧美 日韩 国产 在线 | 91精品视频在线看 | 99久久久国产精品免费观看 | 国产一区精品在线观看 | 国产成人综合在线观看 | 成人国产精品久久久 | 91精品成人福利在线播放 | 国产精品一区在线免费观看 | 福利在线免费 | 欧美精品一区二区中文字幕 | 九九热精 | 91高清视频 | 激情宗合网 | 欧美成人高清视频 |