EasyTouch与UGUI的结合

1、描述

单指转动、双指移动、双指放缩。
注意:在要一个RectTransform上添加ColliderBox组件,具体及其它设置请参考我的demo工程。

上图:
这里写图片描述

2、上控制代码

//[lzh]
using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TestUGUI : MonoBehaviour
{
    public Transform target;
    public Camera m_camera;
    public Button btn_reset;

    // 初始状态
    private Vector3 initPosition = Vector3.zero;
    private Quaternion initRotation = Quaternion.identity;
    private Vector3 initLocalScale = Vector3.one;

    // 手势的一些状态数据
    private Vector3 deltaPosition_for_move = Vector3.zero;
    private Vector2 deltaPosition_for_rotate = Vector2.zero;
    private Quaternion lastRotation_for_rotate = Quaternion.identity;

    // 用于控制单与双指的冲突
    bool canSingleFinger = true;
    bool startSingFinger = false;

    void Start()
    {
        if (target)
        {
            initPosition = target.position;
            initRotation = target.rotation;
            initLocalScale = target.localScale;
        }

        btn_reset.onClick.AddListener(() =>
        {
            target.position = initPosition;
            target.rotation = initRotation;
            target.localScale = initLocalScale;

            deltaPosition_for_move = initPosition;
            deltaPosition_for_rotate = initPosition;
            lastRotation_for_rotate = initRotation;
        });
    }

    void OnEnable()
    {
        EasyTouch.On_TouchStart2Fingers += On_TouchStart2Fingers;
        EasyTouch.On_Cancel2Fingers += On_Cancel2Fingers;

        // 单指转动
        EasyTouch.On_TouchStart += On_TouchStart;
        EasyTouch.On_Drag += EasyTouch_On_Drag;
        EasyTouch.On_DragEnd += EasyTouch_On_DragEnd;

        // 双指移动
        EasyTouch.On_DragStart2Fingers += On_DragStart2Fingers;
        EasyTouch.On_Drag2Fingers += On_Drag2Fingers;
        EasyTouch.On_DragEnd2Fingers += On_DragEnd2Fingers;

        // 双指放缩
        EasyTouch.On_PinchIn += On_PinchIn;
        EasyTouch.On_PinchOut += On_PinchOut;
        EasyTouch.On_PinchEnd += On_PinchEnd;
    }

    void OnDisable()
    {
        EasyTouch.On_TouchStart2Fingers -= On_TouchStart2Fingers;
        EasyTouch.On_Cancel2Fingers -= On_Cancel2Fingers;

        EasyTouch.On_TouchStart -= On_TouchStart;
        EasyTouch.On_Drag -= EasyTouch_On_Drag;
        EasyTouch.On_DragEnd -= EasyTouch_On_DragEnd;

        EasyTouch.On_DragStart2Fingers -= On_DragStart2Fingers;
        EasyTouch.On_Drag2Fingers -= On_Drag2Fingers;
        EasyTouch.On_DragEnd2Fingers -= On_DragEnd2Fingers;

        EasyTouch.On_PinchIn -= On_PinchIn;
        EasyTouch.On_PinchOut -= On_PinchOut;
        EasyTouch.On_PinchEnd -= On_PinchEnd;
    }

    private void On_TouchStart2Fingers(Gesture gesture)
    {
        canSingleFinger = false;
        startSingFinger = false;
    }

    private void On_Cancel2Fingers(Gesture gesture)
    {
        canSingleFinger = true;
    }


    //================================================

    public void On_TouchStart(Gesture gesture)
    {
        Debug.Log("gesture.touchCount = " + gesture.touchCount);
        Debug.Log("gesture.fingerIndex = " + gesture.fingerIndex);
        if (gesture.touchCount > 1) return;
        if (!canSingleFinger) return;

        Debug.Log("Touch in " + gesture.position);
        startSingFinger = true;

        deltaPosition_for_rotate = gesture.position;
        lastRotation_for_rotate = target.rotation;

    }

    private void EasyTouch_On_Drag(Gesture gesture)
    {
        if (gesture.touchCount > 1) return;
        if (!canSingleFinger || !startSingFinger)
        {
            if (canSingleFinger == true && startSingFinger == false)
            {
                On_TouchStart(gesture);
            }
            return;
        }
        Debug.Log("EasyTouch_On_Drag " + gesture.position);

        Vector2 offset = gesture.position - deltaPosition_for_rotate;
        offset = new Vector2(offset.x * 0.3f, offset.y * 0.3f);
        if (Mathf.Abs(offset.x) > Mathf.Abs(offset.y))
        {
            offset.y = 0f;
        }
        else
        {
            offset.x = 0f;
        }

        target.rotation = lastRotation_for_rotate;
        target.Rotate(new Vector3(offset.y, -offset.x,0f), Space.World);

        deltaPosition_for_rotate = gesture.position;
        lastRotation_for_rotate = target.rotation;
    }

    private void EasyTouch_On_DragEnd(Gesture gesture)
    {
        if (gesture.touchCount > 1) return;
        if (!canSingleFinger || !startSingFinger) return;
        startSingFinger = false;
    }

    //================================================

    //public void On_TouchStart(Gesture gesture)
    //{
    //    Debug.Log("Touch in " + gesture.position);       

    //    Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
    //    deltaPosition_for_move = target.position - position;

    //}

    //private void EasyTouch_On_Drag(Gesture gesture)
    //{
    //    Debug.Log("EasyTouch_On_Drag " + gesture.position);
    //    Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
    //    target.position = position + deltaPosition_for_move;
    //}

    void On_DragStart2Fingers(Gesture gesture)
    {
        Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
        deltaPosition_for_move = target.position - position;
    }

    void On_Drag2Fingers(Gesture gesture)
    {
        Vector3 position = m_camera.ScreenToWorldPoint(new Vector3(gesture.position.x, gesture.position.y, target.position.z));
        target.position = position + deltaPosition_for_move;
    }

    void On_DragEnd2Fingers(Gesture gesture)
    {
        deltaPosition_for_move = Vector3.zero;
    }

    //================================================
    private void On_PinchIn(Gesture gesture)
    {
        float zoom = Time.deltaTime * gesture.deltaPinch * 0.3f;

        Vector3 scale = target.localScale;
        if (scale.x - zoom < 0.3)
        {
            target.localScale = Vector3.one * 0.3f;
        }
        else
        {
            target.localScale = new Vector3(scale.x - zoom, scale.y - zoom, scale.z - zoom);
        }
    }

    private void On_PinchOut(Gesture gesture)
    {
        float zoom = Time.deltaTime * gesture.deltaPinch * 0.3f;
        Vector3 scale = target.localScale;
        if (scale.x + zoom > 2)
        {
            target.localScale = Vector3.one * 2f;
        }
        {
            target.localScale = new Vector3(scale.x + zoom, scale.y + zoom, scale.z + zoom);
        }
    }

    private void On_PinchEnd(Gesture gesture)
    {

    }
}

3、上demo工程

单指转动、双指移动、双指放缩。
注意:在要一个RectTransform上添加ColliderBox组件,具体及其它设置请参考我的demo工程。
下载

4、补充 – 2015.12.04

  • 我这使用easytouch的版本好像是4.1的。
  • 代码的中的On_Cancel2Fingers 事件应该换成On_TouchUp2Fingers事件。
    在实际的测试中发现有时候,双指还在屏幕上,也触发了On_Cancel2Fingers 事件,这不是我们要的。我们要的是一个只有双指离开或变成单指或多指时,才触发且会触发的事件。后来发现On_TouchUp2Fingers事件符合这个条件!
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿海-程序员

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值