用过Unity的都知道自带的Input.touches并不支持鼠标输入,给我们的调试带来很大的不方便。那么我们会发现其实有很多触控方面的插件,如inputtouches,easy touch,fingerGesture等。

下面我主要讲解FingerGesture的使用,这个插件不是免费的,可以自行购买

1.导入插件

导入后的插件会在Assets/Plugins下面

2.拖动Assets/Plugins/FingerGestures/Prefabs/FingerGestures到你的Hierarchy中,如下图

3.创建空节点GestureObj,然后绑定脚本TestGesture.cs

using UnityEngine;
using System.Collections; public class testGestures : MonoBehaviour
{ // Use this for initialization
void Start () { } // Update is called once per frame
void Update () { } // 单击
void OnTap(TapGesture gesture)
{
if (gesture.Selection == tapObject)
{
Debug.Log("Tab!!!!!!!!!!!");
}
} // 双击
void OnDoubleTap(TapGesture gesture)
{
if (gesture.Selection == doubleTapObject)
{
Debug.Log("Double Tab!!!!!!!!!!!");
}
} void OnSwipe(SwipeGesture gesture)
{
// make sure we started the swipe gesture on our swipe object
// we use the object the swipe started on, instead of the current one
GameObject selection = gesture.StartSelection; if (selection == swipeObject)
{
Debug.Log("Swipe!!!!!!!");
}
} int dragFingerIndex = -;
void OnDrag(DragGesture gesture)
{
// 获取起始点
FingerGestures.Finger finger = gesture.Fingers[]; if (gesture.Phase == ContinuousGesturePhase.Started)
{
// dismiss this event if we're not interacting with our drag object
if (gesture.Selection != dragObject)
return; // remember which finger is dragging dragObject
dragFingerIndex = finger.Index;
}
else if (finger.Index == dragFingerIndex) // gesture in progress, make sure that this event comes from the finger that is dragging our dragObject
{
if (gesture.Phase == ContinuousGesturePhase.Updated)
{
// update the position by converting the current screen position of the finger to a world position on the Z = 0 plane
dragObject.transform.position = GetWorldPos(gesture.Position);
}
else
{
// reset our drag finger index
dragFingerIndex = -;
}
}
} // 长按
void OnLongPress(LongPressGesture gesture)
{
if (gesture.Selection == longPressObject)
{
Debug.Log("Long press!!!!!!");
}
}
// 定义变量,用来操作
public GameObject longPressObject;
public GameObject tapObject;
public GameObject doubleTapObject;
public GameObject swipeObject;
public GameObject dragObject; // 公共方法
public static Vector3 GetWorldPos(Vector2 screenPos)
{
Ray ray = Camera.main.ScreenPointToRay(screenPos); // we solve for intersection with z = 0 plane
float t = -ray.origin.z / ray.direction.z; return ray.GetPoint(t);
}
}

4.添加指定的触控对象

5.添加Component

6.添加Screen Raycaster

7.要为对象添加2D物理碰撞区域。不然的话点击没有效果。

[Unity2d系列教程] 005.Unity如何使用外部触控插件FingerGuesture的更多相关文章

  1. [Unity2d系列教程] 003.Unity如何调用android的方法

    Unity开发的时候很多时候我们需要用到底层的一些功能,比如摄像,录音,震动等等,我们在Unity的层面是无法完成的.那么我们考虑到Unity是否可以直接调用到android方面的方法,替我们去完成我 ...

  2. [Unity2d系列教程] 006.Unity如何根据图片自动生成Animator

    Unity制作2D产品的时候,我们在制作动画的时候,要不断的生成Animation,Animator等等资源,如果动画一多的话,就变得麻烦.由于Unity是支持插件开发的,我们可以添加一个Editor ...

  3. [Unity2d系列教程] 004.Unity如何调用ios的方法(SDK集成相关)

    和上一篇类似,我们同样希望Unity能够直接调用IOS底层的代码,那么我们就需要研究怎么去实现它.下面让我来带大家看一个简单的例子 1.创建.h和.m文件如下 .h // // myTest.m // ...

  4. 黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(八) Unity Dependency Injection and Interception 依赖注入容器Uni ...

  5. [Unity2d系列教程] 002.引用外部DLL - C

    上一篇我们学习了Unity调用C#生成的外部DLL,但是有时候我们需要访问底层,不能不适用C生成的DLL.下面就让我们一起学习下,C如何生成. 1.创建一个C的控制台程序 2.点击确定->点击下 ...

  6. [Unity2d系列教程] 001.引用外部DLL - C#

    众所周知,Unity可以支持多种语言开发, C#, JS, Boo三种方式的开发, 能够很方便的集成一些外部插件,以便调用现有的动态链接库.学过C#的都知道C#可以生成一个dll供给其他的程序调用.那 ...

  7. Enterprise Library 5.0 系列教程

    1. Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (初级) 2. Microsoft Enterprise L ...

  8. Fastify 系列教程一(路由和日志)

    介绍 Fastify是一个高度专注于以最少开销和强大的插件架构,为开发人员提供最佳体验的Web框架. 它受到了 Hapi 和 Express 的启发,是目前最快的 Node 框架之一. Fastify ...

  9. Fastify 系列教程二 (中间件、钩子函数和装饰器)

    Fastify 系列教程: Fastify 系列教程一 (路由和日志) Fastify 系列教程二 (中间件.钩子函数和装饰器) 中间件 Fastify 提供了与 Express 和 Restify ...

随机推荐

  1. 无向图双连通分量BCC(全网最好理解)

    不是标题党,之前我也写过一篇比较全的,但是对于初学者不友好.传送门? 双连通分量(Biconnected component):     1.边双联通 E-BCC     2.点双连通 V-BCC 双 ...

  2. 在TX2上多线程读取视频帧进行caffe推理

    参考文章:Multi-threaded Camera Caffe Inferencing TX2之多线程读取视频及深度学习推理 背景 一般在TX2上部署深度学习模型时,都是读取摄像头视频或者传入视频文 ...

  3. 一只简单的网络爬虫(基于linux C/C++)————线程相关

    爬虫里面采用了多线程的方式处理多个任务,以便支持并发的处理,把主函数那边算一个线程的话,加上一个DNS解析的线程,以及我们可以设置的max_job_num值,最多使用了1+1+max_job_num个 ...

  4. 经过踩坑,搭建成功的Appium自动化测试环境

    因为最近本人准备搞app自动化,所以就搭建环境过程记录下来(主要踩过好几个坑) 期间有点烦躁,后面调整了下心态还是成功弄好了. 一.Appium环境搭建准备软件 所需要到的软件如下: 1.安装JDK1 ...

  5. Pycharm修改HTML模板

  6. 出现Please make sure you have the correct access rights and the repository exists.问题解决

    问题: 有一段时间没有用码云了,当输入 git push -u origin master命令出现Please make sure you have the correct access rights ...

  7. Azure AD(二)调用受Microsoft 标识平台保护的 ASP.NET Core Web API 下

    一,引言 上一节讲到如何在我们的项目中集成Azure AD 保护我们的API资源,以及在项目中集成Swagger,并且如何把Swagger作为一个客户端进行认证和授权去访问我们的WebApi资源的?本 ...

  8. 帝国cms 批量删除包含关键字的 内容

    删除包含关键字的 内容delete from www_kaifatu_com_ecms_news where playurl like '%关键字%'

  9. SpringBoot基础实战系列(三)springboot单文件与多文件上传

    springboot单文件上传 对于springboot文件上传需要了解一个类MultipartFile ,该类用于文件上传.我此次使用thymeleaf模板引擎,该模板引擎文件后缀 .html. 1 ...

  10. X-CTF(REVERSE高级) 666

    主函数输入的字符会和key比较长度和enflag比较内容,所以这道题的flag和输入有关 key长度为0x12,enflag的值为:izwhroz""w"v.K" ...