我这里使用了 EasyTouch插件
实例代码:
using UnityEngine;
using System;
using System.Collections;
using HedgehogTeam.EasyTouch;
public class CameraController : MonoBehaviour
{
public bool isRotate = true;
//摄像机的距离
public float fieldOfView;
//用来改变摄像机的距离
public float frameIndex = 60;
//是否可以 双指缩放摄像机
public bool isPinchCamera = true;
// Text m_debugTip;
public bool canRotation_X = true;
public bool canRotation_Y = true;
public bool canScale = true;
#region Field and Property
/// <summary>
/// Around center.
/// </summary>
//public Transform target;
/// <summary>
/// Settings of mouse button, pointer and scrollwheel.
/// </summary>
public MouseSettings mouseSettings = new MouseSettings(0, 10, 10);
/// <summary>
/// Range limit of angle.
/// </summary>
public Range angleRange = new Range(-90, 90);
/// <summary>
/// Range limit of distance.
/// </summary>
public Range distanceRange = new Range(1, 10);
/// <summary>
/// Damper for move and rotate.
/// </summary>
[Range(0, 15)]
public float damper = 5;
/// <summary>
/// Camera current angls.
/// </summary>
public Vector2 CurrentAngles { protected set; get; }
/// <summary>
/// Current distance from camera to target.
/// </summary>
public float CurrentDistance { protected set; get; }
/// <summary>
/// Camera target angls.
/// </summary>
protected Vector2 targetAngles;
/// <summary>
/// Target distance from camera to target.
/// </summary>
protected float targetDistance;
#endregion
#region Protected Method
protected virtual void Start()
{
// m_debugTip = GameObject.Find("Text").GetComponent<Text>();
CurrentAngles = targetAngles = transform.eulerAngles;
//CurrentDistance = targetDistance = Vector3.Distance(transform.position, target.position);
}
protected virtual void LateUpdate()
{
#if UNITY_EDITOR || UNITY_STANDALONE_WIN || UNITY_STANDALONE_OSX
AroundByMouseInput();
#elif UNITY_ANDROID || UNITY_IPHONE
AroundByMobileInput();
#endif
}
private void Awake()
{
isRotate = true;
}
// Update is called once per frame
void FixedUpdate()
{
//获取当前触摸点
Gesture gesture = EasyTouch.current;
if (gesture != null && EasyTouch.EvtType.On_Swipe == gesture.type)
{
// OnSwiper(gesture);
}
//if (gesture != null && EasyTouch.EvtType.On_Pinch == gesture.type)
//{
// OnPinch(gesture);
//}
if (gesture != null && EasyTouch.EvtType.On_PinchIn == gesture.type)
{
On_PinchIn(gesture);
}
if (gesture != null && EasyTouch.EvtType.On_PinchOut == gesture.type)
{
On_PinchOut(gesture);
}
if (gesture != null && EasyTouch.EvtType.On_PinchEnd == gesture.type)
{
On_PinchEnd(gesture);
}
if (gesture != null && EasyTouch.EvtType.On_TouchUp == gesture.type)
{
On_TouchUp(gesture);
}
//if (gesture != null && EasyTouch.EvtType.On_TouchStart2Fingers == gesture.type)
//{
// On_TouchStart2Fingers(gesture);
//}
if (frameIndex > 100)
{
frameIndex = 100;
}
if (frameIndex < 25)
{
frameIndex = 25;
}
//ScaleCamera();
}
//记录上一次手机触摸位置判断用户是在左放大还是缩小手势
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private bool m_IsSingleFinger;
public float distance = 10;
public float minDistance = 25;
public float maxDistance = 100;
public float scaleFactor = 0.2f;
private void OnEnable()
{
//targetAngles.y = -30f;
//targetAngles.x = 0;
isRotate = true;
}
private void OnDisable()
{
//transform.localRotation = Quaternion.Euler(0.0f, -30, 0.0f);
//transform.localRotation = Quaternion.Euler(new Vector2(0f,-30f));
//isRotate = false;
//transform.Rotate(new Vector3(0,-30,-0),Space.Self);
targetAngles.y = -30f;
targetAngles.x = 0;
transform.localPosition = new Vector3(0,611,0);
GetComponent<Camera>().fieldOfView = 65;
//transform.eulerAngles = new Vector2(0,-30f);
}
private void On_PinchIn(Gesture gesture)
{
isPinchCamera = true;
isRotate = false;
if (isPinchCamera == true && isRotate == false)
{
frameIndex += gesture.deltaPinch * Time.deltaTime + 1;
frameIndex += gesture.deltaPinch * Time.deltaTime + 2;
frameIndex += gesture.deltaPinch * Time.deltaTime + 3;
frameIndex = (float)Math.Ceiling(frameIndex);
if (frameIndex > 100)
{
return;
}
//float field = GetComponent<Camera>().fieldOfView - (distance * 1f);
//GetComponent<Camera>().fieldOfView = Mathf.Clamp(field, 25, 100);
GetComponent<Camera>().fieldOfView = frameIndex;
}
}
private void On_PinchOut(Gesture gesture)
{
isPinchCamera = true;
isRotate = false;
if (isPinchCamera == true && isRotate == false)
{
frameIndex -= gesture.deltaPinch * Time.deltaTime + 1;
frameIndex -= gesture.deltaPinch * Time.deltaTime + 2;
frameIndex -= gesture.deltaPinch * Time.deltaTime + 3;
frameIndex = (float)Math.Floor(frameIndex);
if (frameIndex < 25)
{
return;
}
//float field = GetComponent<Camera>().fieldOfView - (distance * 1f);
//GetComponent<Camera>().fieldOfView = Mathf.Clamp(field, 25, 100);
GetComponent<Camera>().fieldOfView = frameIndex;
}
}
private void On_PinchEnd(Gesture gesture)
{
Debug.Log("我是On_PinchEnd");
isRotate = true;
}
private void On_TouchUp(Gesture gesture)
{
Debug.Log("我是On_TouchUp");
isRotate = true;
}
//private void On_TouchStart2Fingers(Gesture gesture)
//{
// Debug.Log("我是On_TouchStart2Fingers");
// // Verification that the action on the object
// if (gesture.pickedObject == gameObject)
// {
// EasyTouch.SetEnablePinch(true);
// }
//}
//当触摸滑动
private void OnSwiper(Gesture gesture)
{
isPinchCamera = false;
isRotate = true;
if (isPinchCamera == false && isRotate == true)
{
Debug.Log("OnSwiper");
Debug.Log("gesture.swipe ==" + gesture.swipe);
}
}
//private void ScaleCamera()
//{
// //计算出当前两点触摸点的位置
// var tempPosition1 = Input.GetTouch(0).position;
// var tempPosition2 = Input.GetTouch(1).position;
// float currentTouchDistance = Vector3.Distance(tempPosition1, tempPosition2);
// float lastTouchDistance = Vector3.Distance(oldPosition1, oldPosition2);
// //计算上次和这次双指触摸之间的距离差距
// //然后去更改摄像机的距离
// GetComponent<Camera>().fieldOfView -= ( currentTouchDistance - lastTouchDistance ) * scaleFactor * Time.deltaTime;
// //把距离限制住在min和max之间
// GetComponent<Camera>().fieldOfView = Mathf.Clamp(distance, minDistance, maxDistance);
// //备份上一次触摸点的位置,用于对比
// oldPosition1 = tempPosition1;
// oldPosition2 = tempPosition2;
//}
public void AroundByMobileInput()
{
//isRotate = true;
if (isRotate)
{
Debug.Log("我是移动端");
if (Input.touchCount == 1)
{
if (Input.touches[0].phase == TouchPhase.Moved)
{
targetAngles.y += Input.GetAxis("Mouse X") * mouseSettings.pointerSensitivity;
targetAngles.x -= Input.GetAxis("Mouse Y") * mouseSettings.pointerSensitivity;
//Range.
targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
}
//Mouse pointer.
m_IsSingleFinger = true;
}
//Mouse scrollwheel.
if (canScale)
{
if (Input.touchCount > 1)
{
//计算出当前两点触摸点的位置
if (m_IsSingleFinger)
{
oldPosition1 = Input.GetTouch(0).position;
oldPosition2 = Input.GetTouch(1).position;
}
if (Input.touches[0].phase == TouchPhase.Moved && Input.touches[1].phase == TouchPhase.Moved)
{
var tempPosition1 = Input.GetTouch(0).position;
var tempPosition2 = Input.GetTouch(1).position;
float currentTouchDistance = Vector3.Distance(tempPosition1, tempPosition2);
float lastTouchDistance = Vector3.Distance(oldPosition1, oldPosition2);
//计算上次和这次双指触摸之间的距离差距
//然后去更改摄像机的距离
targetDistance -= (currentTouchDistance - lastTouchDistance) * Time.deltaTime * mouseSettings.wheelSensitivity;
// m_debugTip.text = ( currentTouchDistance - lastTouchDistance ).ToString() + " + " + targetDistance.ToString();
//把距离限制住在min和max之间
//备份上一次触摸点的位置,用于对比
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
m_IsSingleFinger = false;
}
}
}
targetDistance = Mathf.Clamp(targetDistance, distanceRange.min, distanceRange.max);
//Lerp.
CurrentAngles = Vector2.Lerp(CurrentAngles, targetAngles, damper * Time.deltaTime);
CurrentDistance = Mathf.Lerp(CurrentDistance, targetDistance, damper * Time.deltaTime);
if (!canRotation_X) targetAngles.y = 0;
if (!canRotation_Y) targetAngles.x = 0;
//Update transform position and rotation.
transform.rotation = Quaternion.Euler(CurrentAngles);
//transform.position = target.position - transform.forward * CurrentDistance;
// transform.position = target.position - Vector3.forward * CurrentDistance;
}
}
/// <summary>
/// Camera around target by mouse input.
/// </summary>
protected void AroundByMouseInput()
{
//isRotate = true;
if (isRotate)
{
if (Input.GetMouseButton(mouseSettings.mouseButtonID))
{
//Mouse pointer.
targetAngles.y += Input.GetAxis("Mouse X") * mouseSettings.pointerSensitivity;
targetAngles.x -= Input.GetAxis("Mouse Y") * mouseSettings.pointerSensitivity;
//Range.
targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
}
//Mouse scrollwheel.
if (canScale)
{
targetDistance -= Input.GetAxis("Mouse ScrollWheel") * mouseSettings.wheelSensitivity;
}
// m_debugTip.text = Input.GetAxis("Mouse ScrollWheel").ToString() + " + " + targetDistance.ToString();
targetDistance = Mathf.Clamp(targetDistance, distanceRange.min, distanceRange.max);
//Lerp.
CurrentAngles = Vector2.Lerp(CurrentAngles, targetAngles, damper * Time.deltaTime);
CurrentDistance = Mathf.Lerp(CurrentDistance, targetDistance, damper * Time.deltaTime);
if (!canRotation_X) targetAngles.y = 0;
if (!canRotation_Y) targetAngles.x = 0;
//Update transform position and rotation.
transform.rotation = Quaternion.Euler(CurrentAngles);
//transform.position = target.position - transform.forward * CurrentDistance;
// transform.position = target.position - Vector3.forward * CurrentDistance;
}
}
#endregion
}
[Serializable]
public struct MouseSettings
{
/// <summary>
/// ID of mouse button.
/// </summary>
public int mouseButtonID;
/// <summary>
/// Sensitivity of mouse pointer.
/// </summary>
public float pointerSensitivity;
/// <summary>
/// Sensitivity of mouse ScrollWheel.
/// </summary>
public float wheelSensitivity;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="mouseButtonID">ID of mouse button.</param>
/// <param name="pointerSensitivity">Sensitivity of mouse pointer.</param>
/// <param name="wheelSensitivity">Sensitivity of mouse ScrollWheel.</param>
public MouseSettings(int mouseButtonID, float pointerSensitivity, float wheelSensitivity)
{
this.mouseButtonID = mouseButtonID;
this.pointerSensitivity = pointerSensitivity;
this.wheelSensitivity = wheelSensitivity;
}
}
/// <summary>
/// Range form min to max.
/// </summary>
[Serializable]
public struct Range
{
/// <summary>
/// Min value of range.
/// </summary>
public float min;
/// <summary>
/// Max value of range.
/// </summary>
public float max;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="min">Min value of range.</param>
/// <param name="max">Max value of range.</param>
public Range(float min, float max)
{
this.min = min;
this.max = max;
}
}
/// <summary>
/// Rectangle area on plane.
/// </summary>
[Serializable]
public struct PlaneArea
{
/// <summary>
/// Center of area.
/// </summary>
public Transform center;
/// <summary>
/// Width of area.
/// </summary>
public float width;
/// <summary>
/// Length of area.
/// </summary>
public float length;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="center">Center of area.</param>
/// <param name="width">Width of area.</param>
/// <param name="length">Length of area.</param>
public PlaneArea(Transform center, float width, float length)
{
this.center = center;
this.width = width;
this.length = length;
}
}
/// <summary>
/// Target of camera align.
/// </summary>
[Serializable]
public struct AlignTarget
{
/// <summary>
/// Center of align target.
/// </summary>
public Transform center;
/// <summary>
/// Angles of align.
/// </summary>
public Vector2 angles;
/// <summary>
/// Distance from camera to target center.
/// </summary>
public float distance;
/// <summary>
/// Range limit of angle.
/// </summary>
public Range angleRange;
/// <summary>
/// Range limit of distance.
/// </summary>
public Range distanceRange;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="center">Center of align target.</param>
/// <param name="angles">Angles of align.</param>
/// <param name="distance">Distance from camera to target center.</param>
/// <param name="angleRange">Range limit of angle.</param>
/// <param name="distanceRange">Range limit of distance.</param>
public AlignTarget(Transform center, Vector2 angles, float distance, Range angleRange, Range distanceRange)
{
this.center = center;
this.angles = angles;
this.distance = distance;
this.angleRange = angleRange;
this.distanceRange = distanceRange;
}
}
将以上脚本拖拽到 摄像机上就可以了
挂载的脚本 设置如下 ?