用了几个手势插件,发现都不能完美的实现类似IOS的Drag&Swipe效果,
自己实现了一个
实现代码
using UnityEngine;
public class DragTween : MonoBehaviour
{
public Transform target;
public bool igoneTimeScale;
private Vector2 axis; //lerp方向量
private Vector3 fristPressPos; //第一次按下的坐标
private Vector2 toSpeed; //鼠标移动的量
private Vector3 lastPos; //上次的坐标
private Vector3 lastDelta; //上次抬起的坐标
public void Update()
{
MouseInput();
}
private void MouseInput()
{
if (Input.GetMouseButtonDown(0))
{
fristPressPos = lastPos= Input.mousePosition;
axis = Vector2.zero;
OnPress(true, fristPressPos);
}
if(Input.GetMouseButtonUp(0))
{
OnPress(false, Input.mousePosition);
}
toSpeed= Vector2.zero;
if(Input.GetMouseButton(0))
{
toSpeed.x = Input.GetAxis("Mouse X");
toSpeed.y = Input.GetAxis("Mouse Y");
lastDelta= Input.mousePosition - lastPos;
if(lastDelta != Vector3.zero)
{
OnDrag(lastDelta, Input.mousePosition);
lastPos = Input.mousePosition;
}
}
else
{
if(axis!=Vector2.zero)
OnSwipe(axis, lastDelta);
}
var time = igoneTimeScale ? Time.unscaledDeltaTime : Time.deltaTime;
axis=Vector2.Lerp(axis, toSpeed / time, 0.5f * time);
}
private void OnPress(bool isPress,Vector2 pressPos)
{
}
///
/// 当滑动的时候
///
/// 这一次鼠标位置减去上一帧鼠标位置的向量
/// 当前鼠标坐标
private void OnDrag(Vector2 delta,Vector2 currentPos)
{
float angle = delta.magnitude;
Vector3 axis = Vector3.Cross(delta, transform.forward);
target.RotateAround(target.position, axis, angle / Mathf.PI);
}
private void OnSwipe(Vector2 force,Vector2 lastDelta)
{
float f = force.magnitude;
float angle = lastDelta.magnitude;
Vector3 axis = Vector3.Cross(lastDelta, transform.forward);
target.RotateAround(target.position, axis, angle * f / Mathf.PI);
}
}