上一篇中提到一种鼠标按下时的事件触发,即采用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. Javascript的基础

    ECMAScript(语法.标准) BOM(浏览器) DOM(网页) ECMAScript是一个标准,它规定了语法.类型.语句.关键字.保留子.操作符.对象.(相当于法律) BOM(浏览器对象模型): ...

  2. 基于操作系统的Linux网络参数的配置

    一.实验目的 1.掌握Linux下网络参数的查看方法并理解网络参数的含义. 2.掌握Linux下网络参数的配置 二.实验内容 1.查看当前网络配置的参数. 2.在Linux主机中将网络参数按以下要求设 ...

  3. Spring 梳理 - ContentNegotiatingViewResolver

    ContentNegotiatingViewResolver,这个视图解析器允许你用同样的内容数据来呈现不同的view.它支持如下面描述的三种方式: 1)使用扩展名http://localhost:8 ...

  4. FastDfs之TrackerServer的详细配置介绍

    # is this config file disabled # false for enabled # true for disabled disabled=false #当前配置是否不可用fals ...

  5. 使用.NET Core创建Windows服务(二) - 使用Topshelf方式

    原文:Creating Windows Services In .NET Core – Part 2 – The "Topshelf" Way 作者:Dotnet Core Tut ...

  6. 使用sp_getAppLock引发的一个小问题

    这几天线上频繁报如下的错误:“无法释放应用程序锁(数据库主体: 'public',资源: 'aa'),原因是当前没有保留该应用程序锁.” 下面是写法: declare @result int; BEG ...

  7. 整理一些大厂的开源平台及github,向他们看齐...

    有人苦恼,该如何突破技术的局限性... 有人羡慕,技术上你怎么懂得这么多... 有人哀叹,唉,我已经学不动了... 我的总结(纯属个人想法):身处IT,就得不断学习和积累,才不会被狠狠地甩在身后.什么 ...

  8. 关于MySQL退出命令,还有你不知道的一种操作

    前两天再进MySQL窗口的时候,手快点了一个 ' ,并且按下了enter键,于是就出现了这种情况, 然后就退不出来了,为此我还特意上网查了一下,最后的结果基本上都是只能关闭MySQL 重新进入. 因为 ...

  9. Vue核心知识——computed和watch的细节全面分析

    computed和watch的区别 computed特性 1.是计算值,2.应用:就是简化tempalte里面{{}}计算和处理props或$emit的传值,computed(数据联动).3.具有缓存 ...

  10. 算法学习之剑指offer(十一)

    一 题目描述 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推. import java.util.*; ...