2019-03-17 23:43:20 +01:00
|
|
|
using UnityEngine;
|
2019-10-01 17:27:22 +02:00
|
|
|
using UnityAtoms;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
|
|
|
namespace UnityAtoms.Mobile
|
|
|
|
{
|
2019-10-01 23:26:25 +02:00
|
|
|
/* Updates the TouchUserInputVariable on every Update tick. Meant to be called every Update.
|
2019-03-17 23:43:20 +01:00
|
|
|
*/
|
2019-05-29 18:52:27 +02:00
|
|
|
[CreateAssetMenu(menuName = "Unity Atoms/Actions/UpdateTouchUserInput", fileName = "UpdateTouchUserInputVariable")]
|
2019-04-07 16:03:16 +02:00
|
|
|
public sealed class UpdateTouchUserInput : VoidAction
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
|
|
|
public TouchUserInputVariable TouchUserInputVariable;
|
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
private TouchUserInput.State _inputState = TouchUserInput.State.None;
|
|
|
|
|
|
|
|
private Vector2 _inputPos = Vector2.zero;
|
|
|
|
|
|
|
|
private Vector2 _inputPosLastFrame = Vector2.zero;
|
|
|
|
|
|
|
|
private Vector2 _inputPosLastDown = Vector2.zero;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
|
|
|
public override void Do()
|
|
|
|
{
|
|
|
|
#if (UNITY_ANDROID || UNITY_IOS || UNITY_IPHONE) && !UNITY_EDITOR
|
|
|
|
if (Input.touchCount > 0)
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputPos = Input.GetTouch(0).position;
|
2019-04-07 11:10:09 +02:00
|
|
|
if (Input.GetTouch(0).phase == TouchPhase.Began)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputPosLastDown = _inputPos;
|
|
|
|
_inputState = TouchUserInput.State.Down;
|
2019-04-07 11:10:09 +02:00
|
|
|
}
|
|
|
|
else if (Input.GetTouch(0).phase == TouchPhase.Ended)
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputState = TouchUserInput.State.Up;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputState = TouchUserInput.State.Drag;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 11:10:09 +02:00
|
|
|
else
|
2019-03-17 23:43:20 +01:00
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputPos = Vector2.zero;
|
|
|
|
_inputState = TouchUserInput.State.None;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
#elif UNITY_EDITOR || UNITY_STANDALONE
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputPos = Input.mousePosition;
|
2019-03-17 23:43:20 +01:00
|
|
|
|
|
|
|
if (Input.GetMouseButtonDown(0))
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputPosLastDown = _inputPos;
|
|
|
|
_inputState = TouchUserInput.State.Down;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
else if (Input.GetMouseButtonUp(0))
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputState = TouchUserInput.State.Up;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
else if (Input.GetMouseButton(0))
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputState = TouchUserInput.State.Drag;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-04-07 16:03:16 +02:00
|
|
|
_inputState = TouchUserInput.State.None;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-04-07 16:03:16 +02:00
|
|
|
TouchUserInputVariable.SetValue(new TouchUserInput(_inputState, _inputPos, _inputPosLastFrame, _inputPosLastDown));
|
|
|
|
_inputPosLastFrame = _inputPos;
|
2019-03-17 23:43:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|