环境:
- unity2021.3.14f1c1
- Camera类型Orthographic
- TileMap为XY方向摆放
功能:
- 单点触摸或鼠标操作移动Camera。
- 使用BoxCollider2D控制地图范围。
- 鼠标滚轮或多点触摸控制缩放。
- 缩放维持中心点在屏幕的固定位置。
代码:
using System;
using UnityEngine;
public class TileMapCameraController : MonoBehaviour
{
[SerializeField]
GameObject targetMap;
[SerializeField]
Camera targetCam;
private bool enableControl = true;
private float camInitSize = 20;
private float camMinSize = 5;
private float camMaxSize = 30;
private float camSizeScrollWheelV = 10;
private Vector2 firstTouchPos;
private Vector2 cameraFirstPos;
private bool isMoving = false;
private bool isBegan = false;
private bool isOnScale = false;
private float camOriginZ = 0;
private Vector3 mapMin;
private Vector3 mapMax;
private Vector3 camNewPos = new Vector3();
// 双触点初始距离
private float baseDis;
// 双触点初始中心
private Vector2 baseMidScreen;
// 双触点初始中心
private Vector3 baseMidWorld;
// 双触点初始镜头size
private float baseSize;
private float baseScale = 1;
void Awake()
{
Init();
}
private void Init()
{
if (targetMap)
{
var bounds = targetMap.GetComponent<BoxCollider2D>().bounds;
mapMax = bounds.max;
mapMin = bounds.min;
}
if (targetCam)
{
targetCam.orthographicSize = camInitSize;
c
标签:Vector3,缩放,TileMap,float,bounds,private,Unity,Camera,bool
From: https://blog.csdn.net/GrimRaider/article/details/143919354