using UnityEngine;
using System.Collections;
using System;
/// <summary>
/// 兩隻手指的操控:相同方向的移動是旋轉,相反方向的移動是縮放
/// </summary>
public class DetectTouchMovement : MonoBehaviour
{
const float pinchTurnRatio = Mathf.PI / 2;
const float minTurnAngle = 10f;
const float pinchRatio = 1;
const float minPinchDistance = 1f;
const float panRatio = 1;
const float minPanDistance = 0;
Quaternion desiredRotation;
/// <summary>
/// The delta of the angle between two touch points
/// </summary>
static public float turnAngleDelta;
/// <summary>
/// The angle between two touch points
/// </summary>
static public float turnAngle;
/// <summary>
/// The delta of the distance between two touch points that were distancing from each other
/// 兩個觸摸點之間進行了相互保持距離增量
/// </summary>
static public float pinchDistanceDelta;
/// <summary>
/// 兩隻手指距離
/// </summary>
static public float pinchDistance;
public static bool mFingerUP=true;
bool mRotate=false;
bool mCanDistance=false;
string mScale="30";
string mRotationvalue="0.05";
public Action<float> mScaleEven;
static Touch mOldTouch1=new Touch();
/// <summary>
/// Calculates Pinch and Turn - This should be used inside LateUpdate
/// </summary>
static public void Calculate ()
{
pinchDistance = pinchDistanceDelta = 0;
turnAngle = turnAngleDelta = 0;
// if two fingers are touching the screen at the same time ...
if (Input.touchCount == 2)
{
Touch touch1 = Input.touches[0];
Touch touch2 = Input.touches[1];
mFingerUP=false;
// ... if at least one of them moved ...
if (touch1.phase == TouchPhase.Moved || touch2.phase == TouchPhase.Moved)
{
// ... check the delta distance between them ...
pinchDistance = Vector2.Distance(touch1.position, touch2.position);
float prevDistance = Vector2.Distance(touch1.position - touch1.deltaPosition,
touch2.position - touch2.deltaPosition);
pinchDistanceDelta = pinchDistance - prevDistance;
//dot>0=旋轉,dot <=0 縮放
float aDot = Vector2.Dot( touch1.deltaPosition.normalized, touch2.deltaPosition.normalized );
//Debug.Log("70 aDot="+aDot);
if(aDot>0.8f)
{//旋轉
turnAngle = Angle(touch1.position, touch2.position);
float prevTurn = Angle(touch1.position - touch1.deltaPosition,
touch2.position - touch2.deltaPosition);
//turnAngleDelta = Mathf.DeltaAngle(prevTurn, turnAngle);
turnAngleDelta=prevTurn;
if(mOldTouch1.position.x > touch1.position.x)
turnAngleDelta*=-1f;
// ... if it's greater than a minimum threshold, it's a turn!
if (Mathf.Abs(turnAngleDelta) > minTurnAngle)
{
turnAngleDelta *= pinchTurnRatio;
}
else
{
turnAngle = turnAngleDelta = 0;
}
mOldTouch1=touch1;
pinchDistance = pinchDistanceDelta = 0;
}
else
{//縮放
turnAngle = turnAngleDelta = 0;
Debug.Log("95 pinchDistanceDelta = "+pinchDistanceDelta);
// ... if it's greater than a minimum threshold, it's a pinch!
if (Mathf.Abs(pinchDistanceDelta) > minPinchDistance)
{
Debug.Log("pinchDistanceDelta = "+pinchDistanceDelta);
pinchDistanceDelta *= pinchRatio;
}
else
{
pinchDistance = pinchDistanceDelta = 0;
}
}
}
}
else
{
mFingerUP=true;
}
}
static private float Angle (Vector2 pos1, Vector2 pos2) {
Vector2 from = pos2 - pos1;
Vector2 to = new Vector2(1, 0);
float result = Vector2.Angle( from, to );
Vector3 cross = Vector3.Cross( from, to );
if (cross.z > 0) {
result = 360f - result;
}
return result;
}
void Update ()
{
Calculate();
if (Mathf.Abs(pinchDistanceDelta) > 0)
{ // zoom
mRotate=false;
if(mCanDistance)
{
float aValue=this.transform.parent.localScale.x+pinchDistanceDelta/float.Parse(mScale);
Debug.Log("aValue="+aValue);
this.transform.parent.localScale=new Vector3(aValue,1f,1f);
}
else
mCanDistance=true;
}
else if (Mathf.Abs(turnAngleDelta) > 0)
{ // rotate
Vector3 rotationDeg = Vector3.zero;
rotationDeg.y = -turnAngleDelta*float.Parse(mRotationvalue);
desiredRotation *= Quaternion.Euler(rotationDeg);
mCanDistance=false;
transform.rotation=desiredRotation;
}
if(mFingerUP)
{
mCanDistance=false;
mRotate=false;
}
if(this.transform.parent.localScale.x<0.5f)
this.transform.parent.localScale=new Vector3(0.5f,1f,1f);
if(mScaleEven!=null)
mScaleEven(this.transform.parent.localScale.x);
}
}
- Mar 25 Fri 2016 17:40
兩隻手指的操控:相同方向的移動是旋轉,相反方向的移動是縮放
close
全站熱搜
留言列表
發表留言