适用于2d相机的拖动移动和缩放拉伸,其中有位置和大小的最大最小限制
文中用到的GameManager在这篇文章:https://blog.csdn.net/qq_39332021/article/details/106625199
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MapMove : MonoBehaviour
{
private Camera camera;
private Vector3 lastclickpos, lastmappos;
private Vector3 nextpos, v;
private Vector2 lastLeftHandPos, lastRightHandPos;
private float handDistancke,lastHandDistance;
private bool mapmove;
// Start is called before the first frame update
void Start()
{
camera = GetComponent<Camera>();
}
// Update is called once per frame
void Update()
{
if (GameManager.Main.GetDoubleClick())
{
lastLeftHandPos = Input.GetTouch(0).position;
lastRightHandPos = Input.GetTouch(1).position;
lastHandDistance = Vector2.Distance(lastLeftHandPos, lastRightHandPos);
}
if (GameManager.Main.GetDoubleClickUp())
{
if (Input.GetTouch(0).phase == TouchPhase.Ended && Input.GetTouch(1).phase == TouchPhase.Ended)
{
mapmove = false;
GameManager.Main.canchange = true;
}
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
lastclickpos = Input.GetTouch(1).position;
}
else
{
lastclickpos = Input.GetTouch(0).position;
}
lastmappos = transform.position;
}
if (GameManager.Main.GetDoubleClickAlways())
{
if (Input.GetTouch(0).phase == TouchPhase.Ended || Input.GetTouch(1).phase == TouchPhase.Ended)
{
goto one;
}
if (Input.GetTouch(0).phase == TouchPhase.Began || Input.GetTouch(1).phase == TouchPhase.Began)
{
goto one;
}
handDistancke = Vector2.Distance(Input.GetTouch(0).position, Input.GetTouch(1).position);
if (handDistancke < lastHandDistance && camera.orthographicSize < 10)
{
camera.orthographicSize -= (handDistancke - lastHandDistance) / 500.0f;
}
else if (handDistancke > lastHandDistance && camera.orthographicSize > 2)
{
camera.orthographicSize -= (handDistancke - lastHandDistance) / 500.0f;
}
lastLeftHandPos = Input.GetTouch(0).position;
lastRightHandPos = Input.GetTouch(1).position;
lastHandDistance = Vector2.Distance(lastLeftHandPos, lastRightHandPos);
return;
}
one:
if (GameManager.Main.GetClick())
{
mapmove = true;
lastclickpos = GameManager.Main.GetNumPos();
lastmappos = transform.position;
}
if (GameManager.Main.GetClickUp())
{
mapmove = false;
GameManager.Main.canchange = true;
}
if (mapmove)
{
if (Application.platform != RuntimePlatform.WindowsEditor)
{
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
return;
}
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
return;
}
}
nextpos = lastmappos + ((lastclickpos - GameManager.Main.GetNumPos()) / 1080.0f * camera.orthographicSize);
v = nextpos - transform.position;
if (Vector3.Distance(GameManager.Main.GetNumPos(), lastclickpos) > 1.0f)
{
GameManager.Main.canchange = false;
}
if (transform.position.x - camera.orthographicSize/2 < -10.0f && v.x < 0)
{
v.x = 0;
}
if (transform.position.x + camera.orthographicSize/2 > 10.0f && v.x > 0)
{
v.x = 0;
}
if (transform.position.y - camera.orthographicSize/2 < -10.0f && v.y < 0)
{
v.y = 0;
}
if (transform.position.y + camera.orthographicSize/2 > 10.0f && v.y > 0)
{
v.y = 0;
}
transform.position += v;
}
}
}