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

用过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物理碰撞区域。不然的话点击没有效果。

转载于:https://www.cnblogs.com/superdo/p/5230976.html

Fingers is your one stop choice for all gesture handling in Unity (and other C# platforms as Fingers has no Unity specific code). Fingers has been tested and runs great on MAC, Windows, iOS and Android. Full C# source code and shader code is provided! No other asset matches Fingers Gestures on price and feature set. Fingers - Touch Gesture for Unity has been featured in multiple Unity promotions and tutorial videos, showing the high quality and full feature set and value proposition. With Fingers, you get all the common touch gestures you are used to built in. This includes: tap, double tap, long tap, pan, rotate, scale and swipe gestures. In addition, creating your own custom gestures is simple and easy. I've recently began adding on-screen controls. Right now a joystick and dpad are included with more controls planned for the future. Adding gestures in the inspector has never been easier. Simply add from the component menu and configure your gesture. Set the callback to your script and you are done! Pan/rotate/scale and drag and drop is simple. Built in scripts for these scenarios, with demo scenes are provided. I've also added image recognition gestures. With a few minutes of tweaking, you can recognize shapes like circles, lightning bolts, check marks and the letter X. Previewing gestures in the player is easy, even without a touch screen. Just use the shift and control key plus mouse wheel to simulate pinch and rotate gestures, along with each button to simulate a different finger. Fingers has a similar architecture to UIGestureRecognizer from iOS, but is built using all C# code and will easily run on any platform that has a mouse or touch screen. Gestures are restricted to most UI elements by default, but this is easily configured if you want the gestures to pass through additional UI elements. This asset works great with the default Unity UI and Event System. Gestures can be executed simultaneously or exclusively (the default). In addition
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值