手势插件引入暂存

This commit is contained in:
Caiyanpeng committed 2021-10-08 16:03:55 +08:00
1 parent 6d1bc8e2c8
commit fd8f419f09
70 files changed
+5062 -126

No files matched your search

@@ -0,0 +1,178 @@
using UnityEngine;
using System.Collections.Generic;
using System;
namespace Prime31
{
public enum SwipeDirection
{
Left = ( 1 << 0 ),
Right = ( 1 << 1 ),
Up = ( 1 << 2 ),
Down = ( 1 << 4 ),
Horizontal = ( Left | Right ),
Vertical = ( Up | Down ),
All = ( Horizontal | Vertical )
}
public class TKLSwipeDetector : MonoBehaviour
{
public event Action<SwipeDirection> onSwipeDeteced;
public float timeToSwipe = 0.5f;
public float swipeVelocity;
SwipeDirection completedSwipeDirection;
public float _minimumDistance = 2f;
public float _allowedVariance = 1.5f;
public SwipeDirection _swipesToDetect = SwipeDirection.All;
// swipe state info
private Vector2 _startPoint;
private float _startTime;
private SwipeDirection _swipeDetectionState;
// the current swipes that are still possibly valid
bool _didCompleteDetection = true;
void Update()
{
// dont process drags if we have no input
if( TouchKitLite.instance.liveTouches.Count == 0 )
return;
var touch = TouchKitLite.instance.liveTouches[0];
// touch down, possible chance for a swipe
if( touch.phase == TouchPhase.Began )
{
_swipeDetectionState = _swipesToDetect;
_startPoint = touch.position;
_startTime = Time.time;
_didCompleteDetection = false;
}
else if( touch.phase == TouchPhase.Moved )
{
if( !_didCompleteDetection && checkForSwipeCompletion( touch ) )
{
_didCompleteDetection = true;
if( onSwipeDeteced != null )
onSwipeDeteced( completedSwipeDirection );
}
}
}
private bool checkForSwipeCompletion( TKLTouch touch )
{
// if we have a time stipulation and we exceeded it stop listening for swipes
if( timeToSwipe > 0.0f && ( Time.time - _startTime ) > timeToSwipe )
{
return false;
}
// when dealing with standalones (non touch-based devices) we need to be careful what we examaine
// we filter out all touches (mouse movements really) that didnt move
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER || UNITY_WEBGL
if( touch.deltaPosition.x != 0.0f || touch.deltaPosition.y != 0.0f )
{
#endif
// check the delta move positions. We can rule out at least 2 directions
if( touch.deltaPosition.x > 0.0f )
_swipeDetectionState &= ~SwipeDirection.Left;
if( touch.deltaPosition.x < 0.0f )
_swipeDetectionState &= ~SwipeDirection.Right;
if( touch.deltaPosition.y < 0.0f )
_swipeDetectionState &= ~SwipeDirection.Up;
if( touch.deltaPosition.y > 0.0f )
_swipeDetectionState &= ~SwipeDirection.Down;
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER || UNITY_WEBGL
}
#endif
//Debug.Log( string.Format( "swipeStatus: {0}", swipeDetectionState ) );
// Grab the total distance moved in both directions
var xDeltaAbsCm = Mathf.Abs( _startPoint.x - touch.position.x ) / TouchKitLite.instance.screenPixelsPerCm;
var yDeltaAbsCm = Mathf.Abs( _startPoint.y - touch.position.y ) / TouchKitLite.instance.screenPixelsPerCm;
// only check for swipes in directions that havent been ruled out yet
// left check
if( ( _swipeDetectionState & SwipeDirection.Left ) != 0 )
{
if( xDeltaAbsCm > _minimumDistance )
{
if( yDeltaAbsCm < _allowedVariance )
{
completedSwipeDirection = SwipeDirection.Left;
swipeVelocity = xDeltaAbsCm / ( Time.time - _startTime );
return true;
}
// We exceeded our variance so this swipe is no longer allowed
_swipeDetectionState &= ~SwipeDirection.Left;
}
}
// right check
if( ( _swipeDetectionState & SwipeDirection.Right ) != 0 )
{
if( xDeltaAbsCm > _minimumDistance )
{
if( yDeltaAbsCm < _allowedVariance )
{
completedSwipeDirection = SwipeDirection.Right;
swipeVelocity = xDeltaAbsCm / ( Time.time - _startTime );
return true;
}
// We exceeded our variance so this swipe is no longer allowed
_swipeDetectionState &= ~SwipeDirection.Right;
}
}
// up check
if( ( _swipeDetectionState & SwipeDirection.Up ) != 0 )
{
if( yDeltaAbsCm > _minimumDistance )
{
if( xDeltaAbsCm < _allowedVariance )
{
completedSwipeDirection = SwipeDirection.Up;
swipeVelocity = yDeltaAbsCm / ( Time.time - _startTime );
return true;
}
// We exceeded our variance so this swipe is no longer allowed
_swipeDetectionState &= ~SwipeDirection.Up;
}
}
// down check
if( ( _swipeDetectionState & SwipeDirection.Down ) != 0 )
{
if( yDeltaAbsCm > _minimumDistance )
{
if( xDeltaAbsCm < _allowedVariance )
{
completedSwipeDirection = SwipeDirection.Down;
swipeVelocity = yDeltaAbsCm / ( Time.time - _startTime );
return true;
}
// We exceeded our variance so this swipe is no longer allowed
_swipeDetectionState &= ~SwipeDirection.Down;
}
}
return false;
}
}
}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 11d446fb7bf8547c2b2ea2bc44c731dc
timeCreated: 1434758615
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
+143
View File
@@ -0,0 +1,143 @@
using UnityEngine;
using System.Collections;
namespace Prime31 {
public class TKLTouch
{
public readonly int fingerId;
public Vector2 position;
public Vector2 deltaPosition;
public float deltaTime;
public int tapCount;
public TouchPhase phase = TouchPhase.Ended;
public Vector2 previousPosition
{
get { return position - deltaPosition; }
}
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER || UNITY_WEBGL
// used to track mouse movement and fake touches
private Vector2? _lastPosition;
private double _lastClickTime;
private double _multipleClickInterval = 0.2;
#endif
public TKLTouch( int fingerId )
{
// lock this TKLTouch to the fingerId
this.fingerId = fingerId;
}
public TKLTouch populateWithTouch( Touch touch )
{
position = touch.position;
deltaPosition = touch.deltaPosition;
deltaTime = touch.deltaTime;
tapCount = touch.tapCount;
// canceled and ended are the same to us
if( touch.phase == TouchPhase.Canceled )
phase = TouchPhase.Ended;
else
phase = touch.phase;
return this;
}
/// <summary>
/// populates the TKLTouch with either mouse or touch input
/// </summary>
public void populate()
{
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER || UNITY_WEBGL
if (!UnityEditor.EditorApplication.isRemoteConnected)
populateFromMouse();
#else
populateWithTouch( Input.touches[0] );
#endif
}
public bool hasMouseInput()
{
return Input.GetMouseButtonUp( 0 ) || Input.GetMouseButton( 0 );
}
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER || UNITY_WEBGL
public TKLTouch populateWithPosition( Vector3 currentPosition, TouchPhase touchPhase )
{
var currentPosition2d = new Vector2( currentPosition.x, currentPosition.y );
// if we have a lastMousePosition use it to get a delta
if( _lastPosition.HasValue )
deltaPosition = currentPosition2d - _lastPosition.Value;
else
deltaPosition = new Vector2( 0, 0 );
switch( touchPhase )
{
case TouchPhase.Began:
phase = TouchPhase.Began;
// check for multiple clicks
if( Time.time < _lastClickTime + _multipleClickInterval )
tapCount++;
else
tapCount = 1;
_lastPosition = currentPosition2d;
_lastClickTime = Time.time;
break;
case TouchPhase.Stationary:
case TouchPhase.Moved:
if( deltaPosition.sqrMagnitude == 0 )
phase = TouchPhase.Stationary;
else
phase = TouchPhase.Moved;
_lastPosition = currentPosition2d;
break;
case TouchPhase.Ended:
phase = TouchPhase.Ended;
_lastPosition = null;
break;
}
position = currentPosition2d;
return this;
}
public TKLTouch populateFromMouse()
{
// do we have some input to work with?
if( hasMouseInput() )
{
TouchPhase phase = TouchPhase.Moved;
if( Input.GetMouseButtonUp( 0 ) )
phase = TouchPhase.Ended;
if( Input.GetMouseButtonDown( 0 ) )
phase = TouchPhase.Began;
var currentMousePosition = new Vector2( Input.mousePosition.x, Input.mousePosition.y );
this.populateWithPosition( currentMousePosition, phase );
}
return this;
}
#endif
public override string ToString()
{
return string.Format( "[TKLTouch] fingerId: {0}, phase: {1}, position: {2}", fingerId, phase, position );
}
}}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: dd67e044dba9145e4b372b0efe14cb35
timeCreated: 1434758248
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,119 @@
using UnityEngine;
using System.Collections.Generic;
namespace Prime31 {
public class TouchKitLite : MonoBehaviour
{
public bool shouldProcessTouches = true;
const int kTotalTouchesToProcess = 2;
public List<TKLTouch> liveTouches = new List<TKLTouch>( 2 );
TKLTouch[] _touchCache;
const float inchesToCentimeters = 2.54f;
public float screenPixelsPerCm
{
get
{
var fallbackDpi = 72f;
#if UNITY_ANDROID
// Android MDPI setting fallback
// http://developer.android.com/guide/practices/screens_support.html
fallbackDpi = 160f;
#elif UNITY_WP8 || UNITY_WP8_1 || UNITY_WSA || UNITY_WSA_8_0
// Windows phone is harder to track down
// http://www.windowscentral.com/higher-resolution-support-windows-phone-7-dpi-262
fallbackDpi = 92f;
#elif UNITY_IOS
// iPhone 4-6 range
fallbackDpi = 326f;
#endif
return Screen.dpi == 0f ? fallbackDpi / inchesToCentimeters : Screen.dpi / inchesToCentimeters;
}
}
private static TouchKitLite _instance = null;
public static TouchKitLite instance
{
get
{
if( System.Object.Equals( _instance, null ) )
_instance = FindObjectOfType( typeof( TouchKitLite ) ) as TouchKitLite;
return _instance;
}
}
#region MonoBehaviour
void Awake()
{
// prep our TKLTouch cache so we avoid excessive allocations
_touchCache = new TKLTouch[kTotalTouchesToProcess];
for( int i = 0; i < kTotalTouchesToProcess; i++ )
_touchCache[i] = new TKLTouch( i );
}
void Update()
{
liveTouches.Clear();
if( !shouldProcessTouches )
return;
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER || UNITY_WEBGL
// we only need to process if we have some interesting input this frame
if( Input.GetMouseButtonUp( 0 ) || Input.GetMouseButton( 0 ) )
{
#if UNITY_EDITOR
// in the editor if the remote is not connected we use the mouse to simulate touch
if( !UnityEditor.EditorApplication.isRemoteConnected )
#endif
liveTouches.Add( _touchCache[0].populateFromMouse() );
}
#endif
if( Input.touchCount > 0 )
{
var maxTouchIndexToExamine = Mathf.Min( Input.touches.Length, kTotalTouchesToProcess );
for( var i = 0; i < maxTouchIndexToExamine; i++ )
{
var touch = Input.touches[i];
if( touch.fingerId < kTotalTouchesToProcess )
liveTouches.Add( _touchCache[touch.fingerId].populateWithTouch( touch ) );
}
}
}
void OnApplicationQuit()
{
_instance = null;
}
#endregion
public bool hasTouchBeganInRect( Rect rect )
{
for( var i = 0; i < liveTouches.Count; i++ )
{
var touch = liveTouches[i];
if( touch.phase == TouchPhase.Began && rect.Contains( touch.position ) )
return true;
}
return false;
}
}}
@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c9b42263bedf3484b87e7191b7a5501b
timeCreated: 1434758154
licenseType: Pro
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: