This commit is contained in:
2025-09-02 16:06:33 +02:00
2 changed files with 44 additions and 7 deletions

View File

@@ -41,7 +41,7 @@
"name": "",
"id": "f3dcd77f-15e5-4621-af67-001e6b08e3e6",
"path": "<Touchscreen>/Press",
"interactions": "",
"interactions": "Hold",
"processors": "",
"groups": "",
"action": "TouchPress",

View File

@@ -10,6 +10,8 @@ public class InputManager : MonoBehaviour
private InputAction touchPositionAction;
private ITouchInputConsumer defaultConsumer;
private bool isTouchHeld = false;
void Awake()
{
if (Instance != null && Instance != this)
@@ -33,7 +35,10 @@ public class InputManager : MonoBehaviour
void OnEnable()
{
if (touchPressAction != null)
touchPressAction.performed += OnTouchPressPerformed;
{
touchPressAction.started += OnTouchPressStarted;
touchPressAction.canceled += OnTouchPressCanceled;
}
if (touchPositionAction != null)
touchPositionAction.performed += OnTouchPositionPerformed;
}
@@ -41,7 +46,10 @@ public class InputManager : MonoBehaviour
void OnDisable()
{
if (touchPressAction != null)
touchPressAction.performed -= OnTouchPressPerformed;
{
touchPressAction.started -= OnTouchPressStarted;
touchPressAction.canceled -= OnTouchPressCanceled;
}
if (touchPositionAction != null)
touchPositionAction.performed -= OnTouchPositionPerformed;
}
@@ -51,20 +59,49 @@ public class InputManager : MonoBehaviour
defaultConsumer = consumer;
}
private void OnTouchPressPerformed(InputAction.CallbackContext ctx)
private void OnTouchPressStarted(InputAction.CallbackContext ctx)
{
// For button actions, you may want to use InputValue or just handle the event
// If you need position, you can read it from touchPositionAction
// Touch started (finger down)
isTouchHeld = true;
Vector3 _screenPos = Camera.main.ScreenToWorldPoint(touchPositionAction.ReadValue<Vector2>());
Vector2 screenPos = new Vector2(_screenPos.x, _screenPos.y);
if (!TryDelegateToInteractable(screenPos))
defaultConsumer?.OnTouchPress(screenPos);
}
private void OnTouchPressCanceled(InputAction.CallbackContext ctx)
{
// Touch released (finger up)
isTouchHeld = false;
// Optionally, you can notify consumers of release if needed
}
private void OnTouchPositionPerformed(InputAction.CallbackContext ctx)
{
Vector2 pos = ctx.ReadValue<Vector2>();
defaultConsumer?.OnTouchPosition(pos);
if (isTouchHeld)
{
// Convert to world position
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
defaultConsumer?.OnTouchPress(worldPos2D); // Move continuously to finger position
}
else
{
defaultConsumer?.OnTouchPosition(pos); // For drag or hover
}
}
void Update()
{
// Continuously advertise the last touch position while held
if (isTouchHeld && touchPositionAction != null)
{
Vector2 pos = touchPositionAction.ReadValue<Vector2>();
Vector3 worldPos = Camera.main.ScreenToWorldPoint(pos);
Vector2 worldPos2D = new Vector2(worldPos.x, worldPos.y);
defaultConsumer?.OnTouchPress(worldPos2D);
}
}
private bool TryDelegateToInteractable(Vector2 worldPos)