参考网址 http://dp0304.com/unity3d/2013/07/28/fingergestures/
FingerGestures 的具体用法 可以参照:
(1)http://blog.youkuaiyun.com/luyuncsd123/article/details/14123977
(2)http://www.xuanyusong.com/archives/1869
2.
Camera的移动边界处理文件
CameraController.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Sensor;
using Common;
namespace Battle
{
public class CameraController : MonoBehaviour {
// zoom speed
int dragSpeed = BattleConstants.ZOOM_SPEED_OTHER;
//Drag with, height
float xMinSize = BattleConstants.X_MINUS_BOUNDARY;
float zMinSize = BattleConstants.Z_MINUS_BOUNDARY;
//MenuBarHeight
float zBoundary = BattleConstants.MISSION_BATTLE_UI_BOUNDARY;
//NGUI layer no
private const int NGUI_Layer = 8;
private Vector3 BeginPosition = Vector3.zero;
private Vector3 EndPosition = Vector3.zero;
private Vector3 tempVelocity = Vector2.zero; //Velocity
private float m_smoothTime; //Smooth Time
private bool m_PinchFlag = false;
private bool m_SwipeFlag = false;
private bool m_DragFlag = true;
private string m_BGSelectDell = "BGSelectDell";
private Dictionary < float ,int > dirctionary = new Dictionary <</span>float,int> ();
void Start()
{
m_PinchFlag = false;
m_SwipeFlag = false;
m_DragFlag = true;
EndPosition = transform.position;
dirctionary.Add (5000.0f, 1000);
dirctionary.Add (4000.0f, 950);
dirctionary.Add (3000.0f, 900);
dirctionary.Add (2000.0f, 850);
dirctionary.Add (1500.0f, 800);
dirctionary.Add (1000.0f, 750);
dirctionary.Add (900.0f, 700);
dirctionary.Add (800.0f, 650);
dirctionary.Add (700.0f, 600);
dirctionary.Add (600.0f, 550);
dirctionary.Add (500.0f, 500);
dirctionary.Add (400.0f, 450);
dirctionary.Add (300.0f, 400);
dirctionary.Add (200.0f, 350);
dirctionary.Add (100.0f, 300);
dirctionary.Add (0.0f, 250);
}
// onClick
void OnTap( TapGesture gesture )
{
EndPosition = transform.position;
m_PinchFlag = false;
m_SwipeFlag = false;
m_DragFlag = true;
}
/// <</span>summary>
/// Raises the drag event.
/// FingerGuestureドラッグ
/// </</span>summary>
/// <</span>param name="gesture">Gesture.</</span>param>
void OnDrag(DragGesture gesture)
{
EndPosition = transform.position;
m_SwipeFlag = false;
if (gesture.StartSelection == null || gesture.StartSelection.layer == NGUI_Layer || gesture.StartSelection.name == m_BGSelectDell)
{
return;
}
if(m_PinchFlag == true)
{
return;
}
Vector3 screenPos2D = gesture.DeltaMove;
screenPos2D.z = 0f;
//change to screen position
Vector3 cameraPos2D = Camera.main.WorldToScreenPoint(Camera.main.transform.position);
cameraPos2D = cameraPos2D - screenPos2D;
//move camera position
Camera.main.transform.position = CalculateOffset ( Camera.main.ScreenToWorldPoint(cameraPos2D));
}
/// <</span>summary>
/// Changes the ratio.
/// FingerGesture Pinch
/// </</span>summary>
/// <</span>param name="gesture">Gesture.</</span>param>
void OnPinch( PinchGesture gesture )
{
if(gesture.Phase == ContinuousGesturePhase.Started)
{
BeginPosition = this.transform.position;
m_PinchFlag = true;
}
if(gesture.Phase == ContinuousGesturePhase.Updated)
{
if (this.transform.camera.orthographicSize > BattleConstants.ORTHOGRAPHICSIZECBELOW && gesture.Delta > 0)
{
if (this.transform.camera.orthographicSize - BattleConstants.ORTHOGRAPHICSIZECBELOW < gesture.Delta * BattleConstants.PINCHSCALEFACTOR)
{
this.transform.camera.orthographicSize = BattleConstants.ORTHOGRAPHICSIZECBELOW;
}
else
{
this.transform.camera.orthographicSize -= gesture.Delta * BattleConstants.PINCHSCALEFACTOR;
}
}
else if (transform.camera.orthographicSize < BattleConstants.ORTHOGRAPHICSIZECABOVE && gesture.Delta < 0)
{
if (BattleConstants.ORTHOGRAPHICSIZECABOVE - this.transform.camera.orthographicSize < Mathf.Abs(gesture.Delta) * BattleConstants.PINCHSCALEFACTOR)
{
this.transform.camera.orthographicSize = BattleConstants.ORTHOGRAPHICSIZECABOVE;
}
else
{
this.transform.camera.orthographicSize -= gesture.Delta * BattleConstants.PINCHSCALEFACTOR;
}
}
//TODO add by kin
xMinSize = BattleConstants.X_MINUS_BOUNDARY + (this.transform.camera.orthographicSize - BattleConstants.ORTHOGRAPHICSIZEDEFAULT) / 2;
zMinSize = BattleConstants.Z_MINUS_BOUNDARY + (this.transform.camera.orthographicSize - BattleConstants.ORTHOGRAPHICSIZEDEFAULT);
zBoundary = BattleConstants.MISSION_BATTLE_UI_BOUNDARY * this.transform.camera.orthographicSize / BattleConstants.ORTHOGRAPHICSIZEDEFAULT;
Camera.main.transform.position = BeginPosition; //CalculateOffset (Camera.main.transform.position);
}
if(gesture.Phase == ContinuousGesturePhase.Ended)
{
Invoke("setPinchFlagFalse", 0.15f);
}
}
void setPinchFlagFalse()
{
this.m_PinchFlag = false;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <</span>summary>
/// Raises the swipe event.
/// </</span>summary>
/// <</span>param name="gesture">Gesture.</</span>param>
void OnSwipe( SwipeGesture gesture )
{
if (gesture.StartSelection == null || gesture.StartSelection.layer == NGUI_Layer || gesture.StartSelection.name == m_BGSelectDell)
{
return;
}
if(m_PinchFlag == true)
{
return;
}
float velocity = gesture.Velocity;
Vector3 screenPos2D = velocity >= 4000 ? gesture.Move : gesture.Move * 1.2f;
screenPos2D.z = 0f;
//change to screen position
Vector3 cameraPos2D = Camera.main.WorldToScreenPoint(Camera.main.transform.position);
cameraPos2D = cameraPos2D - screenPos2D;
//move camera position
EndPosition = CalculateOffset ( Camera.main.ScreenToWorldPoint(cameraPos2D));
float distance = Vector3.Distance (transform.position, EndPosition);
foreach (KeyValuePair<</span>float, int> kvp in dirctionary )
{
if(velocity > kvp.Key)
{
this.m_smoothTime = distance / kvp.Value;
}
}
m_SwipeFlag = true;
}
void Update ()
{
if(m_SwipeFlag == true)
{
transform.position = new Vector3(Mathf.SmoothDamp(transform.position.x, EndPosition.x, ref tempVelocity.x, this.m_smoothTime),
transform.position.y,
Mathf.SmoothDamp(transform.position.z, EndPosition.z, ref tempVelocity.z, this.m_smoothTime));
if(Vector3.Distance( transform.position, EndPosition) <= 1.0f)
{
transform.position = EndPosition;
m_SwipeFlag = false;
}
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// <</span>summary>
/// Calculates the offset.
/// カメラドラッグ範囲制御
/// </</span>summary>
/// <</span>returns>The offset.</</span>returns>
/// <</span>param name="tmpPosition">Tmp position.</</span>param>
Vector3 CalculateOffset (Vector3 tmpPosition)
{
if (tmpPosition.x < xMinSize)
{
tmpPosition.x = xMinSize;
}
if (tmpPosition.x > xMinSize * -1 )
{
tmpPosition.x = xMinSize * -1;
}
if (tmpPosition.z < zMinSize + zBoundary)
{
tmpPosition.z = zMinSize + zBoundary;
}
if (tmpPosition.z > zMinSize * -1 )
{
tmpPosition.z = zMinSize * -1;
}
return tmpPosition;
}
}
}
文件中用的
宏定义 文件
BattleConstants.cs
#region About Battle Camera --------------------------------------
public const float ORTHOGRAPHICSIZECABOVE = 140.0f;
public const float ORTHOGRAPHICSIZECBELOW = 60.0f;
public const float ORTHOGRAPHICSIZEDEFAULT = 100.0f;
public const float PINCHSCALEFACTOR = 0.2f;
public const int ZOOM_SPEED_IPHONE = 15;
public const int ZOOM_SPEED_ANDROID = 15;
public const int ZOOM_SPEED_OTHER = 5;
//DragCameraController.cs
public const int DRAG_SPEED_IPHONE = 100;
public const int DRAG_SPEED_ANDROID = 100;
public const int DRAG_SPEED_OTHER = 5;
//mod by kin
public const float X_MINUS_BOUNDARY = -140f;
public const float X_PLUS_BOUNDARY = 140f;
public const float Z_MINUS_BOUNDARY = -100f;
public const float Z_PLUS_BOUNDARY = 100f;
public const float MISSION_BATTLE_UI_BOUNDARY = -30f;
#endregion
3. 解释
3.
CalculateOffset 次函数是移动后是否超过 屏幕的边界 超过了就从新计算 终点坐标