Unity3D仿写Button面板事件绑定功能
作者:qq_33994566 发布时间:2023-03-14 10:38:14
本文实例为大家分享了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效果的初级编写。
来源:https://blog.csdn.net/qq_33994566/article/details/87690690
猜你喜欢
- 先看看效果图:package com.fenghuo.struts.download;import java.net.URLEncoder;
- 核心配置文件mybatis-config.xml 系统核心配置文件MyBatis 的配置文件包含了会深深影响 MyBatis 行为的设置和属
- 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再
- 在主Activity中:listview=(ListView)findViewById(R.id.listview);getData();/
- Java裁剪压缩PNG图片,透明背景色变黑import java.awt.Graphics2D;import java.awt.Image;
- 最近要给一个 Winform 项目添加功能,需要一个能显示进度条的弹窗,还要求能够中止任务,所以就做了一个,在此做个记录总结。虽然用的是比较
- 成功本文通过java语言实现ECC+AES混合加密。ECC加密算法具有密钥分配与管理简单,安全强度高等优点,AES的加密算法具有速度快,强度
- 在使用C#进行桌面应用开发中,经常会有对文件进行操作的情况,这时可能会需要对文件夹进行文件扫描,获取所有文件做法如下/// <summ
- 在IntelliJ IDEA 中这个查看一个类也就是当前类的所有继承关系,包括实现的所有的接口和继承的类,这个继承,不仅仅是一级的继承关系,
- 前言LocalDateTime、LocalDate、LocalTime 是 Java8 全新的日期框架,加强了对时间的管理,有很多特别好用的
- 一、String类概述String类代表字符串,java程序中的所有字符串文字(例如"abc")都被实现为此类的实例。也
- 问题:写了一个新的dao接口,进行单元测试时提示:Initialization of bean failed; nested excepti
- 实现的功能1.导入非xls和xlsx格式的文件2.导入空数据的excel文件3.数据缺失4.导入的excel文件中有重复的数据5.导入的ex
- 最近在做关于PDF文档添加水印的功能,折腾了好久,终于好了。以下做个记录:首先会用到iTextSharp组件,大家可以去官网下载,同时我也会
- 用Java编写简单的五子棋,供大家参考,具体内容如下前言这两天在空闲时间做了个五子棋项目,分享给大家看一下,界面是这样的:界面很丑我知道,本
- 模板方法模式模板方法模式法(Template Method)定义一个操作中的算法骨架,而将算法的一些步骤延迟到子类中,使得子类可以不改变该算
- 1、return语句的作用:a、返回一个值,这个值可以是任意类型。b、使程序返回到操作系统(即终止程序)2、java中对于一个函数,不论有没
- 我在 android里面 使用html5的 localStorage 为什么存不进去也读不出来呀?网上搜了好多都没效果mainWebView
- 什么是分布式锁?在回答这个问题之前,我们先回答一下什么是锁。普通的锁,即在单机多线程环境下,当多个线程需要访问同一个变量或代码片段时,被访问
- 线性表是其组成元素间具有线性关系的一种数据结构,对线性表的基本操作主要有,获取元素,设置元素值,遍历,插入,删除,查找,替换,排序等。而线性