上一篇中提到一种鼠标按下时的事件触发,即采用eventtrigger设定pointerdown和pointerup并绑定相应事件。但是若要实现持续按键则需要对绑定的每个方法都添加实现持续按键方法。所以在此通过unityevent来简化过程。

(一)unityevent

unityevent为unity自定义的unity事件,需要与委托unityaction(它需要添加到event的监听中使用)。

以下为例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events; public class UniytEventTest : MonoBehaviour { UnityAction action;
[SerializeField]
UnityEvent actionEvent = new UnityEvent(); public void OnTest01()
{
print("test01");
}
public void OnTest02()
{
print("test02");
}
// Use this for initialization
void Start () {
action = new UnityAction(OnTest01);
action += OnTest02; if (action != null)
{
actionEvent.AddListener(action);
}
actionEvent.Invoke();
} // Update is called once per frame
void Update () { }
}

首先unityevent的特性声明

 [SerializeField]

表示此事件会出现在unity中的面板上,可以绑定事件,否则不可以。定义好事件以后就可以通过invoke方法激活一次。在此多说明一点,若通过代码绑定方法,unityaction默认为无参数的,若果需要带参数则需要通过泛型unityaction<T>来实现

(二)持续按键

通过unityEvent来实现持续按键,按键时事件触发时间间隔为0.1s

代码如下:

using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using System.Collections;
using UnityEngine.UI; public class ConstantPressEvent : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
public float interval = 0.1f; [SerializeField]
UnityEvent m_OnLongpress = new UnityEvent(); private bool isPointDown = false;
private float invokeTime; // Use this for initialization
void Start()
{
} // Update is called once per frame
void Update()
{
if (isPointDown)
{
if (Time.time - invokeTime > interval)
{
//触发点击;
m_OnLongpress.Invoke();
invokeTime = Time.time;
}
}
} public void OnPointerDown(PointerEventData eventData)
{
m_OnLongpress.Invoke(); isPointDown = true; invokeTime = Time.time;
} public void OnPointerUp(PointerEventData eventData)
{
isPointDown = false;
} public void OnPointerExit(PointerEventData eventData)
{
isPointDown = false;
}
}

(三)单击/长按组合

如果需要用到此按钮既有点击又有长按则可用如下代码

using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

public class LongPressEvent : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
public float interval = 1f;

[SerializeField]
UnityEvent longPressEvent = new UnityEvent();

[SerializeField]
UnityEvent pressEvent = new UnityEvent();

private bool isPointDown = false;
private float invokeTime = 0;
private bool longPressInvoked = false;
private bool isPointUp = false;
private bool isDragging = true;

private float distance = 0;
private float precise = 5f;
private float time = 0.15f;
private Vector2 startPos;
private Vector2 endPos;

// Update is called once per frame
void Update()
{
if (isPointDown)
{
if (Time.time - invokeTime > interval)
{
//if (!isInvoked)
//{
// longPressEvent.Invoke();
// invokeTime = Time.time;
// isInvoked = true;
//}
longPressEvent.Invoke();
invokeTime = Time.time;
longPressInvoked = true;
}
}

if (isPointUp)
{
if (Vector2.Distance(startPos, endPos) <= precise && Time.time - invokeTime < time)//Vector2.Distance(startPos,endPos)<=precise&&
{
if (!longPressInvoked)
pressEvent.Invoke();
}

isPointUp = false;
longPressInvoked = false;
}
}

public void OnPointerDown(PointerEventData eventData)
{
startPos = eventData.position;
endPos = eventData.position;

isPointDown = true;
invokeTime = Time.time;
}

public void OnPointerUp(PointerEventData eventData)
{
endPos = eventData.position;
isPointUp = true;

isPointDown = false;
}

public void OnPointerExit(PointerEventData eventData)
{
endPos = eventData.position;
isPointDown = false;
longPressInvoked = false;
}
}

(四)滑动屏幕,旋转模型

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems; public class ModelRotation : MonoBehaviour, IDragHandler, IBeginDragHandler
{
public Transform axisRotation;//旋转对象
public float speedRatate = 0.1f; float posStart = ;
float posDragging = ; public void OnBeginDrag(PointerEventData eventData)
{
posStart = eventData.position.x;
posDragging = eventData.position.x;
//Debug.Log("begin Drag:"+eventData.position);
} public void OnDrag(PointerEventData eventData)
{
posDragging = eventData.position.x;
float scale = ; if (posDragging - posStart > )
{
scale = ;
}
else if(posDragging - posStart < )
{
scale = -;
} axisRotation.Rotate(Vector3.up * speedRatate * scale);
posStart = posDragging;
//Debug.Log("on Drag:" + eventData.position);
}
}

unityevent与持续按键触发的更多相关文章

  1. stm32F042 (二) 按键触发中断

    已经实现GPIO口输出高低电平控制LED,这里实现按键触发中断来改变LED闪亮的频率,因为PB3连着LED,所以PB3的输出模式没有改变,随意选一个GPIO口PA7接按键产生中断.因为nucleo开发 ...

  2. Python窗口学习之浅尝按键触发事件

    一.窗口上敲键盘触发事件(以Enter键为例) 二.点击窗口按钮触发事件(以鼠标左键双击为例) 代码: import tkinter as tk root = tk.Tk() root.geometr ...

  3. 纯C语言写的按键驱动,将按键逻辑与按键处理事件分离~

    button drive 杰杰自己写的一个按键驱动,支持单双击.连按.长按:采用回调处理按键事件(自定义消抖时间),使用只需3步,创建按键,按键事件与回调处理函数链接映射,周期检查按键. 源码地址:h ...

  4. 入门级的按键驱动——按键驱动笔记之poll机制-异步通知-同步互斥阻塞-定时器防抖

    文章对应视频的第12课,第5.6.7.8节. 在这之前还有查询方式的驱动编写,中断方式的驱动编写,这篇文章中暂时没有这些类容.但这篇文章是以这些为基础写的,前面的内容有空补上. 按键驱动——按下按键, ...

  5. JS触发事件大全

          事件 浏览器支持 解说 一般事件 onclick IE3.N2 鼠标点击时触发此事件 ondblclick IE4.N4 鼠标双击时触发此事件 onmousedown IE4.N4 按下鼠 ...

  6. emWin(ucGui) MULTIEDIT控件的按键响应处理 worldsing

    目前没有读过ucgui的源代码,通过应用代码测试出在FRAMEWIN的控件焦点顺序是样的: 按资源列表里创建的控件,默认将焦点落在第一个可接收焦点的控件,目前知道不可接收 焦点的控件有TEXT,在FR ...

  7. 游戏Demo(持续更新中...)

    格斗游戏 主要用于联系Unity的动画系统,并加入了通过检测按键触发不同的技能. WASD控制方向,AD为技能1,SW为技能2,右键跳跃,连续单机普通连招. 本来是要用遮罩实现跑动过程中的攻击动作,但 ...

  8. JS基础知识:Javascript事件触发列表

    Javascript是一种由Netscape的LiveScript发展而来的原型化继承的基于对象的动态类型的区分大小写的客户端脚本语言,主要目的是为了解决服务器端语言. JavaScript使我们有能 ...

  9. 用LED灯和按键来模拟工业自动化设备的运动控制

    开场白: 前面三节讲了独立按键控制跑马灯的各种状态,这一节我们要做一个机械手控制程序,这个机械手可以左右移动,最左边有一个开关感应器,最右边也有一个开关感应器.它也可以上下移动,最下面有一个开关感应器 ...

随机推荐

  1. [phyton]文件的简单读写练习

    f.open() 用于打开一个文件. f=open("record.txt","w",encoding="utf-8")#打开文件,设置文件 ...

  2. Linux 文件复制命令cp

    文件复制命令cp 命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination) cp [option] source1 source2 source3 ... ...

  3. ELK系列(一):安装(elasticsearch + logstash + kibana)

    因为公司使用ELK的缘故,这两天尝试在阿里云上安装了下ELK,这里做个笔记,有兴趣的同学可以看下. 先大致介绍下ELK,ELK是三个组件的缩写,分别是elasticsearch.logstash.ki ...

  4. pyinstaller 打包exe程序读不到配置文件No such file

    挺久没更新博客的,一来之前是觉得才疏学浅,记录下来的太简单没人看.二来时间上不是很充裕(不是借口,有时间打游戏,没时间总结) 偶然有一次发现同事在搜索解决问题的时候正在看我博客的解决思路,很奇妙的感觉 ...

  5. css 精灵图的使用

    精灵图的使用 1.给一个容器定义一个大小(宽高) 2.引入背景图 3.定位到自己你想要的图片位置 例如:  background-position: 0 0;  background-position ...

  6. Javascript设计模式——建造者模式

    建造者模式是相对比较简单的一种设计模式,属于创建型模式的一种: 定义:将一个复杂的对象分解成多个简单的对象来进行构建,将复杂的构建层与表现层分离,使相同的构建过程可以创建不同的表示模式:    优点: ...

  7. Aria2 1.35.0,更新,测试,发布

    在上一篇: 有哪些便宜还好用的东西,买了就感觉得了宝一样? 结尾提到了Tatsuhiro Tsujikawa的aria2计划在10月更新一个新的版本 今天趁着雨后明月挂天,开始了简单的更新 虽然在半年 ...

  8. CSDN VIP如何添加自定义栏目

    几个月前我也开始在csdn上开了博客,一来给自己加几个少的可怜的流量,再者,让公众号的原创文章获得更多的曝光,让有需要的同学看到. 写过csdn博客的同学都知道,默认只有打赏c币功能:也没有专门广告位 ...

  9. mpvue 签字组件

    <template> <div > <canvas class='firstCanvas' canvas-id="firstCanvas" @touc ...

  10. Python调用GithubAPI并进行初步的数据分析

    找到一个Github 上的公开api url = 'https://api.github.com/search/repositories?q=language:python&sort=star ...