用过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. 一只简单的网络爬虫(基于linux C/C++)————利用正则表达式解析页面

    我们向一个HTTP的服务器发送HTTP的请求后,服务器会返回可能一个HTML页面(当然也可以是其他的资源),我们可以利用返回的HTML页面,在其中寻找其他的Url,例如我们可以这样在浏览器上查看一下H ...

  2. async与await----js的异步处理

    async与await----js的异步处理 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 说明 之前写代码遇到一个问题,返回 ...

  3. python 矢量数据转栅格数据

    from osgeo import gdal,osr,ogr#定义投影sr = osr.SpatialReference('LOCAL_CS["arbitrary"]')#在内存中 ...

  4. Coursera课程笔记----P4E.Capstone----Week 4&5

    Spidering and Modeling Email Data(week4&5) Mailing List - Gmane Crawl the archive of a mailing l ...

  5. sql:exists 与 not exists

    $sql = "select a.Vchcode,a.vdate,a.btypeid,a.vcomment,a.total,a.vnumber,b.bfullname,b.artotal,b ...

  6. dedecms_v5.7的apache文件名解析漏洞的学习

    0x00 Apache文件名解析漏洞 Apache是一个Web服务器,可以提供web服务.配合java中间件.PHP实现动态页面访问. Apache和PHP通过接口接入后,Apache接受用户的请求, ...

  7. redis主从复制、主从延迟知几何

    本片章节主要从 redis 主从复制延迟相关知识及影响因素做简要论述. 1.配置:repl-disable-tcp-nodelay 也即是TCP 的 TCP_NODELAY 属性,决定数据的发送时机. ...

  8. ES6-10笔记(一)

    大纲 (慕课的图先用着) scope-作用域 作用域是指程序源代码中定义变量的区域,规定了如何查找变量,也就是确定当前执行代码对变量的访问权限. JavaScript作用域共有两种主要的工作模型--词 ...

  9. 3.3 Go浮点型

    1.Go浮点型 Go 语言提供了两种精度的浮点数,float32 和 float64,编译器默认声明为float64 小数类型就是存放小数的,如1.2 0.005 -2.32 package main ...

  10. MyCat水平分库

    一.什么是水平分库 将一张表水平切分到多个库中 1.1分片原则 1.需要分片的表是少数的 2.能不切分尽量不要切分 3.日志表可以采取归档方式 4.选择合适的切分规则和分片建,确保数据分片均匀,否则依 ...