using System.Collections; using System.Collections.Generic; using UnityEngine; public partial class MapManager : MonoBehaviour { private float moveSpeed = 10; private float mapRealWidth; private float mapRealHeight; private Vector3 lastMousePosition; private void UpdateCamera() { if (Input.GetAxis("Mouse ScrollWheel") < 0) { Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize + Time.deltaTime * moveSpeed * Camera.main.orthographicSize, 5, 40); } else if (Input.GetAxis("Mouse ScrollWheel") > 0) { Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize - Time.deltaTime * moveSpeed * Camera.main.orthographicSize, 5, 40); } // 鼠标中键拖动平移 if (Input.GetMouseButtonDown(2)) // 鼠标中键按下 { lastMousePosition = Input.mousePosition; } else if (Input.GetMouseButton(2)) // 鼠标中键按住 { Vector3 delta = Input.mousePosition - lastMousePosition; Vector3 move = new Vector3(-delta.x, -delta.y, 0) * Time.deltaTime * Camera.main.orthographicSize; // 基于缩放调整速度 Vector3 newPosition = Camera.main.transform.position + move; // 限制相机移动范围(基于地图边界) newPosition.x = Mathf.Clamp(newPosition.x, 0, mapRealWidth); newPosition.y = Mathf.Clamp(newPosition.y, 0, mapRealHeight); Camera.main.transform.position = newPosition; lastMousePosition = Input.mousePosition; // 更新鼠标位置 } } public void ReseCamera(float x, float y) { Camera.main.transform.position = new Vector3(x / 2, y / 2, -10); Camera.main.orthographicSize = 5; mapRealWidth = x; mapRealHeight = y; } public void MoveToCamera(float x, float y) { Vector3 newPosition; newPosition.x = Mathf.Clamp(x, 0, mapRealWidth); newPosition.y = Mathf.Clamp(y, 0, mapRealHeight); Camera.main.transform.position = new Vector3(x, y, -10); } public Vector2Int GetCameraPos() { Vector2Int pos = new Vector2Int(); pos.x = (int)Camera.main.transform.position.x; pos.y = (int)Camera.main.transform.position.y; pos.x = Mathf.Clamp(pos.x, 0, (int)mapRealWidth); pos.y = Mathf.Clamp(pos.y, 0, (int)mapRealHeight); pos.x = (int)(pos.x / map.sideWidth); pos.y = (int)(pos.y / map.sideHeight); return pos; } }