Unity Window触摸屏电脑和移动端Input触控,控制相机旋转缩放
using UnityEngine;
using System;
using System.Collections;
public class CameraController : MonoBehaviour
{
public bool canRotation_X = true;
public bool canRotation_Y = true;
public bool canScale = true;
#region 属性
public Transform target;
public MouseSettings mouseSettings = new MouseSettings(0, 10, 10);
public Range angleRange = new Range(-90, 90);
public Range distanceRange = new Range(0, 10);
public PlaneArea PlaneArea = new PlaneArea();
public AlignTarget AlignTarget = new AlignTarget();
[Range(0, 10)]
public float damper = 5;
public Vector2 CurrentAngles {
protected set; get; }
public float CurrentDistance {
protected set; get; }
protected Vector2 targetAngles;
protected float targetDistance;
#endregion
#region Protected Method
protected virtual void Start()
{
CurrentAngles = targetAngles = transform.eulerAngles;
CurrentDistance = targetDistance = Vector3.Distance(transform.position, target.position);
}
protected virtual void LateUpdate()
{
#if UNITY_EDITOR
AroundByMouseInput();
#elif UNITY_STANDALONE_WIN
AroundByMobileInput();
#elif UNITY_ANDROID || UNITY_IPHONE
AroundByMobileInput();
#endif
}
private Vector2 oldPosition1;
private Vector2 oldPosition2;
private bool m_IsSingleFinger;
protected void AroundByMobileInput()
{
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;
targetAngles.y += Input.touches[0].deltaPosition.x * Time.deltaTime * 5;
targetAngles.x -= Input.touches[0].deltaPosition.y * Time.deltaTime * 5;
targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
}
m_IsSingleFinger = true;
}
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;
oldPosition1 = tempPosition1;
oldPosition2 = tempPosition2;
m_IsSingleFinger = false;
}
}
}
targetDistance = Mathf.Clamp(targetDistance, distanceRange.min, distanceRange.max);
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;
transform.rotation = Quaternion.Euler(CurrentAngles);
transform.position = target.position - transform.forward * CurrentDistance;
}
protected void AroundByMouseInput()
{
if (Input.GetMouseButton(mouseSettings.mouseButtonID))
{
targetAngles.y += Input.GetAxis("Mouse X") * mouseSettings.pointerSensitivity;
targetAngles.x -= Input.GetAxis("Mouse Y") * mouseSettings.pointerSensitivity;
targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
}
if (canScale)
{
targetDistance -= Input.GetAxis("Mouse ScrollWheel") * mouseSettings.wheelSensitivity;
}
targetDistance = Mathf.Clamp(targetDistance, distanceRange.min, distanceRange.max);
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;
transform.rotation = Quaternion.Euler(CurrentAngles);
transform.position = target.position - transform.forward * CurrentDistance;
}
#endregion
}
[Serializable]
public struct MouseSettings
{
public int mouseButtonID;
public float pointerSensitivity;
public float wheelSensitivity;
public MouseSettings(int mouseButtonID, float pointerSensitivity, float wheelSensitivity)
{
this.mouseButtonID = mouseButtonID;
this.pointerSensitivity = pointerSensitivity;
this.wheelSensitivity = wheelSensitivity;
}
}
[Serializable]
public struct Range
{
public float min;
public float max;
public Range(float min, float max)
{
this.min = min;
this.max = max;
}
}
[Serializable]
public struct PlaneArea
{
public Transform center;
public float width;
public float length;
public PlaneArea(Transform center, float width, float length)
{
this.center = center;
this.width = width;
this.length = length;
}
}
[Serializable]
public struct AlignTarget
{
public Transform center;
public Vector2 angles;
public float distance;
public Range angleRange;
public Range distanceRange;
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;
}
}