Unity Fingers Gesture手势插件教程(新)
前言
前幾天Unity資源商城搞活動白嫖到了這個插件,但是看了下網上的教程都是比較舊的版本,所以決定學習記錄一下。個人感覺比easy touch使用方便一些,而且easy touch很久沒更新了。
操作
運行插件內演示場景DemoScene。
按住shift鍵或者ctrl模擬雙指操作,此時不需要點擊鼠標左邊,移動鼠標即可。如果點擊就是三指操作。
shift+鼠標滾輪上下:模擬雙指旋轉
ctrl+鼠標滾輪上下:模擬雙指縮放
配置
打開FingersScriptPrefab,面板里的參數用于全局控制。
參數(后面用到特殊的會繼續補充):
Default DPI:手勢觸發的靈敏度。如果值設置的特別大的話手勢會延遲觸發。除非有特殊需求不然用默認值即可。
我們可以通過修改下圖中的Touch來自定義手指顯示的內容。
基本使用
首先聲明手勢Recognizer。
private TapGestureRecognizer tapGesture;對Recognizer初始化并傳入回調。
private void CreateTapGesture(){tapGesture = new TapGestureRecognizer();tapGesture.StateUpdated += TapGestureCallback; //傳入回調FingersScript.Instance.AddGesture(tapGesture); //對應的有Remove}通過傳入的回調觸發行為。
private void DoubleTapGestureCallback(DigitalRubyShared.GestureRecognizer gesture){if (gesture.State == GestureRecognizerState.Ended){Debug.Log("Tap");}}常用的API(參考案例代碼)
注意手勢是全局的,不需要選中,如果想指定目標需要綁定目標或者自行加邏輯判斷。
StateUpdated:注冊手勢的回調事件
tripleTapGesture.StateUpdated += PlatformSpecificViewTapUpdated;
PlatformSpecificView:指定點擊的目標
tripleTapGesture.PlatformSpecificView = bottomLabel.gameObject;
NumberOfTapsRequired:Tap手勢的連點次數。單擊、雙擊等都是靠這個區分
tripleTapGesture.NumberOfTapsRequired = 3;
AddGesture:注冊手勢
FingersScript.Instance.AddGesture(tripleTapGesture);
GestureRecognizerState:手勢回調中判斷當前的執行階段
if (gesture.State == GestureRecognizerState.Ended)
RequireGestureRecognizerToFail:添加或移除一個需要失敗的手勢。 設置為 null 可以清除所有需要失敗的手勢。比如說下面的例子意思為如果doubleTapGesture手勢觸發了就會導致tapGesture觸發失敗。其實就是防止手勢沖突使用的
tapGesture.RequireGestureRecognizerToFail = doubleTapGesture;
gesture.FocusX, gesture.FocusY:回調中點擊的屏幕坐標
DebugText("Double tapped at {0}, {1}", gesture.FocusX, gesture.FocusY);
Direction:設置滑動方向
swipeGesture.Direction = SwipeGestureRecognizerDirection.Down;
DirectionThreshold:對于設置的方向,這是在該方向與另一個方向上必須成比例的滑動量。 例如,與沿 x 軸移動相比,向下滑動手勢需要在 y 軸上移動更多倍數。 默認值為 1.5,這意味著向下滑動手勢需要在 y 軸與 x 軸上大 1.5 倍。小于或等于 1 表示任何比率都可以接受。簡單來說就是這個值越大就需要越貼合目標方向
swipeGesture.DirectionThreshold = directionThreshold;// allow a swipe, regardless of slope
gesture.StartFocusX, gesture.StartFocusY:手勢開始的位置
DebugText("Swiped from {0},{1} to {2},{3}; velocity: {4}, {5}", gesture.StartFocusX, gesture.StartFocusY, gesture.FocusX, gesture.FocusY, swipeGesture.VelocityX, swipeGesture.VelocityY);
swipeGesture.VelocityX, swipeGesture.VelocityY:滑動的速度(根據focus)
DebugText("Swiped from {0},{1} to {2},{3}; velocity: {4}, {5}", gesture.StartFocusX, gesture.StartFocusY, gesture.FocusX, gesture.FocusY, swipeGesture.VelocityX, swipeGesture.VelocityY);
MinimumNumberOfTouchesToTrack:設置最少用到的手指數,一般用1或2。要注意不是每個手勢都支持多指
panGesture.MinimumNumberOfTouchesToTrack = 2;
MaximumNumberOfTouchesToTrack:設置最大用到的手指數,默認用1或2
longPressGesture.MaximumNumberOfTouchesToTrack = 1;
gesture.DeltaX, gesture.DeltaY:手勢位移的距離
DebugText("Panned, Location: {0}, {1}, Delta: {2}, {3}", gesture.FocusX, gesture.FocusY, gesture.DeltaX, gesture.DeltaY);
scaleGesture.ScaleMultiplier:手指距離的縮放值
DebugText("Scaled: {0}, Focus: {1}, {2}", scaleGesture.ScaleMultiplier, scaleGesture.FocusX, scaleGesture.FocusY);
rotateGesture.RotationRadiansDelta:旋轉弧度的變化
Earth.transform.Rotate(0.0f, 0.0f, rotateGesture.RotationRadiansDelta * Mathf.Rad2Deg);
AllowSimultaneousExecution:允許多個手勢同時操作一個目標
panGesture.AllowSimultaneousExecution(scaleGesture);
panGesture.AllowSimultaneousExecution(rotateGesture);
scaleGesture.AllowSimultaneousExecution(rotateGesture);
CaptureGestureHandler:捕獲點擊的目標并處理。返回false是可穿透,true為不可穿透,null為默認。
FingersScript.Instance.CaptureGestureHandler = CaptureGestureHandler;private static bool? CaptureGestureHandler(GameObject obj){if (obj.name.StartsWith("PassThrough")){return false;}else if (obj.name.StartsWith("NoPass")){return true;}return null;}ComponentTypesToDenyPassThrough(重點注意):除了gesture view之外設置可阻擋gesture檢測的類型。比如防止穿透UI。
FingersScript.Instance.ComponentTypesToDenyPassThrough.Add(typeof(UnityEngine.UI.Image));
StartOrResetGesture:開始或重置手勢。參數分別為手勢、目標是否顯示在最前面(只對sprite有效)、相機、執行手勢的游戲對象、顯示的sprite、執行手勢的模式(是在綁定物體上執行還是任意對象)、接收的相機
FingersScript.StartOrResetGesture(r, BringToFront, Cameras, gameObject, spriteRenderer, GestureRecognizerComponentScriptBase.GestureObjectMode.RequireIntersectWithGameObject, out camera);
ClearTrackedTouchesOnEndOrFail:是否在手勢結束或失敗時清空跟蹤的touches,默認為false。 一般情況下設置為false
tapGesture.ClearTrackedTouchesOnEndOrFail = true;
AddMask:為手勢添加限制區域。參數為collider和手勢。只有在Mask范圍內點擊目標才會生效
FingersScript.Instance.AddMask(Mask1, tapGesture);
AllowSimultaneousExecutionWithAllGestures:允許和其他所有手勢一起執行
tapGesture2.AllowSimultaneousExecutionWithAllGestures();
FingersScript.HasInstance:檢測場景中是否有FingersScript實例
if (FingersScript.HasInstance)
ShowTouches:顯示模擬的點擊,僅在調試時執行此操作,因為它會干擾其他Canvas
FingersScript.Instance.ShowTouches = true;
獲取點擊位置所有可接收射線的物體。
private void TapGestureUpdated(DigitalRubyShared.GestureRecognizer gesture){if (gesture.State == GestureRecognizerState.Ended){Debug.Log("Tapped on " + gesture.PlatformSpecificView);List<UnityEngine.EventSystems.RaycastResult> results = new List<UnityEngine.EventSystems.RaycastResult>();UnityEngine.EventSystems.PointerEventData eventData = new UnityEngine.EventSystems.PointerEventData(UnityEngine.EventSystems.EventSystem.current);eventData.position = new Vector2(gesture.FocusX, gesture.FocusY);UnityEngine.EventSystems.EventSystem.current.RaycastAll(eventData, results);Debug.Log("Event system raycast count: " + results.Count + ", objects: " + string.Join(",", results.Select(r => r.gameObject.name).ToArray()));}}組件介紹
參數比較簡單,直譯或者查看代碼即可理解。
關于搖桿
參數簡單介紹
DeadZone:死區,小于該值的不會觸發搖桿效果。類似敏感度
InputCurve:傳值曲線
EightAxisMode:八角模式,搖桿會變成上下左右和對角八個方向,搖桿轉動有間隔感
MaxRangeRadiusMultiplier:搖桿中心圓拖動到邊界時的偏移
FollowSpeed:搖桿整體跟隨手指的速度,默認為0
StartMode:MustStartInside(搖桿靜止,在搖桿上操作)MoveToPanStart(根據Pan手勢作為搖桿起始點)PanInsideOrPanMovesInside(搖桿靜止,在Pan手勢區域內操作)
RestrictJoystickPositionTo:需要Unity2019以上。將搖桿位置限制在此 collider2D 上,null 表示沒有限制。將 colider 放在搖桿父對象中
ReturnToHomePosition:操作完后自動回到初始位置
Cross Platform Input:輸入軸
Callbacks:搖桿回調,接收Vector2
使用方法
為搖桿傳入回調事件。當然也可以在面板上綁定。
Joystick.JoystickExecuted = JoystickExecuted;
為搖桿加入遮罩。(可選)
FingersScript.Instance.AddMask(Mask1, JoystickScript.PanGesture);
各類Gesture組件
查看DemoSceneComponents場景。面板上功能與前面API介紹的差不多。
FingersPanOrbit
場景:DemoScene3DOrbit
效果:可以方便的實現對目標物體的旋轉移動和縮放,通過調整參數快速的實現想要的效果。
FingersDragDropComponentScript
長按拖拽物體,3D或sprite、UI都可以,但注意只有sprite有顯示在最前面的效果,可以自己修改邏輯。
作者做了很多案例,都是使用這款插件的核心功能擴展的,這里就不一一記述了,大家可以自行翻閱以加深理解。
后續用到其他知識點會繼續補充。
備注
遇到問題,FingersScript與我的游戲框架中生成的UICanvas沖突了,Canvas接收不到點擊事件。
解決方案: 在場景中創建一個UI->EventSystem,不要動態生成。
總結
以上是生活随笔為你收集整理的Unity Fingers Gesture手势插件教程(新)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大西瓜源码
- 下一篇: HTML中粗体strong与b,斜体em