InputActionManager.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5. namespace SC.XR.Unity.Simulation {
  6. /// <summary>
  7. /// Use this class to automatically enable or disable all the inputs of type <see cref="InputAction"/>
  8. /// in a list of assets of type <see cref="InputActionAsset"/>.
  9. /// </summary>
  10. /// <remarks>
  11. /// Actions are initially disabled, meaning they do not listen/react to input yet. This class
  12. /// is used to mass enable actions so that they actively listen for input and run callbacks.
  13. /// </remarks>
  14. /// <seealso cref="InputAction"/>
  15. public class InputActionManager : MonoBehaviour
  16. {
  17. [SerializeField]
  18. [Tooltip("Input action assets to affect when inputs are enabled or disabled.")]
  19. List<InputActionAsset> m_ActionAssets;
  20. /// <summary>
  21. /// Input action assets to affect when inputs are enabled or disabled.
  22. /// </summary>
  23. public List<InputActionAsset> actionAssets
  24. {
  25. get => m_ActionAssets;
  26. set => m_ActionAssets = value ?? throw new ArgumentNullException(nameof(value));
  27. }
  28. protected void OnEnable()
  29. {
  30. EnableInput();
  31. }
  32. protected void OnDisable()
  33. {
  34. DisableInput();
  35. }
  36. /// <summary>
  37. /// Enable all actions referenced by this component.
  38. /// </summary>
  39. /// <remarks>
  40. /// This function will automatically be called when this <see cref="InputActionManager"/> component is enabled.
  41. /// However, this method can be called to enable input manually, such as after disabling it with <see cref="DisableInput"/>.
  42. /// <br />
  43. /// Note that enabling inputs will only enable the action maps contained within the referenced
  44. /// action map assets (see <see cref="actionAssets"/>).
  45. /// </remarks>
  46. /// <seealso cref="DisableInput"/>
  47. public void EnableInput()
  48. {
  49. if (m_ActionAssets == null)
  50. return;
  51. foreach (var actionAsset in m_ActionAssets)
  52. {
  53. if (actionAsset != null)
  54. {
  55. actionAsset.Enable();
  56. }
  57. }
  58. }
  59. /// <summary>
  60. /// Disable all actions referenced by this component.
  61. /// </summary>
  62. /// <remarks>
  63. /// This function will automatically be called when this <see cref="InputActionManager"/> component is disabled.
  64. /// However, this method can be called to disable input manually, such as after enabling it with <see cref="EnableInput"/>.
  65. /// <br />
  66. /// Note that disabling inputs will only disable the action maps contained within the referenced
  67. /// action map assets (see <see cref="actionAssets"/>).
  68. /// </remarks>
  69. /// <seealso cref="EnableInput"/>
  70. public void DisableInput()
  71. {
  72. if (m_ActionAssets == null)
  73. return;
  74. foreach (var actionAsset in m_ActionAssets)
  75. {
  76. if (actionAsset != null)
  77. {
  78. actionAsset.Disable();
  79. }
  80. }
  81. }
  82. }
  83. }