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