From 976afce5ff5d1ab48173ca72ebbb950521747edc Mon Sep 17 00:00:00 2001 From: Burn1ingApe Date: Tue, 27 Sep 2022 01:43:28 +0300 Subject: [PATCH] 1 --- Runtime/Touch.asmdef | 16 +++++ Runtime/Touch.asmdef.meta | 7 +++ Runtime/TouchControlls.cs | 20 ++++++ Runtime/TouchControlls.cs.meta | 11 ++++ Runtime/TouchData.cs | 56 +++++++++++++++++ Runtime/TouchData.cs.meta | 11 ++++ Runtime/TouchEvent.cs | 7 +++ Runtime/TouchEvent.cs.meta | 11 ++++ Runtime/TouchEvents.cs | 107 +++++++++++++++++++++++++++++++++ Runtime/TouchEvents.cs.meta | 11 ++++ 10 files changed, 257 insertions(+) create mode 100644 Runtime/Touch.asmdef create mode 100644 Runtime/Touch.asmdef.meta create mode 100644 Runtime/TouchControlls.cs create mode 100644 Runtime/TouchControlls.cs.meta create mode 100644 Runtime/TouchData.cs create mode 100644 Runtime/TouchData.cs.meta create mode 100644 Runtime/TouchEvent.cs create mode 100644 Runtime/TouchEvent.cs.meta create mode 100644 Runtime/TouchEvents.cs create mode 100644 Runtime/TouchEvents.cs.meta diff --git a/Runtime/Touch.asmdef b/Runtime/Touch.asmdef new file mode 100644 index 0000000..4593df9 --- /dev/null +++ b/Runtime/Touch.asmdef @@ -0,0 +1,16 @@ +{ + "name": "Controls", + "rootNamespace": "", + "references": [ + "GUID:560b04d1a97f54a4e82edc0cbbb69285" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Runtime/Touch.asmdef.meta b/Runtime/Touch.asmdef.meta new file mode 100644 index 0000000..dc7ada8 --- /dev/null +++ b/Runtime/Touch.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 05768dc7d5b006a4cb7cfb72ff8f2a0c +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/TouchControlls.cs b/Runtime/TouchControlls.cs new file mode 100644 index 0000000..352b750 --- /dev/null +++ b/Runtime/TouchControlls.cs @@ -0,0 +1,20 @@ +namespace BurningApe.Touch +{ + public class TouchControlls + { + private TouchEvents _touchEvents; + + public TouchEvent OnTapEvent = new TouchEvent(); + public TouchEvent OnSwipeEvent = new TouchEvent(); + public TouchEvent OnTouchMadeEvent = new TouchEvent(); + public TouchEvent OnTouchEndEvent = new TouchEvent(); + public TouchEvent OnSwipeOnceEvent = new TouchEvent(); + + public TouchControlls(TouchData data) => _touchEvents = new TouchEvents(data); + + + public void Run() => _touchEvents + .OnTouchEvent(OnTapEvent, OnSwipeEvent, OnTouchMadeEvent, OnTouchEndEvent, OnSwipeOnceEvent); + + } +} diff --git a/Runtime/TouchControlls.cs.meta b/Runtime/TouchControlls.cs.meta new file mode 100644 index 0000000..ce7b1cb --- /dev/null +++ b/Runtime/TouchControlls.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 8173a0d02723c8246bbdb4cad29c0108 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/TouchData.cs b/Runtime/TouchData.cs new file mode 100644 index 0000000..d03f3c5 --- /dev/null +++ b/Runtime/TouchData.cs @@ -0,0 +1,56 @@ +using UnityEngine; + +namespace BurningApe.Touch +{ + [System.Serializable] + public struct TouchData + { + #region Swipe Range + /// + /// Range within which swipe is detected + /// + public float SwipeRange { get; private set; } + #endregion + + #region Previous Touch Position + /// + /// The touch position that was made in previous frame + /// + [HideInInspector] public Vector2 PreviousPosition; + #endregion + + #region Current Touch Position + /// + /// The touch position in this frame + /// + [HideInInspector] public Vector2 CurrentPosition; + #endregion + + #region Distance + /// + /// Distance between previous and current touch position + /// + [HideInInspector] public Vector2 Distance; + #endregion + + #region Swipe Directions + /// + /// Avaliable directions of the swipe. + /// + public enum SwipeDirections + { + Right = 0, + Left = 1, + Up = 2, + Down = 3 + } + #endregion + + #region Current Swipe Direction + /// + /// Current direction of the swipe. + /// + [HideInInspector] public SwipeDirections CurrentSwipeDirection; + #endregion + } +} diff --git a/Runtime/TouchData.cs.meta b/Runtime/TouchData.cs.meta new file mode 100644 index 0000000..67d9e78 --- /dev/null +++ b/Runtime/TouchData.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: d182d5efb0aeee241b2c22c25e5165f7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/TouchEvent.cs b/Runtime/TouchEvent.cs new file mode 100644 index 0000000..a34b391 --- /dev/null +++ b/Runtime/TouchEvent.cs @@ -0,0 +1,7 @@ +using UnityEngine.Events; + +namespace BurningApe.Touch +{ + public class TouchEvent : UnityEvent { } +} + diff --git a/Runtime/TouchEvent.cs.meta b/Runtime/TouchEvent.cs.meta new file mode 100644 index 0000000..b2d17b3 --- /dev/null +++ b/Runtime/TouchEvent.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 257d69006ba6c1f4abf513277cd72c3d +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Runtime/TouchEvents.cs b/Runtime/TouchEvents.cs new file mode 100644 index 0000000..22bbe16 --- /dev/null +++ b/Runtime/TouchEvents.cs @@ -0,0 +1,107 @@ +using UnityEngine; + +namespace BurningApe.Touch +{ + public class TouchEvents + { + public TouchData _touchData; + private bool _swipe = false; + + + #region Constructor + public TouchEvents(TouchData newData) => _touchData = newData; + #endregion + + + #region Touch Event + + /// + /// Called when player touches the screen. + /// + /// Event to invoke when player taps the screen. + /// Event to invoke when player swipes the screen. + /// Event to invoke the first time player touches the screen. + /// Event to invoke when player releases finger from the screen. + /// Evebt to invoke when player releases finger only after the swipe + public void OnTouchEvent(TouchEvent OnTapEvent, TouchEvent OnSwipeEvent, + TouchEvent OnTouchMade, TouchEvent OnTouchEnd, TouchEvent OnSwipeOnce) + { + if (Input.touchCount > 0) + { + // Getting touch. + var touch = Input.GetTouch(0); + + // Set current touch position. + _touchData.CurrentPosition = touch.position; + + // Calculate distance between current touch position and previous. + _touchData.Distance = _touchData.CurrentPosition - _touchData.PreviousPosition; + + + switch (touch.phase) + { + case TouchPhase.Began: + // Invoke this event when the finger touches the screen + OnTouchMade.Invoke(_touchData); + + break; + + + case TouchPhase.Moved: + + // Unlock swipe if distance between prev and cur touch pos is bigger than swipe range. + if (_touchData.Distance.magnitude > _touchData.SwipeRange) + { + _swipe = true; + + // Identify direction of swipe. + if (_touchData.PreviousPosition.x < _touchData.CurrentPosition.x) + { + // Right + _touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Right; + } + else if (_touchData.PreviousPosition.x > _touchData.CurrentPosition.x) + { + // Left + _touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Left; + } + else if (_touchData.PreviousPosition.y < _touchData.CurrentPosition.y) + { + // Up + _touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Up; + } + else if (_touchData.PreviousPosition.y > _touchData.CurrentPosition.y) + { + // Down + _touchData.CurrentSwipeDirection = TouchData.SwipeDirections.Down; + } + + // Invoke on swipe event + OnSwipeEvent.Invoke(_touchData); + } + + break; + + + case TouchPhase.Ended: + + // Invoke on touch event only if the tap on screen wasnt identified as swipe. + if (!_swipe) OnTapEvent.Invoke(_touchData); + else OnSwipeOnce.Invoke(_touchData); + + // Lock swipe after calling touch event. + _swipe = false; + + // Invoke this event when the finger is released from the screen + OnTouchEnd.Invoke(_touchData); + + break; + } + + // Set previous touch position to current. + _touchData.PreviousPosition = _touchData.CurrentPosition; + } + } + #endregion + } +} diff --git a/Runtime/TouchEvents.cs.meta b/Runtime/TouchEvents.cs.meta new file mode 100644 index 0000000..0a6fe43 --- /dev/null +++ b/Runtime/TouchEvents.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 5132b34657d8b704db0b46fbc113285b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: