Lua中如何实现Finger Touch进行复杂手势识别?

2026-04-01 18:492阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计811个文字,预计阅读时间需要4分钟。

Lua中如何实现Finger Touch进行复杂手势识别?

%E2%80%9C%E6%89%8B%E5%8A%BF%E8%AF%86%E5%88%AB%E6%8F%92%E4%BB%B6Finger+Touch%EF%BC%8C%E5%B8%B8%E7%94%A8%E4%BA%8E%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8C%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D%E4%B8%80%E4%B8%8BFinger+Touch%EF%BC%8C%E6%8C%BA%E8%AF%AD%E6%8F%90%E6%9C%813%E5%AD%97%E5%8F%AF%E3%80%82

Finger Touch 是一种处理手势识别的插件,在游戏开发中经常会用到,这里我先简单介绍一下Finger Touch(如有错误 欢迎指正) :


#region Event
// Fired when a finger begins touching the screen (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerDown;
// Fired when a finger stops touching the screen (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerUp;
// Fired when a finger taps the screen (this is when a finger begins and stops touching the screen within the ‘TapThreshold‘ time) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerTap;
// Fired when a finger swipes the screen (this is when a finger begins and stops touching the screen within the ‘TapThreshold‘ time, and also moves more than the ‘SwipeThreshold‘ distance) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerSwipe;
// Fired when a finger begins being held on the screen (this is when a finger has been set for longer than the ‘HeldThreshold‘ time) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerHeldDown;

// Fired when at least one finger taps the screen (int = Highest Finger Count)
public static System.Action<int> OnMultiTap;

// add: pinch/drag start/end event
public static System.Action OnPinchStart;
public static System.Action OnPinchEnd;
public static System.Action<CheapFinger> OnDragStart;
public static System.Action OnDragEnd;
#endregion

Finger touch 中 事件 有以上 按下 、抬起、敲击、扭转、长按、多指敲击、拖拽等

其次介绍一下:TouchScreen (自定义类)

TouchScreen 中 在 做 4件事

第一:监测手势触发点

第二: 将屏幕触发点转化为射线

第三:通过射线检测是否触发到某个obj

第四:将射线传递给Lua

简要代码 如下:

public void Tick(float deltaTime)
{
if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary))
{
_currentTouchTime += deltaTime;
if (_currentTouchTime > MIN_TIME)
{
_currentTouchTime = 0.0f;
CallLuaFunction(ON_TOUCH_MOVE);
}
}
#if UNITY_STANDALONE_WIN
else if(Input.GetMouseButton(0))
{
_currentTouchTime += deltaTime;
if (_currentTouchTime > MIN_TIME)
{
_currentTouchTime = 0.0f;
CallLuaFunction(ON_TOUCH_MOVE);
}
}
#endif
else
{
_currentTouchTime = 0.0f;
}
}

private void CallLuaFunction(string functionName)
{
if (string.IsNullOrEmpty(functionName))
{
return;
}
var ray = rayCamera.ScreenPointToRay(Input.mousePosition);


var hitInfo = GetRayCastHit ();
if (hitInfo != null && hitInfo.Length > 0)
{
if (!HitUI(Input.mousePosition))
{
LuaScriptMgr.Instance.CallLuaFunction("TouchAgent." + functionName, hitInfo);
}
}
}

CheapTouch 继承MonoBehavior ,CheapTouch 实现OnEnable、Update方法;

我们要在OnEnable 和 update 里做什么处理呢?这个就是需要和Lua 逻辑 关联起来

OnEnable 中 设置当前实例,也就是当CheapTouch 挂到游戏某个对象上时 实例化 该 脚本。

Update 中做二件事:

1.TouchScreen 实例传给Lua

2.更新各种操作和事件 如:

UpdateFingers();
UpdateMultiTap();
UpdateGestures();
UpdateEvents();

UpdateEvent();处理事件 包括 点击 拖拽 扭转 等等并调用Lua

Lua中如何实现Finger Touch进行复杂手势识别?

例如点击 事件:

private void RaiseFingerDownEvent(CheapFinger finger)
{
finger.SetCurUIGameObject();
fingerType = FINGER_DOWN;
CallLuaEventMethod(finger);

if (OnFingerDown != null) { OnFingerDown(finger); } }

本文共计811个文字,预计阅读时间需要4分钟。

Lua中如何实现Finger Touch进行复杂手势识别?

%E2%80%9C%E6%89%8B%E5%8A%BF%E8%AF%86%E5%88%AB%E6%8F%92%E4%BB%B6Finger+Touch%EF%BC%8C%E5%B8%B8%E7%94%A8%E4%BA%8E%E6%B8%B8%E6%88%8F%E5%BC%80%E5%8F%91%EF%BC%8C%E7%AE%80%E5%8D%95%E4%BB%8B%E7%BB%8D%E4%B8%80%E4%B8%8BFinger+Touch%EF%BC%8C%E6%8C%BA%E8%AF%AD%E6%8F%90%E6%9C%813%E5%AD%97%E5%8F%AF%E3%80%82

Finger Touch 是一种处理手势识别的插件,在游戏开发中经常会用到,这里我先简单介绍一下Finger Touch(如有错误 欢迎指正) :


#region Event
// Fired when a finger begins touching the screen (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerDown;
// Fired when a finger stops touching the screen (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerUp;
// Fired when a finger taps the screen (this is when a finger begins and stops touching the screen within the ‘TapThreshold‘ time) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerTap;
// Fired when a finger swipes the screen (this is when a finger begins and stops touching the screen within the ‘TapThreshold‘ time, and also moves more than the ‘SwipeThreshold‘ distance) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerSwipe;
// Fired when a finger begins being held on the screen (this is when a finger has been set for longer than the ‘HeldThreshold‘ time) (LeanFinger = The current finger)
public static System.Action<CheapFinger> OnFingerHeldDown;

// Fired when at least one finger taps the screen (int = Highest Finger Count)
public static System.Action<int> OnMultiTap;

// add: pinch/drag start/end event
public static System.Action OnPinchStart;
public static System.Action OnPinchEnd;
public static System.Action<CheapFinger> OnDragStart;
public static System.Action OnDragEnd;
#endregion

Finger touch 中 事件 有以上 按下 、抬起、敲击、扭转、长按、多指敲击、拖拽等

其次介绍一下:TouchScreen (自定义类)

TouchScreen 中 在 做 4件事

第一:监测手势触发点

第二: 将屏幕触发点转化为射线

第三:通过射线检测是否触发到某个obj

第四:将射线传递给Lua

简要代码 如下:

public void Tick(float deltaTime)
{
if (Input.touchCount > 0 && (Input.GetTouch(0).phase == TouchPhase.Moved || Input.GetTouch(0).phase == TouchPhase.Stationary))
{
_currentTouchTime += deltaTime;
if (_currentTouchTime > MIN_TIME)
{
_currentTouchTime = 0.0f;
CallLuaFunction(ON_TOUCH_MOVE);
}
}
#if UNITY_STANDALONE_WIN
else if(Input.GetMouseButton(0))
{
_currentTouchTime += deltaTime;
if (_currentTouchTime > MIN_TIME)
{
_currentTouchTime = 0.0f;
CallLuaFunction(ON_TOUCH_MOVE);
}
}
#endif
else
{
_currentTouchTime = 0.0f;
}
}

private void CallLuaFunction(string functionName)
{
if (string.IsNullOrEmpty(functionName))
{
return;
}
var ray = rayCamera.ScreenPointToRay(Input.mousePosition);


var hitInfo = GetRayCastHit ();
if (hitInfo != null && hitInfo.Length > 0)
{
if (!HitUI(Input.mousePosition))
{
LuaScriptMgr.Instance.CallLuaFunction("TouchAgent." + functionName, hitInfo);
}
}
}

CheapTouch 继承MonoBehavior ,CheapTouch 实现OnEnable、Update方法;

我们要在OnEnable 和 update 里做什么处理呢?这个就是需要和Lua 逻辑 关联起来

OnEnable 中 设置当前实例,也就是当CheapTouch 挂到游戏某个对象上时 实例化 该 脚本。

Update 中做二件事:

1.TouchScreen 实例传给Lua

2.更新各种操作和事件 如:

UpdateFingers();
UpdateMultiTap();
UpdateGestures();
UpdateEvents();

UpdateEvent();处理事件 包括 点击 拖拽 扭转 等等并调用Lua

Lua中如何实现Finger Touch进行复杂手势识别?

例如点击 事件:

private void RaiseFingerDownEvent(CheapFinger finger)
{
finger.SetCurUIGameObject();
fingerType = FINGER_DOWN;
CallLuaEventMethod(finger);

if (OnFingerDown != null) { OnFingerDown(finger); } }