From 21a399a0e0e55412c7c401e12acf57cd845048de Mon Sep 17 00:00:00 2001 From: jur Date: Thu, 23 Apr 2026 13:43:22 +0200 Subject: [PATCH] Updated XRControllerInputRemapper. The changes made to XRControllerInputRemapperNew.cs are fully compatible with versions of the Unity Input System package prior to 1.19.0 (including versions as old as 1.0.0). --- .../Scripts/XR/XRControllerInputRemapper.cs | 38 ++++++++++++++++--- 1 file changed, 33 insertions(+), 5 deletions(-) diff --git a/Assets/com.edia.core/Runtime/Base/Scripts/XR/XRControllerInputRemapper.cs b/Assets/com.edia.core/Runtime/Base/Scripts/XR/XRControllerInputRemapper.cs index 5cb79bf6..f5e60e63 100644 --- a/Assets/com.edia.core/Runtime/Base/Scripts/XR/XRControllerInputRemapper.cs +++ b/Assets/com.edia.core/Runtime/Base/Scripts/XR/XRControllerInputRemapper.cs @@ -8,7 +8,7 @@ namespace Edia.XR { /// In order to be flexible for each Xblock, the remapping of a controller key to a method is a separate script [System.Serializable] [AddComponentMenu("EDIA/XR Remap Controller Input")] - public class XRControllerInputRemapper : MonoBehaviour { + public class XRControllerInputRemapperNew : MonoBehaviour { // TODO Allow multiple input actions to one ID // TODO Input remapping should take systems 'allowed interaction' into considiration @@ -19,13 +19,23 @@ public class ControllerInputRemap { public bool isEnabled = false; public InputActionReference inputActionSubmit; public UnityEvent methodToCall; + + internal bool isSubscribed = false; } public List Redirectors = new List(); - private void Start() { + private void OnEnable() { + foreach (ControllerInputRemap r in Redirectors) { + if (r.isEnabled) { + Subscribe(r); + } + } + } + + private void OnDisable() { foreach (ControllerInputRemap r in Redirectors) { - EnableRemapping(r.id, r.isEnabled); + Unsubscribe(r); } } @@ -43,10 +53,28 @@ public List GetControllerRemappings () { /// active public void EnableRemapping (string id, bool onOff) { int index = Redirectors.FindIndex(x => x.id == id); + if (index < 0) return; + + Redirectors[index].isEnabled = onOff; - if (onOff) Redirectors[index].inputActionSubmit.action.performed += Redirectors[index].methodToCall.Invoke; - else Redirectors[index].inputActionSubmit.action.performed -= Redirectors[index].methodToCall.Invoke; + if (onOff) Subscribe(Redirectors[index]); + else Unsubscribe(Redirectors[index]); } + private void Subscribe(ControllerInputRemap r) { + if (r.isSubscribed || r.inputActionSubmit == null || r.inputActionSubmit.action == null) return; + + r.inputActionSubmit.action.Enable(); + r.inputActionSubmit.action.performed += r.methodToCall.Invoke; + r.isSubscribed = true; + } + + private void Unsubscribe(ControllerInputRemap r) { + if (!r.isSubscribed || r.inputActionSubmit == null || r.inputActionSubmit.action == null) return; + + r.inputActionSubmit.action.performed -= r.methodToCall.Invoke; + // Note: We don't disable the action here as other components might be using the same InputActionReference + r.isSubscribed = false; + } } } \ No newline at end of file