手势插件引入暂存
This commit is contained in:
1 parent
6d1bc8e2c8
commit
fd8f419f09
70 files changed
+5062
-126
No files matched your search
@@ -0,0 +1,94 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// replacement for the Unity Rect class that is TouchKit and resolution-aware. Creating one will automatically scale all values
|
||||
/// if TouchKit autoScaleRectsAndDistances is true based on your designTimeResolution.
|
||||
///
|
||||
/// Note the TKRects use the bottom-left as the origin.
|
||||
/// </summary>
|
||||
public struct TKRect
|
||||
{
|
||||
public float x;
|
||||
public float y;
|
||||
public float width;
|
||||
public float height;
|
||||
|
||||
public float xMin { get { return x; } }
|
||||
public float xMax { get { return x + width; } }
|
||||
public float yMin { get { return y; } }
|
||||
public float yMax { get { return y + height; } }
|
||||
public Vector2 center { get { return new Vector2( x + ( width / 2f ), y + ( height / 2f ) ); } }
|
||||
|
||||
|
||||
public TKRect( float x, float y, float width, float height )
|
||||
{
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
|
||||
updateRectWithRuntimeScaleModifier();
|
||||
}
|
||||
|
||||
|
||||
public TKRect( float width, float height, Vector2 center )
|
||||
{
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.x = center.x - width / 2f;
|
||||
this.y = center.y - height / 2f;
|
||||
|
||||
updateRectWithRuntimeScaleModifier();
|
||||
}
|
||||
|
||||
|
||||
private void updateRectWithRuntimeScaleModifier()
|
||||
{
|
||||
var multiplier = TouchKit.instance.runtimeScaleModifier;
|
||||
|
||||
x *= multiplier.x;
|
||||
y *= multiplier.y;
|
||||
width = width * multiplier.x;
|
||||
height = height * multiplier.y;
|
||||
}
|
||||
|
||||
|
||||
public TKRect copyWithExpansion( float allSidesExpansion )
|
||||
{
|
||||
return copyWithExpansion( allSidesExpansion, allSidesExpansion );
|
||||
}
|
||||
|
||||
|
||||
public TKRect copyWithExpansion( float xExpansion, float yExpansion )
|
||||
{
|
||||
xExpansion *= TouchKit.instance.runtimeScaleModifier.x;
|
||||
yExpansion *= TouchKit.instance.runtimeScaleModifier.y;
|
||||
|
||||
var rect = new TKRect();
|
||||
rect.x = this.x - xExpansion;
|
||||
rect.y = this.y - yExpansion;
|
||||
rect.width = this.width + ( xExpansion * 2f );
|
||||
rect.height = this.height + ( yExpansion * 2f );
|
||||
|
||||
return rect;
|
||||
}
|
||||
|
||||
|
||||
public bool contains( Vector2 point )
|
||||
{
|
||||
if( x <= point.x && y <= point.y && xMax >= point.x && yMax >= point.y )
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return string.Format( "TKRect: x: {0}, xMax: {1}, y: {2}, yMax: {3}, width: {4}, height: {5}, center: {6}",
|
||||
x, xMax, y, yMax, width, height, center );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 4f68dd8df2e674a94939789235fb9b28
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,130 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
|
||||
public class TKTouch
|
||||
{
|
||||
public readonly int fingerId;
|
||||
public Vector2 position;
|
||||
public Vector2 startPosition;
|
||||
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 TKTouch( int fingerId )
|
||||
{
|
||||
// lock this TKTouch to the fingerId
|
||||
this.fingerId = fingerId;
|
||||
}
|
||||
|
||||
public TKTouch populateWithTouch( Touch touch )
|
||||
{
|
||||
position = touch.position;
|
||||
deltaPosition = touch.deltaPosition;
|
||||
deltaTime = touch.deltaTime;
|
||||
tapCount = touch.tapCount;
|
||||
|
||||
if (touch.phase== TouchPhase.Began)
|
||||
{
|
||||
startPosition = position;
|
||||
}
|
||||
|
||||
// canceled and ended are the same to us
|
||||
if( touch.phase == TouchPhase.Canceled )
|
||||
phase = TouchPhase.Ended;
|
||||
else
|
||||
phase = touch.phase;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR || UNITY_STANDALONE_OSX || UNITY_STANDALONE_WIN || UNITY_WEBPLAYER || UNITY_WEBGL
|
||||
public TKTouch 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;
|
||||
startPosition = 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 TKTouch populateFromMouse()
|
||||
{
|
||||
// do we have some input to work with?
|
||||
if( Input.GetMouseButtonUp( 0 ) || Input.GetMouseButton( 0 ) )
|
||||
{
|
||||
var phase = TouchPhase.Moved;
|
||||
|
||||
// guard against down and up being called in the same frame
|
||||
if( Input.GetMouseButtonDown( 0 ) && Input.GetMouseButtonUp( 0 ) )
|
||||
phase = TouchPhase.Canceled;
|
||||
else if( Input.GetMouseButtonUp( 0 ) )
|
||||
phase = TouchPhase.Ended;
|
||||
else 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( "[TKTouch] fingerId: {0}, phase: {1}, position: {2}", fingerId, phase, position );
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2defb83e5e57b48bfb27864a8ab5e134
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
@@ -0,0 +1,171 @@
|
||||
using UnityEngine;
|
||||
using System.Collections;
|
||||
|
||||
#if UNITY_EDITOR
|
||||
/// <summary>
|
||||
/// this only exists in the editor to assist with testing and simulating touches and keeping the main class clean
|
||||
/// </summary>
|
||||
public partial class TouchKit
|
||||
{
|
||||
private Vector3? _simulatedMultitouchStartPosition;
|
||||
private Vector3 _simulatedMousePosition;
|
||||
private bool _hasActiveSimulatedTouch;
|
||||
private bool _hasActiveSimulatedMultitouch;
|
||||
private bool _hasUnityRemoteActive;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// returns true if mouse input should be processed as touch input. it will be true when the Unity Remote is not active.
|
||||
/// </summary>
|
||||
private bool shouldProcessMouseInput()
|
||||
{
|
||||
if( !simulateTouches )
|
||||
return false;
|
||||
|
||||
// check to see if the Unity Remote is active
|
||||
if( Input.touchCount > 0 )
|
||||
{
|
||||
Debug.LogWarning( "disabling touch simulation because we detected a Unity Remote connected" );
|
||||
simulateTouches = false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// if enabled and alt is being held down we are simulating pinching
|
||||
if( simulateMultitouch && ( _hasActiveSimulatedMultitouch || Input.GetKey( KeyCode.LeftAlt ) || Input.GetKeyUp( KeyCode.LeftAlt ) ) )
|
||||
{
|
||||
if( Input.GetKeyDown( KeyCode.LeftAlt ) )
|
||||
{
|
||||
_simulatedMultitouchStartPosition = Input.mousePosition;
|
||||
}
|
||||
else if( Input.GetKey( KeyCode.LeftShift ) )
|
||||
{
|
||||
// calculate the last mouse position from the simulated position and shift the start position acordingly
|
||||
var lastMousePosition = _simulatedMultitouchStartPosition.Value + ( _simulatedMultitouchStartPosition.Value - _simulatedMousePosition );
|
||||
var diff = Input.mousePosition - lastMousePosition;
|
||||
_simulatedMultitouchStartPosition += diff;
|
||||
}
|
||||
|
||||
if( Input.GetKey( KeyCode.LeftAlt ) || Input.GetKeyUp( KeyCode.LeftAlt ) )
|
||||
{
|
||||
var diff = new Vector3( Input.mousePosition.x - _simulatedMultitouchStartPosition.Value.x, Input.mousePosition.y - _simulatedMultitouchStartPosition.Value.y );
|
||||
_simulatedMousePosition = _simulatedMultitouchStartPosition.Value - diff;
|
||||
}
|
||||
|
||||
TouchPhase? touchPhase = null;
|
||||
if( Input.GetKey( KeyCode.LeftAlt ) && Input.GetMouseButton( 0 ) )
|
||||
{
|
||||
// if we haven't started yet, add a touch began, else move
|
||||
if( !_hasActiveSimulatedMultitouch )
|
||||
{
|
||||
_hasActiveSimulatedMultitouch = true;
|
||||
touchPhase = TouchPhase.Began;
|
||||
}
|
||||
else
|
||||
{
|
||||
touchPhase = TouchPhase.Moved;
|
||||
}
|
||||
}
|
||||
|
||||
if( ( Input.GetKeyUp( KeyCode.LeftAlt ) || Input.GetMouseButtonUp(0)) && _hasActiveSimulatedMultitouch )
|
||||
{
|
||||
touchPhase = TouchPhase.Ended;
|
||||
_hasActiveSimulatedMultitouch = false;
|
||||
}
|
||||
|
||||
|
||||
if( touchPhase.HasValue )
|
||||
{
|
||||
// we need to set up a second touch
|
||||
_liveTouches.Add( _touchCache[1].populateWithPosition( _simulatedMousePosition, touchPhase.Value ) );
|
||||
}
|
||||
|
||||
if( Input.GetKeyUp( KeyCode.LeftAlt ) )
|
||||
{
|
||||
_simulatedMultitouchStartPosition = null;
|
||||
}
|
||||
}
|
||||
|
||||
_hasActiveSimulatedTouch = Input.GetMouseButton( 0 );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// this is for debugging only while in the editor
|
||||
private void OnDrawGizmos()
|
||||
{
|
||||
if( _instance == null )
|
||||
return;
|
||||
|
||||
if( drawTouches )
|
||||
{
|
||||
// draw a green point for all active touches, including the touches from Unity remote
|
||||
foreach( TKTouch touch in _touchCache )
|
||||
{
|
||||
if( touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary )
|
||||
{
|
||||
var touchPos = Camera.main.ScreenToWorldPoint( new Vector3( touch.position.x, touch.position.y, Camera.main.farClipPlane ) );
|
||||
Gizmos.DrawIcon( touchPos, "greenPoint.png", false );
|
||||
}
|
||||
}
|
||||
|
||||
if( _simulatedMultitouchStartPosition.HasValue && !_hasActiveSimulatedTouch )
|
||||
{
|
||||
var mousePos = Camera.main.ScreenToWorldPoint( new Vector3( Input.mousePosition.x, Input.mousePosition.y, Camera.main.farClipPlane ) );
|
||||
Gizmos.DrawIcon( mousePos, "redPoint.png", false );
|
||||
|
||||
var simulatedPos = Camera.main.ScreenToWorldPoint( new Vector3( _simulatedMousePosition.x, _simulatedMousePosition.y, Camera.main.farClipPlane ) );
|
||||
Gizmos.DrawIcon( simulatedPos, "redPoint.png", false );
|
||||
}
|
||||
}
|
||||
|
||||
if( drawDebugBoundaryFrames )
|
||||
{
|
||||
var colors = new Color[]
|
||||
{
|
||||
Color.red,
|
||||
Color.cyan,
|
||||
Color.red,
|
||||
Color.magenta,
|
||||
Color.yellow
|
||||
};
|
||||
int i = 0;
|
||||
|
||||
foreach( var r in _gestureRecognizers )
|
||||
{
|
||||
if( r.boundaryFrame.HasValue )
|
||||
{
|
||||
debugDrawRect( r.boundaryFrame.Value, colors [i] );
|
||||
if( ++i >= colors.Length )
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private void debugDrawRect( TKRect rect, Color color )
|
||||
{
|
||||
var bl = new Vector3( rect.xMin, rect.yMin, 0 );
|
||||
var br = new Vector3( rect.xMax, rect.yMin, 0 );
|
||||
var tl = new Vector3( rect.xMin, rect.yMax, 0 );
|
||||
var tr = new Vector3( rect.xMax, rect.yMax, 0 );
|
||||
|
||||
bl = Camera.main.ScreenToWorldPoint( new Vector3( bl.x, bl.y, Camera.main.farClipPlane ) );
|
||||
br = Camera.main.ScreenToWorldPoint( new Vector3( br.x, br.y, Camera.main.farClipPlane ) );
|
||||
tl = Camera.main.ScreenToWorldPoint( new Vector3( tl.x, tl.y, Camera.main.farClipPlane ) );
|
||||
tr = Camera.main.ScreenToWorldPoint( new Vector3( tr.x, tr.y, Camera.main.farClipPlane ) );
|
||||
|
||||
// draw four sides
|
||||
Debug.DrawLine( bl, br, color );
|
||||
Debug.DrawLine( br, tr, color );
|
||||
Debug.DrawLine( tr, tl, color );
|
||||
Debug.DrawLine( tl, bl, color );
|
||||
|
||||
// make an "x" at the midpoint
|
||||
Debug.DrawLine( tl, br, color );
|
||||
Debug.DrawLine( bl, tr, color );
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d1f6cfc9c43424c2eaafa3f02ee2fb90
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
Reference in new issue
Block a user