unity_每日随笔_MyButton
来源:互联网 发布:回收站数据恢复 免费 编辑:程序博客网 时间:2024/06/05 09:09
1,继承自unity自带的Button组件。
2,三个回调函数,分别是:按下,抬起,是否长按(长按+点击)
3,判断长按的时间:使用Invoke函数做判断。
4,重写Button类的按下和抬起函数,做为触发机制。
代码如下:
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class MyButton : Button {
public float m_fLongPress;
public delegate void VoidDelegate();
public VoidDelegate onDown;
public VoidDelegate onUp;
public delegate void BoolDelegate(bool bClickOrPress);
public BoolDelegate onLongPress;
private bool isPress;
void Start() {
m_fLongPress = 1f;
isPress = false;
}
public override void OnPointerDown(PointerEventData eventData)
{
base.OnPointerDown(eventData);
if (this.interactable == false)
return;
if (onLongPress != null)
1,如果按下,那一定是点击或者长按
isPress = true;
Invoke("onLongPressFun", m_fLongPress);
if (onDown != null)
onDown();
}
public override void OnPointerUp(PointerEventData eventData)
{
base.OnPointerUp(eventData);
if (this.interactable == false)
return;
CancelInvoke("onLongPressFun");
1,如果已经触发长按了,那一定为假,如果没有,则一定为真
2,前提是有按下才会有抬起,所以isPress初始化由down控制
if (isPress)
{
isPress = false;
if (onLongPress != null)
onLongPress(false);
}
if (onUp != null)
onUp();
}
public void onLongPressFun() {
if (isPress)
{
isPress = false;
if (onLongPress != null)
onLongPress(true);
CancelInvoke("onLongPressFun");
}
}
}
调用代码如下:
void Start () {
MyButton tmpBtn = transform.GetComponent<MyButton>();
tmpBtn.onDown = () => { print("i am down"); };
tmpBtn.onUp = () => { print("i am up"); };
tmpBtn.onLongPress = (bool b) =>
{
1,是点击还是长按的判断
if (b)
print("is longPress");
else
print("is click");
};
}
- unity_每日随笔_MyButton
- unity_每日随笔_TimeHelp计时器
- unity_随笔_MyDelay
- 每日心得随笔
- 每日随笔131117
- 每日随笔(一)
- 每日随笔_Animation_Animator
- 每日随笔_Unity Profiler
- 每日随笔_RenderTexture
- 每日随笔_代码优化
- 每日随笔小记-4月20日
- Unity_协程
- 写个自己看的博客_每日随笔_unity_particleSystem
- 写给自己看的博客_每日随笔_json
- UNITY_引擎_UGUI
- Unity_角色控制详解
- Unity_事件函数
- Unity_游戏源码
- python的I/O操作
- 匿名内部类的创建原理
- Android_浅谈
- 【Web】vue2.0音乐APP实战中的知识点总结(二)
- linux
- unity_每日随笔_MyButton
- Crash 现场分析
- mybatis3中@SelectProvider的使用技巧
- 蓝牙知识【协议栈简图】 【 UUID解析 】 【属性、Characteris、描述符和Service以及profile的关系】【蓝牙无线通道】【缩写词】
- nginx之php,及在lnmp环境下的论坛
- E-POJ-3087 Shuffle'm Up
- J
- matlab之cell的清空
- [KMP][字符串Hash] #93 div1 cf 126B Password