diff --git a/Assets/Tests/InputSystem/Plugins/UITests.cs b/Assets/Tests/InputSystem/Plugins/UITests.cs index 288ecfd75a..d5f3c72514 100644 --- a/Assets/Tests/InputSystem/Plugins/UITests.cs +++ b/Assets/Tests/InputSystem/Plugins/UITests.cs @@ -2764,6 +2764,81 @@ public void UI_ClickDraggingMouseDoesNotAllocateGCMemory() }, Is.Not.AllocatingGCMemory()); } + [UnityTest] + [Category("UI")] + public IEnumerator UI_CanNavigateUI_WithLocalMultiPlayerRoot_Null_UsingGamepads() + { + // Setup navigation + var gamepad = InputSystem.AddDevice(); + var scene = CreateTestUI(makeSelectable: true); + + // Create actions for navigation + var asset = ScriptableObject.CreateInstance(); + var map = asset.AddActionMap("map"); + var moveAction = map.AddAction("move", type: InputActionType.Value, binding: "/leftStick"); + var submitAction = map.AddAction("submit", type: InputActionType.Button, binding: "/buttonSouth"); + + // Assign actions to the UI module + scene.uiModule.move = InputActionReference.Create(moveAction); + scene.uiModule.submit = InputActionReference.Create(submitAction); + map.Enable(); + + // Test 1: Assign localMultiPlayerRoot to a value + scene.eventSystem.playerRoot = scene.parentGameObject; + + // Initial selection + scene.eventSystem.SetSelectedGameObject(scene.leftGameObject); + yield return null; + + // Move right + Set(gamepad.leftStick, new Vector2(1, 0)); + yield return null; + + Assert.That(scene.eventSystem.currentSelectedGameObject, Is.SameAs(scene.rightGameObject),"Right navigation did not work when localMultiPlayerRoot was set"); + + // Move left + Set(gamepad.leftStick, Vector2.zero); + yield return null; + Set(gamepad.leftStick, new Vector2(-1, 0)); + yield return null; + + Assert.That(scene.eventSystem.currentSelectedGameObject, Is.SameAs(scene.leftGameObject),"Left navigation did not work when localMultiPlayerRoot was set"); + + // Reset stick position + Set(gamepad.leftStick, Vector2.zero); + yield return null; + + // Test 2: With localMultiPlayerRoot set to null + scene.eventSystem.playerRoot = null; + + // Reset selection + scene.eventSystem.SetSelectedGameObject(scene.leftGameObject); + yield return null; + + // Move right + Set(gamepad.leftStick, new Vector2(1, 0)); + yield return null; + + Assert.That(scene.eventSystem.currentSelectedGameObject, Is.SameAs(scene.rightGameObject),"Right navigation did not work when localMultiPlayerRoot was null"); + + // Move left + Set(gamepad.leftStick, Vector2.zero); + yield return null; + Set(gamepad.leftStick, new Vector2(-1, 0)); + yield return null; + + Assert.That(scene.eventSystem.currentSelectedGameObject, Is.SameAs(scene.leftGameObject),"Left navigation did not work when localMultiPlayerRoot was null"); + + // Submit + PressAndRelease(gamepad.buttonSouth); + yield return null; + + Assert.That(scene.leftChildReceiver.events, Has.Exactly(1).With.Property("type").EqualTo(EventType.Submit),"Submit event was not received when localMultiPlayerRoot was null"); + + // Checking that localMultiPlayerRoot is null + Assert.AreEqual(null, scene.uiModule.localMultiPlayerRoot); + } + [UnityTest] [Category("UI")] // Check that two players can have separate UI, and that both selections will stay active when diff --git a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/MultiplayerEventSystem.cs b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/MultiplayerEventSystem.cs index be68155123..31dceb30a7 100644 --- a/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/MultiplayerEventSystem.cs +++ b/Packages/com.unity.inputsystem/InputSystem/Plugins/UI/MultiplayerEventSystem.cs @@ -49,8 +49,6 @@ protected override void OnDisable() private void InitializePlayerRoot() { - if (m_PlayerRoot == null) return; - var inputModule = GetComponent(); if (inputModule != null) inputModule.localMultiPlayerRoot = m_PlayerRoot;