您的位置:首页 > 产品设计 > UI/UE

EasyTouch与UGUI的结合使用

2016-03-10 14:06 731 查看
http://blog.csdn.net/AnYuanLzh/article/details/50145151#
目录(?)[-]

描述
上控制代码
上demo工程
补充 20151204

1、描述

单指转动、双指移动、双指放缩。

注意:在要一个RectTransform上添加ColliderBox组件,具体及其它设置请参考我的demo工程。

上图:



2、上控制代码

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事件符合这个条件!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: