using UnityEngine; public class CameraDragMove : MonoBehaviour { public ShowPanel m_ShowPanel; public Camera cam; // 이동시킬 카메라 public SpriteRenderer targetSprite; // 영역 기준이 되는 스프라이트 public GameObject go_btnbg; public float dragSpeed = 1f; private Vector3 lastTouchPos; Vector3 lastcamPos; private bool isDragging = false; void Reset() { cam = Camera.main; } 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; lastcamPos = cam.transform.position; isDragging = false; } else if (moved) { Vector3 deltaWorld = cam.ScreenToWorldPoint(position) - cam.ScreenToWorldPoint(lastTouchPos); deltaWorld.z = 0f; cam.transform.position -= deltaWorld * dragSpeed; ClampCameraPosition(); lastTouchPos = position; if (lastcamPos != cam.transform.position || Mathf.Abs(deltaWorld.x) >= 0.01f || Mathf.Abs(deltaWorld.y) >= 0.01f) isDragging = true; } else if (ended) { if (!isDragging) m_ShowPanel.OnClick_Btn(4); } } void ClampCameraPosition() { if (targetSprite == null) return; Bounds spriteBounds = targetSprite.bounds; float camHeight = cam.orthographicSize * 2f; float camWidth = camHeight * cam.aspect; Vector3 pos = cam.transform.position; // 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); } cam.transform.position = pos; } }