
THAM GIA GROUP CỦA TTC TRÊN FACEBOOK
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();
}
}
}