目录
- 1. AroundCameraMobile
- 2. AroundCameraPC
- 3. 调用方法
1. AroundCameraMobile
using Developer.CameraExtension;
using UnityEngine;
[RequireComponent(typeof(Camera))]
public class AroundCameraMobile : MonoBehaviour
{
#region Mono-Singleton
private static AroundCameraMobile instance;
public static AroundCameraMobile Instance
{
get
{
if (!instance)
instance = FindObjectOfType<AroundCameraMobile>();
return instance;
}
}
#endregion
#region Property and Field
private Transform target;
private Vector2 savePreAngles;
private bool isBegin;
public MouseSettings touchSettings = new MouseSettings(1, 10, 10);
public Range angleRange = new Range(-90, 90);
public Range distanceRange = new Range(1, 10);
[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;
private enum TouchType
{
None,
Move,
Scale,
Rotate
}
private TouchType touchType = TouchType.None;
#endregion
#region Protected Method
protected virtual void Start()
{
target = new GameObject("selftarget").transform;
target.SetParent(transform.root);
currentAngles = targetAngles = transform.eulerAngles;
savePreAngles = transform.eulerAngles;
currentDistance = targetDistance = Vector3.Distance(transform.position, target.position);
}
protected virtual void LateUpdate()
{
if (!isBegin)
return;
#if UNITY_STANDALONE
CheckMouseInput();
#endif
#if UNITY_ANDROID
CheckTouchInput();
#endif
}
public void SetTarget(Transform tar)
{
if (!ReferenceEquals(tar, null))
{
target.position = tar.position;
isBegin = true;
}
}
public void SetDefault()
{
target.position = Vector3.zero;
isBegin = false;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
currentAngles = targetAngles = savePreAngles;
currentDistance = targetDistance = 50;
}
protected void CheckMouseInput()
{
if (Input.GetMouseButton(0))
{
targetAngles.y += Input.GetAxis("Mouse X") * touchSettings.pointerSensitivity * 50;
targetAngles.x -= Input.GetAxis("Mouse Y") * touchSettings.pointerSensitivity * 50;
targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
}
if (Input.GetMouseButton(2))
{
float xAxis = Input.GetAxis("Mouse X");
float yAxis = Input.GetAxis("Mouse Y");
target.localPosition -= transform.right.normalized * xAxis + transform.up.normalized * yAxis;
}
targetDistance -= Input.GetAxis("Mouse ScrollWheel") * touchSettings.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);
transform.rotation = Quaternion.Euler(currentAngles);
transform.position = target.position - transform.forward * currentDistance;
}
protected void CheckTouchInput()
{
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
if (touch.phase == TouchPhase.Began)
touchType = TouchType.Rotate;
if (touchType == TouchType.Rotate)
{
Vector2 deltaPos = touch.deltaPosition;
targetAngles.y += deltaPos.x * touchSettings.pointerSensitivity;
targetAngles.x -= deltaPos.y * touchSettings.pointerSensitivity;
targetAngles.x = Mathf.Clamp(targetAngles.x, angleRange.min, angleRange.max);
}
}
if (Input.touchCount == 2)
{
Touch touchZero = Input.GetTouch(0);
Touch touchOne = Input.GetTouch(1);
if (touchZero.phase == TouchPhase.Ended || touchOne.phase == TouchPhase.Ended)
{
touchType = TouchType.None;
return;
}
Vector2 touchZeroPrevPos = touchZero.position - touchZero.deltaPosition;
Vector2 touchOnePrevPos = touchOne.position - touchOne.deltaPosition;
var dir = Vector2.Dot(touchZero.deltaPosition, touchOne.deltaPosition);
touchType = dir > 0 ? TouchType.Move : TouchType.Scale;
if (touchType == TouchType.Move)
{
target.position -= (transform.right.normalized * touchZero.deltaPosition.x + transform.up.normalized * touchZero.deltaPosition.y) * 0.02f;
return;
}
if (touchType == TouchType.Scale)
{
float prevTouchDeltaMag = (touchZeroPrevPos - touchOnePrevPos).magnitude;
float touchDeltaMag = (touchZero.position - touchOne.position).magnitude;
float deltaMagnitudeDiff = prevTouchDeltaMag - touchDeltaMag;
float scaleF = -deltaMagnitudeDiff / 1000f;
targetDistance -= scaleF * touchSettings.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);
transform.rotation = Quaternion.Euler(currentAngles);
transform.position = target.position - transform.forward * currentDistance;
}
#endregion
}
2. AroundCameraPC
using UnityEngine;
namespace Developer.CameraExtension
{
[RequireComponent(typeof(Camera))]
[AddComponentMenu("Developer/CameraExtension/AroundCamera")]
public class AroundCameraPC : MonoBehaviour
{
#region Property and Field
public MouseSettings mouseSettings = new MouseSettings(1, 10, 10);
public Range angleRange = new Range(-90, 90);
public Range distanceRange = new Range(1, 10);
[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 Mono-Singleton
private static AroundCameraPC instance;
public static AroundCameraPC Instance
{
get
{
if (!instance)
instance = FindObjectOfType<AroundCameraPC>();
return instance;
}
}
#endregion
private Vector2 savePreAngles;
private Transform selfTarget;
private bool isBegin;
#region Protected Method
protected virtual void Start()
{
selfTarget = new GameObject("selftarget").transform;
selfTarget.SetParent(transform.root);
currentAngles = targetAngles = transform.eulerAngles;
savePreAngles = transform.eulerAngles;
currentDistance = targetDistance = Vector3.Distance(transform.position, selfTarget.position) + 50;
}
protected virtual void LateUpdate()
{
if (!isBegin)
return;
CheckMouseInput();
}
public void SetTarget(Transform tar)
{
if (!ReferenceEquals(tar, null))
{
selfTarget.position = tar.position;
isBegin = true;
}
}
public void SetDefault()
{
selfTarget.position = Vector3.zero;
isBegin = false;
transform.localPosition = Vector3.zero;
transform.localRotation = Quaternion.identity;
currentAngles = targetAngles = savePreAngles;
currentDistance = targetDistance = 50;
}
protected void CheckMouseInput()
{
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 (Input.GetMouseButton(2))
{
float xAxis = Input.GetAxis("Mouse X");
float yAxis = Input.GetAxis("Mouse Y");
selfTarget.localPosition -= transform.right.normalized * xAxis + transform.up.normalized * yAxis;
}
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);
transform.rotation = Quaternion.Euler(currentAngles);
transform.position = selfTarget.position - transform.forward * currentDistance;
}
#endregion
}
}
3. 调用方法
using System.Collections.Generic;
using Developer.CameraExtension;
using UnityEngine;
using UnityEngine.UI;
public class CommonGameTarget : MonoBehaviour
{
private Button TuoKaBtn, DeTuoKaBtn;
public GameObject GameTargetPa;
public List<GameObject> ControlGameObject;
private GameObject ControlGoProp
{
get
{
foreach (var g in ControlGameObject)
if (g.activeInHierarchy)
return g;
return null;
}
}
private ImageTargetInherit m_ImageTarget;
private void Start()
{
TuoKaBtn = Utils.UtilsSearch.RecursiveFindChild(transform, "btn_Tuoka").GetComponent<Button>();
DeTuoKaBtn = Utils.UtilsSearch.RecursiveFindChild(transform, "btn_DeTuoKa").GetComponent<Button>();
TuoKaBtn.onClick.AddListener(StartTuoKa);
DeTuoKaBtn.onClick.AddListener(StopTuoKa);
m_ImageTarget = transform.FindInParents<ImageTargetInherit>();
}
private void StartTuoKa()
{
m_ImageTarget.IsTuoKa = true;
EasyARManager.Instance.StartTrack();
TuoKaBtn.gameObject.SetActive(false);
DeTuoKaBtn.gameObject.SetActive(true);
AroundCameraMobile.Instance.SetTarget(ControlGoProp.transform);
}
private void StopTuoKa()
{
m_ImageTarget.IsTuoKa = false;
m_ImageTarget.OnStopTuoKa();
EasyARManager.Instance.StopTrack();
TuoKaBtn.gameObject.SetActive(true);
DeTuoKaBtn.gameObject.SetActive(false);
AroundCameraMobile.Instance.SetDefault();
}
}