Unity-UGUI制作的小地图-可放大缩小视角,大地图小地图切换
1.效果图 文章最后也会附上 Demo下载地址 备注:资源是URP的,普通平台修改资源材质即可
-
缩小状态
-
放大状态
2.实现思路
首先新建一个相机来显示小地图,需要新建一个层级小地图来渲染,主相机不渲染,
新建一个rendertexture把相机画面输出到上面,新建Canvas->Rawimage,把rendertexture附上即可。
3.代码
// 这是相机视角缩放
btn_ZoomIn.onClick.AddListener(() =>
{
TargetSize -= 4f;
TargetSize = Mathf.Clamp(TargetSize, MinZoom, MaxZoom);
isZoom = true;
});
btn_ZoomOut.onClick.AddListener(() =>
{
TargetSize += 4f;
TargetSize = Mathf.Clamp(TargetSize, MinZoom, MaxZoom);
isZoom = true;
});
//这是切换大小地图
btn_Scale.onClick.AddListener(delegate
{
RectTransform rect = Tran_MiniMap.GetComponent<RectTransform>();
if (rect.rect.width == 436f) //小的时候
{
TargetSize = 10f;
rect.sizeDelta = new Vector2(436f * 3f, 436 * 3f);
rect.anchoredPosition = new Vector2((Screen.width / 2f), -(Screen.height / 2f));
}
else
{
TargetSize = 5f;
rect.sizeDelta = new Vector2(436f, 436f);
rect.anchoredPosition = new Vector2(260f, -240f);
}
isZoom = true;
});
//这是移动渐变效果
if (isZoom)
{
float target = MiniMapPersonCam.orthographicSize;
target = Mathf.Lerp(target, TargetSize, 0.1f);
target = Mathf.Clamp(target, MinZoom, MaxZoom);
MiniMapPersonCam.orthographicSize = target;
float c = Math.Abs(MiniMapPersonCam.orthographicSize - TargetSize);
if (c <= 0.001f)
isZoom = false;
}
4.项目源码
文章最后也会附上 Demo下载地址 备注:资源是URP的,普通平台修改资源材质即可