清理项目目录,引入 InputModule 做多平台输入适配
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
using SepCore.InputModule;
|
||||
using UnityEngine;
|
||||
using UnityEngine.EventSystems;
|
||||
|
||||
namespace SepCore.InputModule.Runtime.VirtualInput
|
||||
{
|
||||
public sealed class VirtualButtonBridge : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
|
||||
{
|
||||
[SerializeField] private InputActionId _actionId;
|
||||
[SerializeField] private InputContextId _contextId = InputContextId.GameplayExplore;
|
||||
[SerializeField] private bool _injectDeviceKind = true;
|
||||
[SerializeField] private InputModuleComponent _inputModule;
|
||||
|
||||
public void OnPointerDown(PointerEventData eventData)
|
||||
{
|
||||
Inject(InputCommandPhase.Started);
|
||||
Inject(InputCommandPhase.Performed);
|
||||
|
||||
if (_injectDeviceKind)
|
||||
{
|
||||
TryForceDeviceKind();
|
||||
}
|
||||
}
|
||||
|
||||
public void OnPointerUp(PointerEventData eventData)
|
||||
{
|
||||
Inject(InputCommandPhase.Canceled);
|
||||
}
|
||||
|
||||
private void Inject(InputCommandPhase phase)
|
||||
{
|
||||
InputModuleComponent inputModule = GetInputModule();
|
||||
if (inputModule == null || !inputModule.IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
float scalar = phase == InputCommandPhase.Performed ? 1f : 0f;
|
||||
var command = new InputCommand(
|
||||
_actionId,
|
||||
_contextId,
|
||||
phase,
|
||||
InputDeviceKind.Touch,
|
||||
Vector2.zero,
|
||||
scalar,
|
||||
Time.time);
|
||||
|
||||
inputModule.InjectCommand(command);
|
||||
}
|
||||
|
||||
private void TryForceDeviceKind()
|
||||
{
|
||||
InputModuleComponent inputModule = GetInputModule();
|
||||
if (inputModule == null || !inputModule.IsInitialized)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
inputModule.ForceDeviceKind(InputDeviceKind.Touch);
|
||||
}
|
||||
|
||||
private InputModuleComponent GetInputModule()
|
||||
{
|
||||
if (_inputModule != null)
|
||||
{
|
||||
return _inputModule;
|
||||
}
|
||||
|
||||
_inputModule = FindObjectOfType<InputModuleComponent>();
|
||||
return _inputModule;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user