DeltaWithButton.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. using UnityEngine.InputSystem.Layouts;
  4. using UnityEngine.InputSystem.Utilities;
  5. using UnityEngine.Scripting;
  6. #if UNITY_EDITOR
  7. using UnityEditor;
  8. #endif
  9. namespace Unity.RenderStreaming.Samples
  10. {
  11. using InputSystem = UnityEngine.InputSystem.InputSystem;
  12. #if UNITY_EDITOR
  13. [InitializeOnLoad]
  14. #endif
  15. [Preserve]
  16. [DisplayStringFormat("{button}+{delta}")]
  17. class DeltaWithButton : InputBindingComposite<Vector2>
  18. {
  19. #if UNITY_EDITOR
  20. static DeltaWithButton()
  21. {
  22. Initialize();
  23. }
  24. #endif
  25. [RuntimeInitializeOnLoadMethod]
  26. static void Initialize()
  27. {
  28. InputSystem.RegisterBindingComposite<DeltaWithButton>();
  29. }
  30. [InputControl(layout = "Button")] public int delta;
  31. [InputControl(layout = "Button")] public int button;
  32. public override Vector2 ReadValue(ref InputBindingCompositeContext context)
  33. {
  34. return context.ReadValueAsButton(button) ? context.ReadValue<Vector2, Vector2MagnitudeComparer>(delta) : default;
  35. }
  36. public override float EvaluateMagnitude(ref InputBindingCompositeContext context)
  37. {
  38. return ReadValue(ref context).magnitude;
  39. }
  40. }
  41. }