using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
/*************************************************************
* 场景相关
*************************************************************/
public class TileCamera : MonoBehaviour
{
public static bool isMove = false;
private static Touch oldTouch1; //上次触摸点1(手指1)
private static Touch oldTouch2; //上次触摸点2(手指2)
/// <summary>
/// 相机平移
/// </summary>
public static void MoveView(Camera Camera)
{
if (Input.touchCount == 1)
{
var deltaposition = Input.GetTouch(0).deltaPosition;
Camera.transform.Translate(-deltaposition.x * Time.deltaTime, -deltaposition.y * Time.deltaTime, 0, Space.World);
Camera.transform.position = new Vector3(Mathf.Clamp(Camera.transform.position.x, -20f, 20f), Mathf.Clamp(Camera.transform.position.y, -1f, 20f), Camera.transform.position.z);
}
}
/// <summary>
/// 处理视野的拉近和拉远效果
/// </summary>
public static void ScrollView()
{
//没有触摸,就是触摸点为0
if (Input.touchCount <= 0)
{
return;
}
if (Input.touchCount == 2)
{
//多点触摸, 放大缩小
Touch newTouch1 = Input.GetTouch(0);
Touch newTouch2 = Input.GetTouch(1);
//第2点刚开始接触屏幕, 只记录,不做处理
if (newTouch2.phase == TouchPhase.Began)
{
oldTouch2 = newTouch2;
oldTouch1 = newTouch1;
return;
}
//计算老的两点距离和新的两点间距离,变大要放大模型,变小要缩放模型
float oldDistance = Vector2.Distance(oldTouch1.position, oldTouch2.position);
float newDistance = Vector2.Distance(newTouch1.position, newTouch2.position);
//两个距离之差,为正表示放大手势, 为负表示缩小手势
float offset = newDistance - oldDistance;
//放大因子, 一个像素按 0.01倍来算(100可调整)
float scaleFactor = offset / 100f;
//在什么情况下进行缩放
Camera.main.orthographicSize -= scaleFactor;
Camera.main.orthographicSize = Mathf.Clamp(Camera.main.orthographicSize, 2f, 10f);
//记住最新的触摸点,下次使用
oldTouch1 = newTouch1;
oldTouch2 = newTouch2;
}
}
}