[Asset] [Unity] TouchScript - Chia sẻ Custom Gestures

Dang_KhoaDang_Khoa Posts: 3,861Administrators
68747470733a2f2f7261772e6769746875622e636f6d2f77696b692f546f7563685363726970742f546f7563685363726970742f696d616765732f647666752e6a7067

Giới thiệu:
TouchScript là asset chuyên dùng để xử lý các thao tác trên màn hình được đánh giá cao nhất hiện nay.
Nó cho phép mình tự tạo các Gestures riêng - tức là các bộ nhận diện thao tác trên màn hình, tuỳ mục đích sử dụng (chạm, vuốt, kéo, xoay...)

Tải về:
Có thể tải trong 2 địa chỉ sau:
Custom Gestures:

Swipe: Custom Gesture đầu tiên của mình, giúp nhận diện thao tác vuốt trái, phải, lên, xuống
using System.Collections.Generic;
using UnityEngine;
using TouchScript.Gestures;
using TouchScript.Pointers;
using System;

public class SwipeGesture : Gesture
{

   #region Events

   public event EventHandler<EventArgs> Swiped
   {
       add { swipedInvoker += value; }
       remove { swipedInvoker -= value; }
   }

   // Needed to overcome iOS AOT limitations
   private EventHandler<EventArgs> swipedInvoker;

   // Dùng cho unity event
   public GestureEvent OnSwipe = new GestureEvent();

   #endregion

   #region Public properties
   public float MinDistande
   {
       get { return minDistance; }
       set { minDistance = value; }
   }

   /// <summary>
   /// Gets or sets direction to look for.
   /// </summary>
   /// <value> Direction of movement. </value>
   public GestureDirection Direction
   {
       get { return direction; }
       set { direction = value; }
   }

   public Vector2 ScreenStartPosition { get; private set; }
   /// <summary>
   /// Gets normalized swipe direction when gesture is recognized.
   /// </summary>
   public Vector2 ScreenSwipeVector { get; private set; }

   #endregion

   #region Private variables
   [SerializeField] private float minDistance = 0.5f;
   [SerializeField] private GestureDirection direction = GestureDirection.Any;

   // The only pointer we are interested in
   private Pointer primaryPointer;
   #endregion

   #region Unity methods
   [ContextMenu("Basic Editor")]
   private void switchToBasicEditor()
   {
       basicEditor = true;
   }
   #endregion

   #region Gesture callbacks

   // Pointers pressed this frame
   protected override void pointersPressed(IList<Pointer> pointers)
   {
       if (State == GestureState.Idle)
       {
           primaryPointer = pointers[0];
           ScreenStartPosition = primaryPointer.Position;
           // Start the gesture
           setState(GestureState.Possible);
       }
   }

   // Pointers updated this frame
   protected override void pointersUpdated(IList<Pointer> pointers)
   {
       if (State == GestureState.Possible)
       {
           foreach (var p in pointers)
           {
               if (p.Id == primaryPointer.Id)
               {
                   float minDistanceInCentimeter = minDistance * touchManager.DotsPerCentimeter;
                   switch (Direction)
                   {
                       case GestureDirection.Horizontal:

                           if (p.Position.x - ScreenStartPosition.x > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.right;
                               setState(GestureState.Recognized);
                           }
                           else if (ScreenStartPosition.x - p.Position.x > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.left;
                               setState(GestureState.Recognized);
                           }

                           break;
                       case GestureDirection.Vertical:

                           if (p.Position.y - ScreenStartPosition.y > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.up;
                               setState(GestureState.Recognized);
                           }
                           else if (ScreenStartPosition.y - p.Position.y > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.down;
                               setState(GestureState.Recognized);
                           }
                           break;

                       default:

                           if (p.Position.x - ScreenStartPosition.x > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.right;
                               setState(GestureState.Recognized);
                           }
                           else if (ScreenStartPosition.x - p.Position.x > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.left;
                               setState(GestureState.Recognized);
                           }
                           else if (p.Position.y - ScreenStartPosition.y > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.up;
                               setState(GestureState.Recognized);
                           }
                           else if (ScreenStartPosition.y - p.Position.y > minDistanceInCentimeter)
                           {
                               ScreenSwipeVector = Vector2.down;
                               setState(GestureState.Recognized);
                           }
                           break;
                   }
                   return;
               }
           }
       }
   }

   // Pointers released this frame
   protected override void pointersReleased(IList<Pointer> pointers)
   {
       foreach (var p in pointers)
       {
           if (p.Id == primaryPointer.Id)
           {
               // If the pointer we are interested was released, cancel the gesture
               setState(GestureState.Cancelled);
               return;
           }
       }
   }

   // Pointers cancelled this frame
   protected override void pointersCancelled(IList<Pointer> pointers)
   {
       foreach (var p in pointers)
       {
           if (p.Id == primaryPointer.Id)
           {
               // If the pointer we are interested was cancelled, cancel the gesture
               setState(GestureState.Cancelled);
               return;
           }
       }
   }

   // Called when the gesture transitions to Began state
   protected override void onRecognized()
   {
       if (swipedInvoker != null) swipedInvoker(this, EventArgs.Empty);
       if (UseUnityEvents) OnSwipe.Invoke(this);
   }

   // This method is called when gesture is reset when recognized or failed
   protected override void reset()
   {
       base.reset();
       primaryPointer = null;
       ScreenStartPosition = Vector2.zero;
       ScreenSwipeVector = Vector2.zero;
   }

   #endregion

   /// <summary>
   /// Direction of a flick.
   /// </summary>
   public enum GestureDirection
   {
       Any,

       Horizontal,

       Vertical,
   }

}
using UnityEngine;
using UnityEditor;

namespace TouchScript.Editor.Gestures
{
   [CustomEditor(typeof(SwipeGesture), true)]
   internal sealed class SwipeGestureEditor : GestureEditor
   {

       public static readonly GUIContent TEXT_HELP = new GUIContent("Component này nhận diện thao tác vuốt trên object.");
       public static readonly GUIContent DIRECTION = new GUIContent("Direction", "Vuốt ngang hay dọc");
       public static readonly GUIContent MIN_DISTANCE = new GUIContent("Minimum Distance (cm)", "Khoảng cách ngắn nhất (tính bằng cm) mà pointer di chuyển để thao tác được nhận diện là vuốt.");

       private SerializedProperty direction;
       private SerializedProperty minDistance;
       private SerializedProperty OnSwipe;

       protected override void OnEnable()
       {
           minDistance = serializedObject.FindProperty("minDistance");
           direction = serializedObject.FindProperty("direction");
           
           OnSwipe = serializedObject.FindProperty("OnSwipe");

           base.OnEnable();
       }

       protected override GUIContent getHelpText()
       {
           return TEXT_HELP;
       }
       protected override void drawBasic()
       {
           EditorGUILayout.PropertyField(direction, DIRECTION);
       }

       protected override void drawGeneral()
       {
           EditorGUILayout.PropertyField(direction, DIRECTION);
           EditorGUILayout.PropertyField(minDistance, MIN_DISTANCE);
           base.drawGeneral();
       }

       protected override void drawUnityEvents()
       {
           EditorGUILayout.PropertyField(OnSwipe);
           base.drawUnityEvents();
       }
   }
}

Lưu ý:
Đây là topic chia sẻ Gesture, nếu ai có thì chia sẻ hoặc cần Gesture thì cứ gửi yêu cầu tại đây nha.
Script đặt tên trùng với tên class, nếu đuôi có chữ Editor thì bạn đặt nó vào thư mục tên Editor là được nha.
Sign In or Register to comment.