using System.Collections.Generic; using UnityEngine.XR.Interaction.Toolkit.Inputs.Haptics; namespace UnityEngine.XR.Interaction.Toolkit.Inputs.Readers { /// /// Utility class that provides useful methods for behaviors that use input readers. /// /// /// /// public static class XRInputReaderUtility { /// /// Helper method for setting an input property which automatically enables or disables /// directly serialized embedded input actions during Play mode. /// /// The to the field. /// The new value being set. /// The behavior with the property being set. /// /// This example demonstrates code of a MonoBehaviour that uses an input property: /// /// [SerializeField] /// XRInputHapticImpulseProvider m_HapticOutput = new XRInputHapticImpulseProvider("Haptic"); /// /// public XRInputHapticImpulseProvider hapticOutput /// { /// get => m_HapticOutput; /// set => XRInputReaderUtility.SetInputProperty(ref m_HapticOutput, value, this); /// } /// /// void OnEnable() /// { /// m_HapticOutput.EnableDirectActionIfModeUsed(); /// } /// /// void OnDisable() /// { /// m_HapticOutput.DisableDirectActionIfModeUsed(); /// } /// /// public static void SetInputProperty(ref XRInputHapticImpulseProvider property, XRInputHapticImpulseProvider value, Behaviour behavior) { if (value == null) { Debug.LogError("Setting XRInputHapticImpulseProvider property to null is disallowed and has therefore been ignored.", behavior); return; } if (Application.isPlaying) property?.DisableDirectActionIfModeUsed(); property = value; if (Application.isPlaying && behavior.isActiveAndEnabled) property.EnableDirectActionIfModeUsed(); } /// /// Helper method for setting an input property which automatically enables or disables /// directly serialized embedded input actions during Play mode. /// /// The to the field. /// The new value being set. /// The behavior with the property being set. /// /// This example demonstrates code of a MonoBehaviour that uses an input property: /// /// [SerializeField] /// XRInputButtonReader m_GrabMoveInput = new XRInputButtonReader("Grab Move"); /// /// public XRInputButtonReader grabMoveInput /// { /// get => m_GrabMoveInput; /// set => XRInputReaderUtility.SetInputProperty(ref m_GrabMoveInput, value, this); /// } /// /// void OnEnable() /// { /// m_GrabMoveInput.EnableDirectActionIfModeUsed(); /// } /// /// void OnDisable() /// { /// m_GrabMoveInput.DisableDirectActionIfModeUsed(); /// } /// /// public static void SetInputProperty(ref XRInputButtonReader property, XRInputButtonReader value, Behaviour behavior) { if (value == null) { Debug.LogError("Setting XRInputButtonReader property to null is disallowed and has therefore been ignored.", behavior); return; } if (Application.isPlaying) property?.DisableDirectActionIfModeUsed(); property = value; if (Application.isPlaying && behavior.isActiveAndEnabled) property.EnableDirectActionIfModeUsed(); } /// /// Helper method for setting an input property which automatically enables or disables /// directly serialized embedded input actions during Play mode. /// /// The to the field. /// The new value being set. /// The behavior with the property being set. /// Type of the value read by the property, such as or . /// /// This example demonstrates code of a MonoBehaviour that uses an input property: /// /// [SerializeField] /// XRInputValueReader<Vector2> m_LeftHandMoveInput = new XRInputValueReader<Vector2>("Left Hand Move"); /// /// public XRInputValueReader<Vector2> leftHandMoveInput /// { /// get => m_LeftHandMoveInput; /// set => XRInputReaderUtility.SetInputProperty(ref m_LeftHandMoveInput, value, this); /// } /// /// void OnEnable() /// { /// m_LeftHandMoveInput.EnableDirectActionIfModeUsed(); /// } /// /// void OnDisable() /// { /// m_LeftHandMoveInput.DisableDirectActionIfModeUsed(); /// } /// /// public static void SetInputProperty(ref XRInputValueReader property, XRInputValueReader value, Behaviour behavior) where TValue : struct { if (value == null) { Debug.LogError("Setting XRInputValueReader property to null is disallowed and has therefore been ignored.", behavior); return; } if (Application.isPlaying) property?.DisableDirectActionIfModeUsed(); property = value; if (Application.isPlaying && behavior.isActiveAndEnabled) property.EnableDirectActionIfModeUsed(); } internal static void SetInputProperty(ref XRInputButtonReader property, XRInputButtonReader value, Behaviour behavior, List buttonReaders) { if (value == null) { Debug.LogError("Setting XRInputButtonReader property to null is disallowed and has therefore been ignored.", behavior); return; } if (Application.isPlaying && property != null) { buttonReaders?.Remove(property); property.DisableDirectActionIfModeUsed(); } property = value; if (Application.isPlaying) { buttonReaders?.Add(property); if (behavior.isActiveAndEnabled) property.EnableDirectActionIfModeUsed(); } } internal static void SetInputProperty(ref XRInputValueReader property, XRInputValueReader value, Behaviour behavior, List valueReaders) where TValue : struct { if (value == null) { Debug.LogError("Setting XRInputValueReader property to null is disallowed and has therefore been ignored.", behavior); return; } if (Application.isPlaying && property != null) { valueReaders?.Remove(property); property.DisableDirectActionIfModeUsed(); } property = value; if (Application.isPlaying) { valueReaders?.Add(property); if (behavior.isActiveAndEnabled) property.EnableDirectActionIfModeUsed(); } } } }