目录
1.UGUI实现大地图滚动与缩放
2.Sprite Renderer实现大地图滑动与缩放
1.UGUI实现大地图滚动与缩放
如图,大地图尺寸是超出屏幕1920*1080的,我们使用ScrollView实现大地图滑动
⚠️注意:camera使用 Perspective
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class UiScale : MonoBehaviour
{
public ScrollRect bgScroll;
private float lastDis = 0;
private float cameraDis = 0;
void Start()
{
}
private void OnEnable()
{
}
void Update()
{
if (Input.touchCount > 1)
{
bgScroll.horizontal = false;
bgScroll.vertical = false;
Touch t1 = Input.GetTouch(0);
Touch t2 = Input.GetTouch(1);
if (t2.phase == TouchPhase.Began)
{
lastDis = Vector2.Distance(t1.position, t2.position);
}
else
if (t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved)
{
float dis = Vector2.Distance(t1.position, t2.position);
if (Mathf.Abs(dis - lastDis) > 1)
{
if (dis != lastDis)
{
if (dis < lastDis)
{
if (cameraDis != 0)
{
cameraDis += 10;
cameraDis = Mathf.Clamp(cameraDis, -50, 0);
lastDis = dis;
}
}
else
{
if (cameraDis != -50)
{
cameraDis -= 10;
cameraDis = Mathf.Clamp(cameraDis, -50, 0);
lastDis = dis;
}
}
}
}
}
}
else
{
bgScroll.horizontal = true;
bgScroll.vertical = true;
}
if (Input.mouseScrollDelta.y != 0)
{
if (Input.mouseScrollDelta.y > 0)
{
cameraDis -= 10;
cameraDis = Mathf.Clamp(cameraDis, -50, 0);
}
else
{
cameraDis += 10;
cameraDis = Mathf.Clamp(cameraDis, -50, 0);
}
}
if (Input.GetKeyDown(KeyCode.Escape))
{
Application.Quit();
}
}
private void LateUpdate()
{
if (this.transform.position.z == cameraDis) { return; }
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, cameraDis);
}
}
2.Sprite Renderer实现大地图滑动与缩放
文章来源:https://www.toymoban.com/news/detail-677231.html
⚠️注意:Camera使用Orthographic文章来源地址https://www.toymoban.com/news/detail-677231.html
using UnityEngine;
public class SpriteBgContr : MonoBehaviour
{
public Camera _camera;
private Vector3 oriMousePos;
private Vector3 oriPos;
private int curDis = 3;
private float lastDis = 0;
void Start()
{
}
void Update()
{
}
private void OnMouseDown()
{
oriMousePos = Input.mousePosition;
oriPos = _camera.transform.position;
}
private void OnMouseDrag()
{
if (Input.touchCount > 1)
{
Touch t1 = Input.GetTouch(0);
Touch t2 = Input.GetTouch(1);
if (t2.phase == TouchPhase.Began)
{
lastDis = Vector2.Distance(t1.position, t2.position);
}
else
if (t1.phase == TouchPhase.Moved && t2.phase == TouchPhase.Moved)
{
float dis = Vector2.Distance(t1.position, t2.position);
if (Mathf.Abs(dis - lastDis) > 1)
{
if (dis != lastDis)
{
if (dis < lastDis)
{
if (curDis < 3)
{
curDis += 1;
curDis = Mathf.Clamp(curDis, 1, 3);
lastDis = dis;
}
}
else
{
if (curDis > 1)
{
curDis -= 1;
curDis = Mathf.Clamp(curDis, 1, 3);
lastDis = dis;
}
}
}
}
}
}
else
{
var offsetX = _camera.ScreenToWorldPoint(Input.mousePosition).x - _camera.ScreenToWorldPoint(oriMousePos).x;
var offsetY = _camera.ScreenToWorldPoint(Input.mousePosition).y - _camera.ScreenToWorldPoint(oriMousePos).y;
_camera.transform.position = new Vector3(Mathf.Clamp(oriPos.x - offsetX, -4, 4), Mathf.Clamp(_camera.transform.position.y - offsetY, -2, 2), -10);
}
}
private void LateUpdate()
{
if (_camera.fieldOfView == curDis) { return; }
_camera.orthographicSize = curDis;
}
}
到了这里,关于Unity 大地图滑动与缩放的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!