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

144 lines
3.6 KiB
C#
Raw Normal View History

2024-10-19 02:50:18 +08:00
using UnityEngine;
2024-10-19 16:54:28 +08:00
//[RequireComponent(typeof(Camera))]
2024-10-19 02:50:18 +08:00
public class CameraControl : MonoBehaviour
{
public float cameraMaxSize;
public float cameraMinSize;
2024-10-19 16:54:28 +08:00
public Camera m_cam;
2024-10-19 02:50:18 +08:00
private Transform m_selfTrans;
private bool m_fingerDown = false;
2024-10-19 16:54:28 +08:00
2024-10-19 02:50:18 +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;
private void Awake()
2024-10-19 16:54:28 +08:00
{
2024-10-19 02:50:18 +08:00
m_selfTrans = transform;
}
private void Update()
{
#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 (2 == Input.touchCount)
{
// ˫ָ<CBAB><D6B8><EFBFBD><EFBFBD>
HandleTwoFingerScale();
}
else if (1 == Input.touchCount)
{
if (TouchPhase.Began == Input.touches[0].phase)
{
m_fingerDown = true;
m_oneFingerDragStartPos = GetWorldPos(Input.mousePosition);
}
else if (TouchPhase.Moved == Input.touches[0].phase)
{
// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
HandleFingerDragMove(Input.touches[0].position);
}
m_twoFingerLastDistance = -1;
}
else
{
m_fingerDown = false;
m_twoFingerLastDistance = -1;
}
#endif
}
/// <summary>
/// <20><>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>
/// </summary>
private void HandleFingerDragMove(Vector2 fingerPos)
{
//<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;
}
/// <summary>
/// ˫ָ<CBAB><D6B8><EFBFBD><EFBFBD>
/// </summary>
private void HandleTwoFingerScale()
{
float distance = Vector2.Distance(Input.touches[0].position, Input.touches[1].position);
if (-1 == m_twoFingerLastDistance) m_twoFingerLastDistance = distance;
// <20><><EFBFBD><EFBFBD>һ֡<D2BB>Ƚϱ
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>
private void HandleMouseScrollWheel(float distance)
{
if (0 == distance) return;
ScaleCamere(distance);
}
/// <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-19 16:54:28 +08:00
m_cam.orthographicSize -= scale * 0.01f;
2024-10-19 02:50:18 +08:00
if (m_cam.orthographicSize < cameraMinSize)
{
m_cam.orthographicSize = cameraMinSize;
}
if (m_cam.orthographicSize > cameraMaxSize)
{
m_cam.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, 0));
}
}