您的位置:首页 > 移动开发 > Unity3D

[Unity2d系列教程] 005.Unity如何使用外部触控插件FingerGuesture

2016-03-01 14:45 701 查看
用过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 = -1;
void OnDrag(DragGesture gesture)
{
// 获取起始点
FingerGestures.Finger finger = gesture.Fingers[0];

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 = -1;
}
}
}

// 长按
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物理碰撞区域。不然的话点击没有效果。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: