Cute_demon_attacks/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs
2024-11-07 20:43:21 +08:00

195 lines
4.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using System;
public class Cinemachine_2dCon : MonoBehaviour
{
public float cameraMaxSize;
public float cameraMinSize;
public float zoomSpeed;
public CinemachineVirtualCamera v_cam;
public Camera m_cam;
private Transform m_selfTrans;
private bool m_fingerDown = false;
//事件,用于开启或关闭摄像逻辑
public static Action<bool> SetCameraContorl;
private bool IsCan = true;
/// <summary>
/// 单指滑动的手指位置
/// </summary>
private Vector3 m_oneFingerDragStartPos;
/// <summary>
/// 双指缩放的上一帧的双指距离
/// </summary>
private float m_twoFingerLastDistance = -1;
private void Awake()
{
m_selfTrans = transform;
}
private void Update()
{
if (!IsCan)
{
return;
}
#if UNITY_EDITOR || UNITY_STANDALONE
// 桌面端:鼠标按下时
if (Input.GetMouseButtonDown(0))
{
m_fingerDown = true;
m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition); // 获取鼠标按下的起始位置
}
// 桌面端:鼠标松开时
if (Input.GetMouseButtonUp(0))
{
m_fingerDown = false;
m_twoFingerLastDistance = -1;
}
// 处理鼠标拖动
//if (m_fingerDown)
//{
// HandleFingerDragMove(Input.mousePosition);
//}
// 处理鼠标滚轮缩放
//var distance = Input.GetAxis("Mouse ScrollWheel");
//HandleMouseScrollWheel(distance * 10);
#else
// 移动端:双指缩放
if (Input.touchCount == 2)
{
HandleTwoFingerScale();
m_fingerDown = false; // 避免双指缩放时误触单指拖动
}
// 移动端:单指滑动
else if (Input.touchCount == 1)
{
Touch touch = Input.touches[0];
// 手指刚触碰屏幕时
if (touch.phase == TouchPhase.Began)
{
m_fingerDown = true;
m_oneFingerDragStartPos = GetWorldPos(touch.position); // 获取手指按下的起始位置
}
// 手指在屏幕上滑动时
else if (touch.phase == TouchPhase.Moved)
{
HandleFingerDragMove(touch.position); // 处理手指拖动
}
m_twoFingerLastDistance = -1; // 复位双指缩放距离
}
else
{
m_fingerDown = false;
m_twoFingerLastDistance = -1;
}
#endif
}
/// <summary>
/// 单指滑动
/// </summary>
private void HandleFingerDragMove(Vector2 fingerPos)
{
//滑动差
Vector3 cha = m_oneFingerDragStartPos - GetWorldPos(fingerPos);
Vector3 newP = m_cam.transform.position;
newP.x = newP.x + cha.x;
newP.y = newP.y + cha.y;
m_selfTrans.position = newP;
}
/// <summary>
/// 双指缩放
/// </summary>
private void HandleTwoFingerScale()
{
float distance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
if (m_twoFingerLastDistance == -1)
{
m_twoFingerLastDistance = distance;
return; // 避免首次执行时发生误缩放
}
// 计算缩放比例
float scale = 0.1f * (distance - m_twoFingerLastDistance);
ScaleCamere(scale);
m_twoFingerLastDistance = distance;
}
/// <summary>
/// 鼠标滚轮缩放
/// </summary>
/// <param name="distance"></param>
//private void HandleMouseScrollWheel(float distance)
//{
// if (0 == distance) return;
// ScaleCamere(distance);
//}
/// <summary>
/// 缩放摄像机的视口
/// </summary>
/// <param name="scale"></param>
private void ScaleCamere(float scale)
{
v_cam.m_Lens.OrthographicSize -= scale * zoomSpeed;
if (v_cam.m_Lens.OrthographicSize < cameraMinSize)
{
v_cam.m_Lens.OrthographicSize = cameraMinSize;
}
if (v_cam.m_Lens.OrthographicSize > cameraMaxSize)
{
v_cam.m_Lens.OrthographicSize = cameraMaxSize;
}
}
/// <summary>
/// 屏幕坐标换算成3D坐标
/// </summary>
/// <param name="screenPos">屏幕坐标</param>
/// <returns></returns>
Vector3 GetWorldPos(Vector2 screenPos)
{
return m_cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(m_cam.transform.position.z)));
}
//避免内存泄露
private void OnEnable()
{
SetCameraContorl += ToggleCameraControl;
}
private void OnDisable()
{
SetCameraContorl -= ToggleCameraControl;
}
/// <summary>
///
/// </summary>
/// <param name="isActive">禁用或启用</param>
private void ToggleCameraControl(bool TorF)
{
IsCan = TorF;
}
}