官方API http://carloswilkes.com/Documentation/LeanTouch
安装LeanTouch,生成LeanTouch组件
屏幕点击演示
当点击屏幕时,通过LeanFingerDown脚本的OnWorld回调,参数Vector3,点击获得世界的位置。
当鼠标点击抬起时触发,同上
滑动控制
滑动的方向
if (delta.sqrMagnitude > 0.0f)
{
Angle = Mathf.Atan2(delta.x, delta.y) * Mathf.Rad2Deg;
}
public RectTransform arrow;
public void OnDelta(Vector2 pos)
{
if (pos.sqrMagnitude > 0.0f)
{
float Angle = Mathf.Atan2(pos.x, pos.y) * Mathf.Rad2Deg;
arrow.DOLocalRotateQuaternion(Quaternion.Euler(0.0f, 0.0f, -Angle), 0.5f);
}
}
另外,注意LeanTouch的SwipeThreshold=50,LeanFingerSwipe里的IgnoreStartedOverGui与Ignore Is Over Gui勾去掉;
4个方向的识别,N=(0,90),E=(90,90),S=(180,90),W=(270,90)
8个方向的识别,N=(0,45),NE=(45,45),E=(90,45),SE=(135,45),S=(180,45),SW=(225,45),W=(270,45),NW=(315,45)
Tap轻触
生成一个预制体
UI遮挡,不能触发Tap
双击Tap触发
长按的解决方案
手势控制,拖动,放大,旋转
手势控制,拖动,放大,旋转;同上,勾选了Relative,对应世界坐标与相对坐标的区别
手势控制,拖动,放大,旋转;同上,对应2D图片
手势控制,拖动,放大,旋转;勾选了Relative,对应世界坐标与相对坐标的区别;同上,对应2D图片
手势控制,拖动,放大,旋转;同上,对应UI图片
手势控制,拖动,放大,旋转;勾选了Relative,对应世界坐标与相对坐标的区别;同上,对应UI图片
选择3D物体,点击其他位置,取消选择
同上的功能外,在需要控制的对象里,加入拖动,放大,旋转功能,由于有LeanSelectabel的脚本,程序自动设置为,若没有选中,则3个动作的脚本不起作用。
同上,勾选了Relative
同上,2D的选择
同上
使用LeanDestroy,消失物体
UI的选择
同上
按住为选择,放开既为不选择
同上
在对象物体上,勾选Isolate Selecting Fingers,如果2个指头不同时触碰在一个物体上操作,是无效的操作
滑动手指,出现拖尾的线条效果
拖动镜头
似乎相同
用脚本来实现动作
namespace Lean.Touch
{
/// This component will hook into every LeanTouch event, and spam the console with the information.
[HelpURL(LeanTouch.HelpUrlPrefix + "LeanTouchEvents")]
[AddComponentMenu(LeanTouch.ComponentPathPrefix + "Touch Events")]
public class LeanTouchEvents : MonoBehaviour
{
protected virtual void OnEnable()
{
// 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 HandleFingerDown(LeanFinger finger)
{
Debug.Log("Finger " + finger.Index + " began touching the screen");
}
public void HandleFingerUpdate(LeanFinger finger)
{
Debug.Log("Finger " + finger.Index + " is still touching the screen");
}
public void HandleFingerUp(LeanFinger finger)
{
Debug.Log("Finger " + finger.Index + " finished touching the screen");
}
public void HandleFingerTap(LeanFinger finger)
{
Debug.Log("Finger " + finger.Index + " tapped the screen");
}
public void HandleFingerSwipe(LeanFinger finger)
{
Debug.Log("Finger " + finger.Index + " swiped the screen");
}
public void HandleGesture(List 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));
}
}
}
控制盒子转动
通过Axis里设置X,Y,Z的设置,来开启或屏蔽相关轴的控制
0,不响应;-1 / 1,响应,但是方向不同
使用LeanFingerSwipe,LeanSelect,LeanSelectable来实现
public void OnSelect(Lean.Touch.LeanFinger finger)
{
if (finger.SwipeScreenDelta.x >= 0)
{
// 向右
print("向右");
}
else
{
// 向左
print("向左");
}
}
通过这4步,即可实现,在一个图片上左右滑动,产生作用;而在外部滑动则不起作用
使用到LeanSelect,LeanSelectable,LeanFingerTap,LeanFingerHeld
public void OnSelect(Lean.Touch.LeanFinger finger)
{
if (finger.Tap) {
print("tap");
}
if (finger.Old) {
print("old");
}
}
完成以上4步,即可实现一个即可轻触又可长按的按钮
完成以上几步,即可修改为单个UI拖动