[Player][Input] Switch to generic input consumption interface. Version package json file
This commit is contained in:
8
Assets/Scripts/ITouchInputConsumer.cs
Normal file
8
Assets/Scripts/ITouchInputConsumer.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using UnityEngine;
|
||||
|
||||
public interface ITouchInputConsumer
|
||||
{
|
||||
void OnTouchPress(Vector2 screenPosition);
|
||||
void OnTouchPosition(Vector2 screenPosition);
|
||||
}
|
||||
|
||||
76
Assets/Scripts/InputManager.cs
Normal file
76
Assets/Scripts/InputManager.cs
Normal file
@@ -0,0 +1,76 @@
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using System;
|
||||
|
||||
public class InputManager : MonoBehaviour
|
||||
{
|
||||
public static InputManager Instance { get; private set; }
|
||||
private PlayerInput playerInput;
|
||||
private InputAction touchPressAction;
|
||||
private InputAction touchPositionAction;
|
||||
private ITouchInputConsumer defaultConsumer;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
if (Instance != null && Instance != this)
|
||||
{
|
||||
Destroy(gameObject);
|
||||
return;
|
||||
}
|
||||
Instance = this;
|
||||
DontDestroyOnLoad(gameObject);
|
||||
playerInput = GetComponent<PlayerInput>();
|
||||
if (playerInput == null)
|
||||
{
|
||||
Debug.LogError("InputManager requires a PlayerInput component attached to the same GameObject.");
|
||||
return;
|
||||
}
|
||||
// Find actions by name in the assigned action map
|
||||
touchPressAction = playerInput.actions.FindAction("TouchPress", false);
|
||||
touchPositionAction = playerInput.actions.FindAction("TouchPosition", false);
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
if (touchPressAction != null)
|
||||
touchPressAction.performed += OnTouchPressPerformed;
|
||||
if (touchPositionAction != null)
|
||||
touchPositionAction.performed += OnTouchPositionPerformed;
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
if (touchPressAction != null)
|
||||
touchPressAction.performed -= OnTouchPressPerformed;
|
||||
if (touchPositionAction != null)
|
||||
touchPositionAction.performed -= OnTouchPositionPerformed;
|
||||
}
|
||||
|
||||
public void SetDefaultConsumer(ITouchInputConsumer consumer)
|
||||
{
|
||||
defaultConsumer = consumer;
|
||||
}
|
||||
|
||||
private void OnTouchPressPerformed(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
|
||||
Vector3 _screenPos = Camera.main.ScreenToWorldPoint(touchPositionAction.ReadValue<Vector2>());
|
||||
Vector2 screenPos = new Vector2(_screenPos.x, _screenPos.y);
|
||||
if (!TryDelegateToInteractable(screenPos))
|
||||
defaultConsumer?.OnTouchPress(screenPos);
|
||||
}
|
||||
|
||||
private void OnTouchPositionPerformed(InputAction.CallbackContext ctx)
|
||||
{
|
||||
Vector2 pos = ctx.ReadValue<Vector2>();
|
||||
defaultConsumer?.OnTouchPosition(pos);
|
||||
}
|
||||
|
||||
private bool TryDelegateToInteractable(Vector2 screenPos)
|
||||
{
|
||||
// TODO: Raycast logic to find ITouchInputConsumer at screenPos
|
||||
// For now, always return false (no interactable found)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ using UnityEngine.InputSystem;
|
||||
|
||||
// Basic touch/mouse movement controller suitable for top-down 2D or 3D overworld
|
||||
// Attach to the player GameObject. Works with or without Rigidbody/Rigidbody2D.
|
||||
public class PlayerTouchController : MonoBehaviour
|
||||
public class PlayerTouchController : MonoBehaviour, ITouchInputConsumer
|
||||
{
|
||||
public float moveSpeed = 5f;
|
||||
public float stopDistance = 0.1f;
|
||||
@@ -32,64 +32,40 @@ public class PlayerTouchController : MonoBehaviour
|
||||
hasTarget = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
void OnEnable()
|
||||
{
|
||||
InputManager.Instance?.SetDefaultConsumer(this);
|
||||
}
|
||||
|
||||
// Remove Update and HandleInput
|
||||
|
||||
public void OnTouchPress(Vector2 worldPosition)
|
||||
{
|
||||
Debug.Log($"PlayerTouchController.OnTouchPress received worldPosition: {worldPosition}");
|
||||
SetTargetPosition(worldPosition);
|
||||
}
|
||||
|
||||
public void OnTouchPosition(Vector2 screenPosition)
|
||||
{
|
||||
Debug.Log($"PlayerTouchController.OnTouchPosition called with screenPosition: {screenPosition}");
|
||||
// Optionally handle drag/move here
|
||||
}
|
||||
|
||||
void SetTargetPosition(Vector2 worldPosition)
|
||||
{
|
||||
Debug.Log($"PlayerTouchController.SetTargetPosition: worldPosition={worldPosition}");
|
||||
targetPosition = new Vector3(worldPosition.x, worldPosition.y, transform.position.z);
|
||||
hasTarget = true;
|
||||
}
|
||||
|
||||
void FixedUpdate()
|
||||
{
|
||||
HandleInput();
|
||||
if (hasTarget)
|
||||
{
|
||||
MoveTowardsTarget();
|
||||
}
|
||||
}
|
||||
|
||||
void HandleInput()
|
||||
{
|
||||
#if ENABLE_INPUT_SYSTEM
|
||||
// Using the new Unity Input System (InputSystem package) only.
|
||||
// Touch input (mobile)
|
||||
if (Touchscreen.current != null && Touchscreen.current.touches.Count > 0)
|
||||
{
|
||||
var touch = Touchscreen.current.touches[0];
|
||||
// press indicates the touch is down; use position even while moved/held
|
||||
if (touch.press != null && touch.press.isPressed)
|
||||
{
|
||||
Vector2 pos = touch.position.ReadValue();
|
||||
SetTargetFromScreenPoint(new Vector3(pos.x, pos.y, 0f));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Mouse support (Editor/PC)
|
||||
if (Mouse.current != null && Mouse.current.leftButton != null && Mouse.current.leftButton.isPressed)
|
||||
{
|
||||
Vector2 mpos = Mouse.current.position.ReadValue();
|
||||
SetTargetFromScreenPoint(new Vector3(mpos.x, mpos.y, 0f));
|
||||
}
|
||||
#else
|
||||
// Input System package not yet enabled/installed. Provide a helpful runtime message.
|
||||
// This branch ensures the script compiles until the package is installed and Player Settings are set.
|
||||
if (Application.isPlaying)
|
||||
{
|
||||
Debug.LogError("PlayerTouchController: New Input System is not enabled. Install 'com.unity.inputsystem' and set Active Input Handling to 'Input System Package' (or Both) in Player Settings, then restart the Editor.");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void SetTargetFromScreenPoint(Vector3 screenPoint)
|
||||
{
|
||||
Camera cam = Camera.main;
|
||||
if (cam == null)
|
||||
{
|
||||
Debug.LogWarning("PlayerTouchController: No Camera.main found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert screen point to world point at the player's depth
|
||||
Vector3 worldPoint = cam.ScreenToWorldPoint(new Vector3(screenPoint.x, screenPoint.y, cam.WorldToScreenPoint(transform.position).z));
|
||||
// For 2D top-down games using orthographic camera, z will be player's z already.
|
||||
targetPosition = worldPoint;
|
||||
hasTarget = true;
|
||||
}
|
||||
|
||||
void MoveTowardsTarget()
|
||||
{
|
||||
// Keep original y (or z for 2D) depending on scene setup. We assume the player moves on the plane defined by its current transform.
|
||||
|
||||
Reference in New Issue
Block a user