Shegotwet/Assets/ResWork/ShowAlbum/CameraDragMove.cs

97 lines
2.9 KiB
C#
Raw Permalink Normal View History

2025-09-08 22:28:02 +00:00
using UnityEngine;
public class CameraDragMove : MonoBehaviour
{
2025-09-12 03:38:36 +00:00
public ShowPanel m_ShowPanel;
2025-09-08 22:28:02 +00:00
public SpriteRenderer targetSprite; // 영역 기준이 되는 스프라이트
public GameObject go_btnbg;
public float dragSpeed = 1f;
private Vector3 lastTouchPos;
Vector3 lastcamPos;
private bool isDragging = false;
void Update()
{
#if UNITY_EDITOR || UNITY_STANDALONE
if (Input.GetMouseButtonDown(0) || Input.GetMouseButton(0) || Input.GetMouseButtonUp(0))
{
ProcessInput(
Input.GetMouseButtonDown(0),
Input.GetMouseButton(0),
Input.GetMouseButtonUp(0),
Input.mousePosition
);
}
#else
if (Input.touchCount == 1)
{
Touch touch = Input.GetTouch(0);
ProcessInput(
touch.phase == TouchPhase.Began,
touch.phase == TouchPhase.Moved,
touch.phase == TouchPhase.Ended,
touch.position
);
}
#endif
}
void ProcessInput(bool began, bool moved, bool ended, Vector3 position)
{
if (began)
{
lastTouchPos = position;
2025-09-12 03:38:36 +00:00
lastcamPos = Camera.main.transform.position;
2025-09-08 22:28:02 +00:00
isDragging = false;
}
else if (moved)
{
2025-09-12 03:38:36 +00:00
Vector3 deltaWorld = Camera.main.ScreenToWorldPoint(position) - Camera.main.ScreenToWorldPoint(lastTouchPos);
2025-09-08 22:28:02 +00:00
deltaWorld.z = 0f;
2025-09-12 03:38:36 +00:00
Camera.main.transform.position -= deltaWorld * dragSpeed;
2025-09-08 22:28:02 +00:00
ClampCameraPosition();
lastTouchPos = position;
2025-09-12 03:38:36 +00:00
if (lastcamPos != Camera.main.transform.position || Mathf.Abs(deltaWorld.x) >= 0.01f || Mathf.Abs(deltaWorld.y) >= 0.01f)
2025-09-08 22:28:02 +00:00
isDragging = true;
}
else if (ended)
{
2025-09-12 03:38:36 +00:00
if (!isDragging)
m_ShowPanel.OnClick_Btn(4);
2025-09-08 22:28:02 +00:00
}
}
void ClampCameraPosition()
{
if (targetSprite == null) return;
Bounds spriteBounds = targetSprite.bounds;
2025-09-12 03:38:36 +00:00
float camHeight = Camera.main.orthographicSize * 2f;
float camWidth = camHeight * Camera.main.aspect;
2025-09-08 22:28:02 +00:00
2025-09-12 03:38:36 +00:00
Vector3 pos = Camera.main.transform.position;
2025-09-08 22:28:02 +00:00
// sprite가 카메라보다 작은 경우 → 카메라를 가운데 고정
if (camWidth >= spriteBounds.size.x || camHeight >= spriteBounds.size.y)
{
pos.x = spriteBounds.center.x;
pos.y = spriteBounds.center.y;
}
else
{
float minX = spriteBounds.min.x + camWidth / 2f;
float maxX = spriteBounds.max.x - camWidth / 2f;
float minY = spriteBounds.min.y + camHeight / 2f;
float maxY = spriteBounds.max.y - camHeight / 2f;
pos.x = Mathf.Clamp(pos.x, minX, maxX);
pos.y = Mathf.Clamp(pos.y, minY, maxY);
}
2025-09-12 03:38:36 +00:00
Camera.main.transform.position = pos;
2025-09-08 22:28:02 +00:00
}
}