You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
213 lines
7.5 KiB
213 lines
7.5 KiB
using AX.MessageSystem; |
|
using System.Collections; |
|
using System.Collections.Generic; |
|
using UnityEngine; |
|
using System; |
|
using UnityEngine.UI; |
|
/// <summary> |
|
/// 小地图管理类 |
|
/// </summary> |
|
public class MiniMapCameraManager : MonoBehaviour |
|
{ |
|
//UIRoot |
|
private Canvas canvas; |
|
//小地图 |
|
private Transform miniMap; |
|
//UI相机 |
|
private Camera uiCamera; |
|
//主相机 |
|
private Camera mainCamera; |
|
//小地图相机 |
|
private Camera miniMapCamera; |
|
//主相机管理 |
|
private CameraManager cameraManager; |
|
//缩放状态 |
|
private ZoomState zoomState = ZoomState.Normal; |
|
//小地图UIRoot |
|
private Transform miniMapUIRoot; |
|
//缩放比例 |
|
private Vector3 screenScale = Vector3.one; |
|
//当前缩放比例 |
|
private Vector3 currentScale = Vector3.one; |
|
//缩放速度 |
|
public float zoomSpeed = 10; |
|
//相机最小值 |
|
public float zoomMin = 50; |
|
//相机最大值 |
|
public float zoomMax = 300; |
|
//当前相机大小 |
|
private float currentSize = 50; |
|
//相机视野边界最大值 |
|
public Vector2 PosToWorldMax = new Vector2(700f,450); |
|
//相机视野边界最小值 |
|
public Vector2 PosToWorldMin = new Vector2(-1700f,-1700); |
|
/// <summary> |
|
/// 初始化 |
|
/// </summary> |
|
void Start() |
|
{ |
|
canvas = GameObject.Find("Canvas").GetComponent<Canvas>(); |
|
miniMap = GameObject.Find("Minimap Image").transform; |
|
uiCamera = GameObject.Find("UICamera").GetComponent<Camera>(); |
|
miniMapUIRoot = GameObject.Find("MiniMapPanel").transform; |
|
mainCamera = Camera.main; |
|
miniMapCamera = GetComponent<Camera>(); |
|
cameraManager = mainCamera.GetComponent<CameraManager>(); |
|
SetMiniMapCameraOrthographicSize(currentSize); |
|
transform.position = new Vector3(cameraManager.target.transform.position.x, 900f, cameraManager.target.transform.position.z); |
|
InitScale(); |
|
} |
|
//初始化小地图的缩放比例 |
|
private void InitScale() |
|
{ |
|
//获取UI的实际尺寸 |
|
float width = (miniMapUIRoot as RectTransform).rect.width; |
|
float height = (miniMapUIRoot as RectTransform).rect.height; |
|
//获取UI同比例下屏幕的实际尺寸 |
|
float tempX = canvas.GetComponent<CanvasScaler>().referenceResolution.x * 0.5f; |
|
float tempY = canvas.GetComponent<CanvasScaler>().referenceResolution.y * 0.5f; |
|
//计算屏幕比例 |
|
screenScale = new Vector3(tempX / width, tempY / height, 1f); |
|
} |
|
private void OnEnable() |
|
{ |
|
//监听小地图缩小 |
|
MessageDispatcher.AddListener(MessageName.MiniMapShrink.ToString(), MiniMapShrink); |
|
//放大 |
|
MessageDispatcher.AddListener(MessageName.MiniMapEnlarge.ToString(), MiniMapEnlarge); |
|
//缩放 |
|
MessageDispatcher.AddListener(MessageName.MiniMapZoom.ToString(), MiniMapZoom); |
|
//右键点击 |
|
MessageDispatcher.AddListener(MessageName.MiniMapRightClick.ToString(), MiniMapRightClick); |
|
} |
|
|
|
private void OnDisable() |
|
{ |
|
MessageDispatcher.RemoveListener(MessageName.MiniMapShrink.ToString(), MiniMapShrink); |
|
MessageDispatcher.RemoveListener(MessageName.MiniMapEnlarge.ToString(), MiniMapEnlarge); |
|
MessageDispatcher.RemoveListener(MessageName.MiniMapZoom.ToString(), MiniMapZoom); |
|
MessageDispatcher.AddListener(MessageName.MiniMapRightClick.ToString(), MiniMapRightClick); |
|
} |
|
/// <summary> |
|
/// 在小地图上点击右键 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
private void MiniMapRightClick(IMessage obj) |
|
{ |
|
UpdateMiniMapPosition(); |
|
} |
|
/// <summary> |
|
/// 处理小地图缩放消息 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
private void MiniMapZoom(IMessage obj) |
|
{ |
|
switch (zoomState) |
|
{ |
|
case ZoomState.Normal: |
|
miniMapUIRoot.localScale = screenScale; |
|
zoomState = ZoomState.Max; |
|
currentScale = screenScale; |
|
break; |
|
case ZoomState.Max: |
|
miniMapUIRoot.localScale = Vector3.one; |
|
zoomState = ZoomState.Normal; |
|
currentScale = Vector3.one; |
|
break; |
|
case ZoomState.Min: |
|
break; |
|
} |
|
} |
|
/// <summary> |
|
/// 处理小地图放大消息 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
private void MiniMapEnlarge(IMessage obj) |
|
{ |
|
currentSize = miniMapCamera.orthographicSize + zoomSpeed; |
|
SetMiniMapCameraOrthographicSize(currentSize); |
|
} |
|
/// <summary> |
|
/// 处理小地图缩小消息 |
|
/// </summary> |
|
/// <param name="obj"></param> |
|
private void MiniMapShrink(IMessage obj) |
|
{ |
|
currentSize = miniMapCamera.orthographicSize - zoomSpeed; |
|
SetMiniMapCameraOrthographicSize(currentSize); |
|
} |
|
/// <summary> |
|
/// 修改小地图相机大小 |
|
/// </summary> |
|
/// <param name="value"></param> |
|
void SetMiniMapCameraOrthographicSize(float value) |
|
{ |
|
miniMapCamera.orthographicSize = Mathf.Clamp(value, zoomMin, zoomMax); |
|
} |
|
/// <summary> |
|
/// 更新小地图坐标 |
|
/// </summary> |
|
void UpdateMiniMapPosition() |
|
{ |
|
//获取鼠标所谓屏幕像素坐标 |
|
Vector2 mousePos; |
|
RectTransformUtility.ScreenPointToLocalPointInRectangle( |
|
canvas.transform as RectTransform, |
|
Input.mousePosition, |
|
canvas.worldCamera, |
|
out mousePos); |
|
//获取小地图所在屏幕像素坐标 |
|
Vector2 miniMapPos; |
|
RectTransformUtility.ScreenPointToLocalPointInRectangle( |
|
canvas.transform as RectTransform, |
|
uiCamera.WorldToScreenPoint(miniMap.position), |
|
canvas.worldCamera, |
|
out miniMapPos); |
|
//计算点击位置小地图的偏移值 |
|
Vector2 miniMapOffset = mousePos - miniMapPos; |
|
Vector3 minimapCameraPos = transform.position; |
|
//Debug.Log(string.Format("小地图中心点与鼠标点击位置偏移:{0}", miniMapOffset)); |
|
float XRate = -miniMapOffset.x / ((miniMap as RectTransform).rect.width * currentScale.x); |
|
float YRate = -miniMapOffset.y / ((miniMap as RectTransform).rect.height * currentScale.y); |
|
Vector3 worldOffset = new Vector3( |
|
XRate * miniMapCamera.orthographicSize * 2, |
|
0, |
|
YRate * miniMapCamera.orthographicSize * 2); |
|
Vector3 clickPosToWorld = minimapCameraPos + worldOffset; |
|
|
|
//限制相机显示位置 |
|
if (clickPosToWorld.x > PosToWorldMax.x) |
|
{ |
|
clickPosToWorld.x = PosToWorldMax.x; |
|
} |
|
if (clickPosToWorld.z > PosToWorldMax.y) |
|
{ |
|
clickPosToWorld.z = PosToWorldMax.y; |
|
} |
|
if(clickPosToWorld.x<PosToWorldMin.x) |
|
{ |
|
clickPosToWorld.x = PosToWorldMin.x; |
|
} |
|
if (clickPosToWorld.z<PosToWorldMin.y) |
|
{ |
|
clickPosToWorld.z = PosToWorldMin.y; |
|
} |
|
//cameraManager.target.transform.position = new Vector3( |
|
// clickPosToWorld.x, |
|
// cameraManager.target.transform.position.y, |
|
// clickPosToWorld.z+6); |
|
cameraManager.SetCameraView(new Vector3( |
|
clickPosToWorld.x, |
|
cameraManager.target.transform.position.y, |
|
clickPosToWorld.z + 6), 100, 0, 0); |
|
//通知主相机复位 |
|
MessageDispatcher.SendMessage(MessageName.RestCamera.ToString()); |
|
transform.position = new Vector3(clickPosToWorld.x, 900, clickPosToWorld.z); |
|
} |
|
public enum ZoomState |
|
{ |
|
Normal, |
|
Max, |
|
Min |
|
} |
|
}
|
|
|