unity3d相机拖拽移动脚本,结合easyTouch,不提供easyTouch插件,此脚本需easyTouch5支持,下载easyTouch后把脚本放到工程即可,然后按下面的图示例就可以了
using UnityEngine;
using HedgehogTeam.EasyTouch;
/// <summary>
/// 相机拖拽
/// </summary>
public class CameraManager : MonoBehaviour
{
public static CameraManager Instance;
public float OriginDistance { get; private set; }
public Transform CameraRoot
{
get
{
if (CameraRoot_ == null)
CameraRoot_ = GameObject.Find("MainCamera").transform;
return CameraRoot_;
}
}
public GameObject WorldCameraObj
{
get
{
if (WorldCameraObj_ == null)
WorldCameraObj_ = GameObject.Find("WorldCamera").gameObject;
return WorldCameraObj_;
}
}
public Camera WorldCamera
{
get
{
if (WorldCamera_ == null)
{
WorldCamera_ = WorldCameraObj.GetComponent<Camera>();
}
return WorldCamera_;
}
}
private Transform CameraRoot_;
private GameObject WorldCameraObj_;
private Camera WorldCamera_;
// 缩放视口边界限制
private float MinDistance = 20f;
private float MaxDistance = 110;
// 边界限制
private RangeAttribute BoundX;
private RangeAttribute BoundZ;
public Vector2 BoundaryX;
public Vector2 BoundaryZ;
// 拖拽移动速度
public float MoveSpeed = 10f;
private Vector3 TargetPos;
private Vector3 EulerAngles;
public delegate void CameraOpreateHandler(Gesture gesture);
public delegate bool CameraDragHandler(Gesture gesture);
public delegate void CameraHandler();
public event CameraOpreateHandler On_SwipeStart;
public event CameraOpreateHandler On_Swipe;
public event CameraHandler On_SwipeCamera;
public event CameraOpreateHandler On_Pinch;
public event CameraOpreateHandler On_Twist;
public event CameraOpreateHandler On_SimpleTap;
public event CameraOpreateHandler On_TouchUp;
public event CameraOpreateHandler On_LongTapStart;
public event CameraOpreateHandler On_LongTap;
public event CameraOpreateHandler On_LongTapEnd;
public event CameraDragHandler CheckCameraCanDrag;
private void Awake()
{
Instance = this;
OriginDistance = GetCameraDistance();
TargetPos = CameraRoot.position;
EulerAngles = CameraRoot.eulerAngles;
EasyTouch.On_Pinch += EasyTouch_On_Pinch;
EasyTouch.On_PinchEnd += EasyTouch_On_PinchEnd;
EasyTouch.On_PinchIn += EasyTouch_On_PinchIn;
EasyTouch.On_PinchOut += EasyTouch_On_PinchOut;
EasyTouch.On_Twist += EasyTouch_On_Twist;
EasyTouch.On_TwistEnd += EasyTouch_On_TwistEnd;
EasyTouch.On_DragStart += EasyTouch_On_DragStart;
EasyTouch.On_SwipeStart += EasyTouch_On_SwipeStart;
EasyTouch.On_Drag += EasyTouch_On_Drag;
EasyTouch.On_Swipe += EasyTouch_On_Swipe;
EasyTouch.On_SimpleTap += EasyTouch_On_SimpleTap;
EasyTouch.On_TouchUp += EasyTouch_On_TouchUp;
EasyTouch.On_SimpleTap += OnSimpleTap;
InitWorldCamera();
}
private void OnDestroy()
{
EasyTouch.On_PinchEnd -= EasyTouch_On_PinchEnd;
EasyTouch.On_PinchIn -= EasyTouch_On_PinchIn;
EasyTouch.On_PinchOut -= EasyTouch_On_PinchOut;
EasyTouch.On_Twist -= EasyTouch_On_Twist;
EasyTouch.On_TwistEnd -= EasyTouch_On_TwistEnd;
EasyTouch.On_DragStart -= EasyTouch_On_DragStart;
EasyTouch.On_SwipeStart -= EasyTouch_On_SwipeStart;
EasyTouch.On_Drag -= EasyTouch_On_Drag;
EasyTouch.On_Swipe -= EasyTouch_On_Swipe;
EasyTouch.On_SimpleTap -= EasyTouch_On_SimpleTap;
EasyTouch.On_TouchUp -= EasyTouch_On_TouchUp;
EasyTouch.On_SimpleTap -= OnSimpleTap;
}
// 点击
private void OnSimpleTap(Gesture gesture)
{
if (gesture.pickedObject)
Debug.LogError(gesture.pickedObject.name);
}
private Camera CurCamera;
private void EasyTouch_On_PinchIn(Gesture gesture)
{
EasyTouch.SetEnableTwist(false);
if (CurCamera == null)
CurCamera = WorldCameraObj.GetComponent<Camera>();
if (CurCamera.orthographicSize >= 30)
return;
CurCamera.orthographicSize++;
float v = 5 * (CurCamera.orthographicSize / 10 - 1);
BoundX = new RangeAttribute(-20 + -v, 20 + v);
BoundZ = new RangeAttribute(-15 + -v, 15 + v);
}
private void EasyTouch_On_PinchOut(Gesture gesture)
{
EasyTouch.SetEnableTwist(false);
if (CurCamera == null)
CurCamera = WorldCameraObj.GetComponent<Camera>();
if (CurCamera.orthographicSize <= 10)
return;
CurCamera.orthographicSize--;
}
/// <summary>
/// 缩放
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_Pinch(Gesture gesture)
{
EasyTouch.SetEnableTwist(false);
// 直接改变视口来缩放
//Camera.main.fieldOfView += gesture.deltaPinch * Time.deltaTime;
// 考虑到视口变太大 画面扭曲的关系,所以改成根据朝向修改坐标来完成缩放
Vector3 pos = WorldCameraObj.transform.position + WorldCameraObj.transform.forward * (gesture.deltaPinch * Time.deltaTime * 10f / ((float)Screen.width / (float)Screen.height));
// 缩放距离限制
float distance = Vector3.Distance(CameraRoot_.position, pos);
if (distance < MinDistance || distance > MaxDistance)
return;
WorldCameraObj.GetComponent<Camera>().orthographicSize = distance;
WorldCameraObj.transform.position = pos;
WorldCameraObj.transform.position = new Vector3(WorldCameraObj.transform.position.x, pos.y, WorldCameraObj.transform.position.z);
if (On_Pinch != null)
On_Pinch(gesture);
}
private void EasyTouch_On_PinchEnd(Gesture gesture)
{
EasyTouch.SetEnableTwist(true);
}
/// <summary>
/// 旋转
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_Twist(Gesture gesture)
{
EasyTouch.SetEnablePinch(false);
CameraRoot.Rotate(Vector3.up * gesture.twistAngle);
if (On_Twist != null)
On_Twist(gesture);
}
void EasyTouch_On_TwistEnd(Gesture gesture)
{
//EasyTouch.SetEnablePinch( true );
}
/// <summary>
/// 开始拖
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_DragStart(Gesture gesture)
{
EasyTouch_On_SwipeStart(gesture);
}
/// <summary>
/// 开始划
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_SwipeStart(Gesture gesture)
{
if (On_SwipeStart != null)
On_SwipeStart(gesture);
}
/// <summary>
/// 拖
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_Drag(Gesture gesture)
{
// Debug.LogError( "moveing" );
EasyTouch_On_Swipe(gesture);
}
/// <summary>
/// 划
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_Swipe(Gesture gesture)
{
if (CheckCameraCanDrag != null)
{
if (!CheckCameraCanDrag(gesture))
{
// 如果不能拖相机,那么触发该回调,通知其它模块触发了拖动
if (On_Swipe != null)
On_Swipe(gesture);
return;
}
}
float rate = Vector3.Distance(WorldCameraObj.transform.position, CameraRoot.transform.position);
Vector3 pos = CameraRoot.transform.position;
pos -= (CameraRoot.transform.right * gesture.deltaPosition.x / (float)Screen.width * rate * MoveSpeed);
pos -= (CameraRoot.transform.forward * gesture.deltaPosition.y / (float)Screen.height * rate * MoveSpeed);
// 边界限制
if (pos.x <= (BoundX.min) || pos.x >= (BoundX.max))
{
return;
}
if (pos.z <= (BoundZ.min) || pos.z >= (BoundZ.max))
{
return;
}
TargetPos = pos;
}
/// <summary>
/// 点击
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_SimpleTap(Gesture gesture)
{
if (On_SimpleTap != null)
On_SimpleTap(gesture);
}
/// <summary>
/// 释放
/// </summary>
/// <param name="gesture"></param>
private void EasyTouch_On_TouchUp(Gesture gesture)
{
if (On_TouchUp != null)
On_TouchUp(gesture);
}
private void FixedUpdate()
{
if (Vector3.Distance(CameraRoot.position, TargetPos) > 0.1f)
{
CameraRoot.position = Vector3.Lerp(CameraRoot.transform.position, TargetPos, Time.deltaTime * 10f);
if (On_SwipeCamera != null)
On_SwipeCamera();
}
else
{
if (updateCamera != null)
{
updateCamera();
updateCamera = null;
}
}
if (Vector3.Distance(CameraRoot.eulerAngles, EulerAngles) > 0.1f)
{
Quaternion temp = new Quaternion();
temp.eulerAngles = EulerAngles;
CameraRoot.rotation = Quaternion.RotateTowards(CameraRoot.rotation, temp, Time.deltaTime * 50f);
}
}
/// <summary>
/// 设置触摸是否可用
/// </summary>
/// <param name="b"></param>
public static void SetTouchActive(bool b)
{
EasyTouch.SetEnabled(b);
}
public float GetCameraDistance()
{
return Vector3.Distance(CameraRoot.position, WorldCameraObj.transform.position);
}
public void InitWorldCamera()
{
TargetPos = new Vector3(0, 0, 0);
CameraRoot.position = TargetPos;
EulerAngles = new Vector3(0, 0, 0);
CameraRoot_.eulerAngles = EulerAngles;
WorldCamera.fieldOfView = 30f;
WorldCamera.farClipPlane = 50000;
WorldCameraObj.transform.localPosition = new Vector3(0, 25f, 0);
WorldCameraObj.transform.localEulerAngles = new Vector3(65f, 0, 0);
BoundX = new RangeAttribute(BoundaryX.x, BoundaryX.y);
BoundZ = new RangeAttribute(BoundaryZ.x, BoundaryZ.y);
}
/// <summary>
/// 摄像机跳转到指定坐标
/// </summary>
/// <param name="pos"></param>
/// <param name="callback"></param>
public void MoveCameraToPosition(Vector3 pos, CameraHandler callback = null)
{
// 需要注意跳转目标坐标如果超出边界限制的话需要跳转目标坐标的指定方向最大值
Vector3 temp = pos;
if (pos.x <= BoundX.min)
temp.x = BoundX.min;
if (pos.x >= BoundX.max)
temp.x = BoundX.max;
if (pos.z <= BoundZ.min)
temp.z = BoundZ.min;
if (pos.z >= BoundZ.max)
temp.z = BoundZ.max;
TargetPos.x = temp.x;
TargetPos.z = temp.z;
updateCamera += callback;
}
private event CameraHandler updateCamera;
public void SetTargetPOS(Vector3 pos)
{
TargetPos.x = pos.x;
TargetPos.z = pos.z;
}
}