服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

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# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03