放大缩小 移动 Tilemap
使用新版的 Lean
简洁一点是:
// 准备好我们要用的手指
var fingers = Use.GetFingers();
// 计算缩放,并确保它是有效的
var pinchScale = LeanGesture.GetPinchScale(fingers);
// 在什么情况下进行缩放
ScreenDepth.Camera.orthographicSize += (1 - pinchScale) * 3;
ScreenDepth.Camera.orthographicSize = Mathf.Clamp(ScreenDepth.Camera.orthographicSize, 1f, 3f);
具体一点是:
using UnityEngine;
using Lean.Common;
using FSA = UnityEngine.Serialization.FormerlySerializedAsAttribute;
using System.Collections.Generic;
namespace Lean.Touch
{
//[HelpURL(LeanTouch.HelpUrlPrefix + "LeanDragCamera")]
//[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Drag Camera")]
public class MapCamera : MonoBehaviour
{
/// <summary>The method used to find fingers to use with this component. See LeanFingerFilter documentation for more information.</summary>
public LeanFingerFilter Use = new LeanFingerFilter(true);
/// <summary>The method used to find world coordinates from a finger. See LeanScreenDepth documentation for more information.</summary>
public LeanScreenDepth ScreenDepth = new LeanScreenDepth(LeanScreenDepth.ConversionType.DepthIntercept);
/// <summary>The movement speed will be multiplied by this.
/// -1 = Inverted Controls.</summary>
[Tooltip("The movement speed will be multiplied by this.\n\n-1 = Inverted Controls.")]
public float Sensitivity = 1.0f;
/// <summary>If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.
/// -1 = Instantly change.
/// 1 = Slowly change.
/// 10 = Quickly change.</summary>
[Tooltip("If you want this component to change smoothly over time, then this allows you to control how quick the changes reach their target value.\n\n-1 = Instantly change.\n\n1 = Slowly change.\n\n10 = Quickly change.")]
[FSA("Dampening")] public float Damping = -1.0f;
/// <summary>This allows you to control how much momenum is retained when the dragging fingers are all released.
/// NOTE: This requires <b>Dampening</b> to be above 0.</summary>
[Tooltip("This allows you to control how much momenum is retained when the dragging fingers are all released.\n\nNOTE: This requires <b>Dampening</b> to be above 0.")]
[Range(0.0f, 1.0f)]
public float Inertia;
[HideInInspector]
[SerializeField]
private Vector3 remainingDelta;
/// <summary>This method moves the current GameObject to the center point of all selected objects.</summary>
[ContextMenu("Move To Selection")]
public virtual void MoveToSelection()
{
var center = default(Vector3);
var count = 0;
foreach (var selectable in LeanSelectable.Instances)
{
if (selectable.IsSelected == true)
{
center += selectable.transform.position;
count += 1;
}
}
if (count > 0)
{
var oldPosition = transform.localPosition;
transform.position = center / count;
remainingDelta += transform.localPosition - oldPosition;
transform.localPosition = oldPosition;
}
}
/// <summary>If you've set Use to ManuallyAddedFingers, then you can call this method to manually add a finger.</summary>
public void AddFinger(LeanFinger finger)
{
Use.AddFinger(finger);
}
/// <summary>If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove a finger.</summary>
public void RemoveFinger(LeanFinger finger)
{
Use.RemoveFinger(finger);
}
/// <summary>If you've set Use to ManuallyAddedFingers, then you can call this method to manually remove all fingers.</summary>
public void RemoveAllFingers()
{
Use.RemoveAllFingers();
}
#if UNITY_EDITOR
protected virtual void Reset()
{
Use.UpdateRequiredSelectable(gameObject);
}
#endif
protected virtual void Awake()
{
Use.UpdateRequiredSelectable(gameObject);
}
protected virtual void LateUpdate()
{
//准备好我们要用的手指
var fingers = Use.GetFingers();
//获取所有手指的最后和当前屏幕点
var lastScreenPoint = LeanGesture.GetLastScreenCenter(fingers);
var screenPoint = LeanGesture.GetScreenCenter(fingers);
// 计算缩放,并确保它是有效的
var pinchScale = LeanGesture.GetPinchScale(fingers);
// 得到他们转换后的世界三角洲
var worldDelta = ScreenDepth.ConvertDelta(lastScreenPoint, screenPoint, gameObject);
// 存储当前位置
var oldPosition = transform.localPosition;
// 以世界三角洲为基础平移镜头
transform.position -= worldDelta * Sensitivity;
// Add to remainingDelta
remainingDelta += transform.localPosition - oldPosition;
// Get t value
var factor = LeanHelper.GetDampenFactor(Damping, Time.deltaTime);
// 抑制剩余
var newRemainingDelta = Vector3.Lerp(remainingDelta, Vector3.zero, factor);
// 通过增量改变这个位置
transform.localPosition = oldPosition + remainingDelta - newRemainingDelta;
if (fingers.Count == 0 && Inertia > 0.0f && Damping > 0.0f)
{
newRemainingDelta = Vector3.Lerp(newRemainingDelta, remainingDelta, Inertia);
}
// 用衰减值更新剩余的增量值
remainingDelta = newRemainingDelta;
// 在什么情况下进行缩放
ScreenDepth.Camera.orthographicSize += (1 - pinchScale) * 3;
ScreenDepth.Camera.orthographicSize = Mathf.Clamp(ScreenDepth.Camera.orthographicSize, 1f, 3f);
}
}
}
使用老的 Lean
using FairyGUI;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Lean.Touch
{
public class TilemapCamera : MonoBehaviour
{
public Camera tCamera;
public static bool isCanMove = true;
public float Sensitivity = 1.0f;
public LeanFingerFilter Use = new LeanFingerFilter(true);
private static bool isCanTwinkle = false; //是否可以闪烁
// Start is called before the first frame update
protected virtual void OnEnable()
{
tCamera = GameObject.Find("TilemapCamera").GetComponent<Camera>();
// Hook into the events we need
LeanTouch.OnFingerDown += HandleFingerDown;
LeanTouch.OnFingerUpdate += HandleFingerUpdate;
LeanTouch.OnFingerUp += HandleFingerUp;
LeanTouch.OnFingerTap += HandleFingerTap;
//LeanTouch.OnFingerSwipe += HandleFingerSwipe;
LeanTouch.OnGesture += HandleGesture;
}
protected virtual void OnDisable()
{
// Unhook the events
//LeanTouch.OnFingerDown -= HandleFingerDown;
//LeanTouch.OnFingerUpdate -= HandleFingerUpdate;
//LeanTouch.OnFingerUp -= HandleFingerUp;
LeanTouch.OnFingerTap -= HandleFingerTap;
//LeanTouch.OnFingerSwipe -= HandleFingerSwipe;
LeanTouch.OnGesture -= HandleGesture;
}
// 地图放大缩小
public void ScrollView(float scale)
{
float scaleFactor = scale;
//在什么情况下进行缩放
tCamera.orthographicSize += (1 - scaleFactor)*3;
tCamera.orthographicSize = Mathf.Clamp(tCamera.orthographicSize, 10f, 20f);
//Debug.LogError("**************" + tCamera.orthographicSize);
}
private void Update()
{
if (!Stage.isTouchOnUI)
{
var fingers = Use.GetFingers();
var screenDelta = LeanGesture.GetScreenDelta(fingers);
if (screenDelta != Vector2.zero)
{
Translate(screenDelta);
}
}
}
// 移动
private void Translate(Vector2 screenDelta)
{
// Make sure the camera exists
var camera = LeanTouch.GetCamera(tCamera, gameObject);
if (camera != null)
{
//if (Input.GetTouch(0).phase == TouchPhase.Moved && TileCamera.isCanMove == true) {
if ( TileCamera.isCanMove == true)
{
//{
// Screen position of the transform
var screenPoint = camera.WorldToScreenPoint(transform.position);
// Add the deltaPosition
screenPoint -= (Vector3)screenDelta * Sensitivity;
transform.position = camera.ScreenToWorldPoint(screenPoint);
if(GameController.TileManage.go==null)
{
return;
}
//if (GameController.TileManage.go.transform.position.x < -39.5f || GameController.TileManage.go.transform.position.x > 39.5f || GameController.TileManage.go.transform.position.y < 6.5f || GameController.TileManage.go.transform.position.y > 37.5f)
if (transform.position.x < -39.5f || transform.position.x > 39.5f || transform.position.y < 6.5f || transform.position.y > 37.5f)
{
// 红色动效
Twinkle();
isCanTwinkle = true;
}
else
{
isCanTwinkle = false;
}
//调起白云
ShowCloud();
transform.position = new Vector3(Mathf.Clamp(transform.position.x, -40f, 40f), Mathf.Clamp(transform.position.y, 6f, 38f), transform.position.z);
//// 限制移动距离
//Debug.Log("screenPoint" + transform.position);
}
}
else
{
Debug.LogError("Failed to find camera. Either tag your camera as MainCamera, or set one in this component.", this);
}
}
private float Alpha1 = 0;
private float Alpha2 = 0;
public void ShowCloud()
{
// 左上角
if (transform.position.x < -36.32f && transform.position.y > 32.69)
{
// 出现白云
// -37.41 35.44 白云完全显示
//Vector2 Alpha1 = Vector2.Lerp(new Vector2(transform.position.x, transform.position.y),new Vector2(-37.41f,35.44f), transform.position.x / -37.41f);
//Vector2 Alpha2 = Vector2.Lerp(new Vector2(transform.position.x, transform.position.y), new Vector2(-37.41f, 35.44f), transform.position.y / -37.41f);
Alpha1 = transform.position.x / -37.41f;
Alpha2 = transform.position.y / 35.44f;
Alpha1 = Alpha1 > 1 ? 1 : Alpha1;
Alpha2 = Alpha2 > 1 ? 1 : Alpha2;
}
else
{
// 隐藏白云
Alpha1 = 0;
Alpha2 = 0;
}
//if (Alpha1 < Alpha2)
//{
// GameZootoPia._Instance.SetLeftCloudAlphaValue(Alpha1);
//}
//else
//{
// GameZootoPia._Instance.SetLeftCloudAlphaValue(Alpha2);
//}
//右上角
if (transform.position.x > 29.3 && transform.position.y > 30)
{
// 出现白云
// 35.62 32.51 白云完全显示
Alpha1 = transform.position.x / 35.62f;
Alpha2 = transform.position.y / 32.51f;
Alpha1 = Alpha1 > 1 ? 1 : Alpha1;
Alpha2 = Alpha2 > 1 ? 1 : Alpha2;
}
else
{
// 隐藏白云
Alpha1 = 0;
Alpha2 = 0;
}
//if (Alpha1 < Alpha2)
//{
// GameZootoPia._Instance.SetRightCloudAlphaValue(Alpha1);
//}
//else
//{
// GameZootoPia._Instance.SetRightCloudAlphaValue(Alpha2);
//}
//Debug.Log(Alpha1 + " " + Alpha2);
}
public static void Twinkle()
{
if (isCanTwinkle == false)
{
GameController.EventManager.CommonEvent.Dispatch(EventId.BoundaryDetermination, isCanTwinkle);
}
}
public void HandleFingerDown(LeanFinger finger)
{
if (!Stage.isTouchOnUI)
{
TileEvents.MouseDown();
}
}
public void HandleFingerUpdate(LeanFinger finger)
{
if (!Stage.isTouchOnUI)
{
TileEvents.MouseMove();
}
}
public void HandleFingerUp(LeanFinger finger)
{
if (!Stage.isTouchOnUI)
{
TileEvents.MouseUp();
}
}
public void HandleFingerTap(LeanFinger finger)
{
}
public void HandleFingerSwipe(LeanFinger finger)
{
}
public void HandleGesture(List<LeanFinger> fingers)
{
//Debug.Log("Gesture with " + fingers.Count + " finger(s)");
//Debug.Log(" pinch scale: " + LeanGesture.GetPinchScale(fingers));
//Debug.Log(" twist degrees: " + LeanGesture.GetTwistDegrees(fingers));
//Debug.Log(" twist radians: " + LeanGesture.GetTwistRadians(fingers));
//Debug.Log(" screen delta: " + LeanGesture.GetScreenDelta(fingers));
float scale = LeanGesture.GetPinchScale(fingers);
ScrollView(scale);
}
}
}