Cute_demon_attacks/meng_yao/Assets/communal/camera/Cinemachine_2dCon.cs

195 lines
4.8 KiB
C#
Raw Normal View History

2024-10-19 16:54:28 +08:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
2024-10-26 10:52:38 +08:00
using System;
2024-10-19 16:54:28 +08:00
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;
2024-10-26 10:52:38 +08:00
//<2F>¼<EFBFBD><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڿ<EFBFBD><DABF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><D8B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>߼<EFBFBD>
public static Action<bool> SetCameraContorl;
private bool IsCan = true;
2024-10-19 16:54:28 +08:00
/// <summary>
/// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָλ<D6B8><CEBB>
/// </summary>
private Vector3 m_oneFingerDragStartPos;
/// <summary>
/// ˫ָ<CBAB><D6B8><EFBFBD>ŵ<EFBFBD><C5B5><EFBFBD>һ֡<D2BB><D6A1>˫ָ<CBAB><D6B8><EFBFBD><EFBFBD>
/// </summary>
private float m_twoFingerLastDistance = -1;
2024-10-26 10:52:38 +08:00
2024-10-19 16:54:28 +08:00
private void Awake()
2024-10-26 10:52:38 +08:00
{
2024-10-19 16:54:28 +08:00
m_selfTrans = transform;
}
private void Update()
{
2024-10-26 10:52:38 +08:00
if (!IsCan)
{
return;
}
2024-10-19 16:54:28 +08:00
#if UNITY_EDITOR || UNITY_STANDALONE
2024-11-08 00:41:10 +08:00
//// <20><><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EAB0B4>ʱ
//if (Input.GetMouseButtonDown(0))
//{
// m_fingerDown = true;
// m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition); // <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ʼλ<CABC><CEBB>
//}
2024-10-19 16:54:28 +08:00
2024-11-08 00:41:10 +08:00
//// <20><><EFBFBD><EFBFBD><EFBFBD>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD>ʱ
//if (Input.GetMouseButtonUp(0))
//{
// m_fingerDown = false;
// m_twoFingerLastDistance = -1;
//}
2024-10-19 16:54:28 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>϶<EFBFBD>
2024-11-07 20:43:21 +08:00
//if (m_fingerDown)
//{
// HandleFingerDragMove(Input.mousePosition);
//}
2024-10-19 16:54:28 +08:00
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2024-11-07 20:27:33 +08:00
//var distance = Input.GetAxis("Mouse ScrollWheel");
//HandleMouseScrollWheel(distance * 10);
2024-10-19 16:54:28 +08:00
#else
// <20>ƶ<EFBFBD><C6B6>ˣ<EFBFBD>˫ָ<CBAB><D6B8><EFBFBD><EFBFBD>
if (Input.touchCount == 2)
{
HandleTwoFingerScale();
m_fingerDown = false; // <20><><EFBFBD><EFBFBD>˫ָ<CBAB><D6B8><EFBFBD><EFBFBD>ʱ<EFBFBD>󴥵<EFBFBD>ָ<EFBFBD>϶<EFBFBD>
}
// <20>ƶ<EFBFBD><C6B6>ˣ<EFBFBD><CBA3><EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
else if (Input.touchCount == 1)
{
Touch touch = Input.touches[0];
// <20><>ָ<EFBFBD>մ<EFBFBD><D5B4><EFBFBD><EFBFBD><EFBFBD>Ļʱ
if (touch.phase == TouchPhase.Began)
{
m_fingerDown = true;
m_oneFingerDragStartPos = GetWorldPos(touch.position); // <20><>ȡ<EFBFBD><C8A1>ָ<EFBFBD><D6B8><EFBFBD>µ<EFBFBD><C2B5><EFBFBD>ʼλ<CABC><CEBB>
}
// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>Ļ<EFBFBD>ϻ<EFBFBD><CFBB><EFBFBD>ʱ
else if (touch.phase == TouchPhase.Moved)
{
HandleFingerDragMove(touch.position); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>϶<EFBFBD>
}
m_twoFingerLastDistance = -1; // <20><>λ˫ָ<CBAB><D6B8><EFBFBD>ž<EFBFBD><C5BE><EFBFBD>
}
else
{
m_fingerDown = false;
m_twoFingerLastDistance = -1;
}
#endif
}
/// <summary>
/// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
/// </summary>
private void HandleFingerDragMove(Vector2 fingerPos)
{
2024-11-08 00:41:10 +08:00
////<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//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;
2024-10-19 16:54:28 +08:00
}
/// <summary>
/// ˫ָ<CBAB><D6B8><EFBFBD><EFBFBD>
/// </summary>
private void HandleTwoFingerScale()
{
float distance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
if (m_twoFingerLastDistance == -1)
{
m_twoFingerLastDistance = distance;
return; // <20><><EFBFBD><EFBFBD><EFBFBD>״<EFBFBD>ִ<EFBFBD><D6B4>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ű<EFBFBD><C5B1><EFBFBD>
float scale = 0.1f * (distance - m_twoFingerLastDistance);
ScaleCamere(scale);
m_twoFingerLastDistance = distance;
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="distance"></param>
2024-11-07 20:27:33 +08:00
//private void HandleMouseScrollWheel(float distance)
//{
// if (0 == distance) return;
// ScaleCamere(distance);
//}
2024-10-19 16:54:28 +08:00
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
/// </summary>
/// <param name="scale"></param>
private void ScaleCamere(float scale)
2024-10-26 10:52:38 +08:00
{
2024-10-19 16:54:28 +08:00
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>
/// <20><>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD><EABBBB><EFBFBD><EFBFBD>3D<33><44><EFBFBD><EFBFBD>
/// </summary>
/// <param name="screenPos"><3E><>Ļ<EFBFBD><C4BB><EFBFBD><EFBFBD></param>
/// <returns></returns>
Vector3 GetWorldPos(Vector2 screenPos)
{
return m_cam.ScreenToWorldPoint(new Vector3(screenPos.x, screenPos.y, Mathf.Abs(m_cam.transform.position.z)));
}
2024-10-26 10:52:38 +08:00
//<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>й¶
private void OnEnable()
{
SetCameraContorl += ToggleCameraControl;
}
private void OnDisable()
{
SetCameraContorl -= ToggleCameraControl;
}
/// <summary>
///
/// </summary>
/// <param name="isActive"><3E><><EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD></param>
private void ToggleCameraControl(bool TorF)
{
IsCan = TorF;
}
2024-10-19 16:54:28 +08:00
}