Unity3D仿写Button面板事件绑定功能
本文实例为大家分享了unity3d仿写button面板事件绑定功能的具体代码,供大家参考,具体内容如下
最近在做一个情节引导得项目。其中一个需求特点是:每一步都要显示类似的信息,不同的是,每一次要去引导玩家玩的东西不同。比如:第一步需要显示物体1,第二步需要显示物体2,区别就是在相同的脚本调用不同的函数。我们不可能为了每一次不同的设置写不同的脚本,如果一千多步,难道要写一千多个脚本?突然想要unity 的ugui中button事件的绑定是一个好的解决方案。于是我上网搜索了button的源代码。如下:
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添加事件。于是我们可以照猫画虎择取我们自己需要的部份写我们自己需要的事件绑定。代码如下:
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(); } } }
我们将脚本挂到空物体上,效果如下:
使用方法如下例子:
我们自己写一个步骤设置器,代码如下:
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效果的初级编写。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。