如何在Unity中实现iOS设备的滑动手势检测?
Unity iOS滑动手势检测实现方案
一、调用iOS原生手势识别功能
可以通过Unity的原生插件机制,直接调用iOS系统的UIGestureRecognizer实现系统级滑动检测,步骤如下:
- 编写iOS原生插件代码(Objective-C)
创建.mm文件(如iOSGesturePlugin.mm),放在Unity项目的Assets/Plugins/iOS目录下:
#import <UIKit/UIKit.h> @interface iOSGestureHandler : NSObject @property (nonatomic, strong) UISwipeGestureRecognizer *leftSwipe; @property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipe; @property (nonatomic, strong) UISwipeGestureRecognizer *upSwipe; @property (nonatomic, strong) UISwipeGestureRecognizer *downSwipe; @end @implementation iOSGestureHandler + (instancetype)sharedInstance { static iOSGestureHandler *instance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ instance = [[iOSGestureHandler alloc] init]; [instance setupGestures]; }); return instance; } - (void)setupGestures { UIWindow *window = [[UIApplication sharedApplication] keyWindow]; _leftSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleLeftSwipe:)]; _leftSwipe.direction = UISwipeGestureRecognizerDirectionLeft; [window addGestureRecognizer:_leftSwipe]; _rightSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleRightSwipe:)]; _rightSwipe.direction = UISwipeGestureRecognizerDirectionRight; [window addGestureRecognizer:_rightSwipe]; _upSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleUpSwipe:)]; _upSwipe.direction = UISwipeGestureRecognizerDirectionUp; [window addGestureRecognizer:_upSwipe]; _downSwipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleDownSwipe:)]; _downSwipe.direction = UISwipeGestureRecognizerDirectionDown; [window addGestureRecognizer:_downSwipe]; } - (void)handleLeftSwipe:(UISwipeGestureRecognizer *)gesture { UnitySendMessage("GestureReceiver", "OnLeftSwipe", ""); } - (void)handleRightSwipe:(UISwipeGestureRecognizer *)gesture { UnitySendMessage("GestureReceiver", "OnRightSwipe", ""); } - (void)handleUpSwipe:(UISwipeGestureRecognizer *)gesture { UnitySendMessage("GestureReceiver", "OnUpSwipe", ""); } - (void)handleDownSwipe:(UISwipeGestureRecognizer *)gesture { UnitySendMessage("GestureReceiver", "OnDownSwipe", ""); } @end extern "C" { void _InitiOSGestures() { [iOSGestureHandler sharedInstance]; } }
- 编写C#调用脚本
创建iOSGestureWrapper.cs,用来初始化插件:
using UnityEngine; public class iOSGestureWrapper : MonoBehaviour { [System.Runtime.InteropServices.DllImport("__Internal")] private static extern void _InitiOSGestures(); void Start() { #if UNITY_IOS && !UNITY_EDITOR _InitiOSGestures(); #endif } }
- 创建回调接收脚本
创建GestureReceiver.cs,挂载到名为GestureReceiver的GameObject上:
using UnityEngine; public class GestureReceiver : MonoBehaviour { public void OnLeftSwipe() { Debug.Log("iOS原生左滑触发"); // 编写左滑逻辑 } public void OnRightSwipe() { Debug.Log("iOS原生右滑触发"); // 编写右滑逻辑 } public void OnUpSwipe() { Debug.Log("iOS原生上滑触发"); // 编写上滑逻辑 } public void OnDownSwipe() { Debug.Log("iOS原生下滑触发"); // 编写下滑逻辑 } }
注意:在Unity插件设置中,将iOSGesturePlugin.mm的平台限定为iOS。
二、第三方库/插件推荐
1. Unity官方Input System包
替代旧版Input Manager,原生支持触摸手势:
- 通过Package Manager安装
Input System包 - 创建
Input Action Asset,添加Swipe动作并配置阈值参数 - 脚本绑定示例:
using UnityEngine; using UnityEngine.InputSystem; public class SwipeDetector : MonoBehaviour { public InputAction swipeAction; void OnEnable() { swipeAction.Enable(); swipeAction.performed += OnSwipePerformed; } void OnDisable() { swipeAction.performed -= OnSwipePerformed; swipeAction.Disable(); } void OnSwipePerformed(InputAction.CallbackContext context) { Vector2 swipeDelta = context.ReadValue<Vector2>(); Debug.Log($"滑动方向:{swipeDelta}"); // 根据swipeDelta判断滑动方向 } }
2. Lean Touch
轻量级触摸处理插件,支持多种手势:
- 在Unity Asset Store安装后,挂载
LeanTouch组件 - 监听滑动事件示例:
using UnityEngine; using Lean.Touch; public class LeanSwipeHandler : MonoBehaviour { void OnEnable() { LeanTouch.OnSwipe += OnSwipe; } void OnDisable() { LeanTouch.OnSwipe -= OnSwipe; } void OnSwipe(LeanFinger finger) { Vector2 swipeDir = finger.SwipeScreenDelta; if (Mathf.Abs(swipeDir.x) > Mathf.Abs(swipeDir.y)) { Debug.Log(swipeDir.x > 0 ? "右滑" : "左滑"); } else { Debug.Log(swipeDir.y > 0 ? "上滑" : "下滑"); } } }
3. TouchKit
专注触摸手势的插件,提供开箱即用的滑动检测API,配置简单,适合新手快速集成。
三、可靠的自定义滑动检测实现
修正之前的自定义算法问题,加入阈值过滤误触和长按:
using UnityEngine; public class CustomSwipeDetector : MonoBehaviour { public float swipeThreshold = 50f; // 滑动位移阈值(像素) public float timeThreshold = 0.5f; // 滑动时间阈值(秒) private Vector2 touchStartPos; private float touchStartTime; void Update() { if (Input.touchCount == 1) { Touch touch = Input.GetTouch(0); switch (touch.phase) { case TouchPhase.Began: touchStartPos = touch.position; touchStartTime = Time.time; break; case TouchPhase.Ended: float timeElapsed = Time.time - touchStartTime; if (timeElapsed > timeThreshold) break; Vector2 swipeDelta = touch.position - touchStartPos; if (Mathf.Abs(swipeDelta.x) > swipeThreshold || Mathf.Abs(swipeDelta.y) > swipeThreshold) { if (Mathf.Abs(swipeDelta.x) > Mathf.Abs(swipeDelta.y)) { Debug.Log(swipeDelta.x > 0 ? "自定义右滑" : "自定义左滑"); } else { Debug.Log(swipeDelta.y > 0 ? "自定义上滑" : "自定义下滑"); } } break; } } } }
内容的提问来源于stack exchange,提问作者Zaxoosh
相关产品推荐
相关产品推荐

