Skip to content

Commit 19cb9dc

Browse files
CHANGE: Override UGUI EventSystem with InputSystemUIInputModule (#1902)
* Override InputModule component of EventSystem * Make it override only when ISX is enabled and IM disabled InputSystem should override the input module for the `EventSystem` game object only when it's the only input system available. If IM (Input Manager) is available as well or is the only input backend available, we fallback to default behavior of adding `StandaloneInputModule`. * Use static ObjectFactory.AddComponent * Test that UGUI EventSystem is created correctly This test assumes that the Input System package is enabled in the project. In cases like this, the EventSystem GameObject will always be created with the InputSystemUIInputModule. * Rely on ENABLE_INPUT_SYSTEM instead This avoids us having to use `EditorPlayerSettingsHelpers` which uses `FindObjectsOfTypeAll`. This is not allowed to be done on ScriptableObject constructor (or instance field initializer) * Update CHANGELOG --------- Co-authored-by: Lyndon Homewood <[email protected]>
1 parent c98f192 commit 19cb9dc

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#if UNITY_EDITOR && UNITY_6000_0_OR_NEWER
2+
using System;
3+
using NUnit.Framework;
4+
using UnityEditor;
5+
using UnityEditor.SceneManagement;
6+
using UnityEngine.SceneManagement;
7+
using UnityEngine;
8+
using UnityEngine.EventSystems;
9+
using UnityEngine.InputSystem.UI;
10+
11+
internal class UGUITests
12+
{
13+
Scene m_Scene;
14+
[SetUp]
15+
public void SetUp()
16+
{
17+
// Ensure that the scene is clean before starting the test
18+
m_Scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
19+
}
20+
21+
[TearDown]
22+
public void TearDown()
23+
{
24+
EditorSceneManager.CloseScene(m_Scene, true);
25+
}
26+
27+
[Test]
28+
[Category("UGUITests")]
29+
// This test checks that when the Input System is enabled the EventSystem GameObject is created with the
30+
// InputSystemUIInputModule component.
31+
public void UGUITests_Editor_EventSystemGameObjectUsesUIInputModule()
32+
{
33+
m_Scene = EditorSceneManager.NewScene(NewSceneSetup.DefaultGameObjects);
34+
35+
// Creates the EventSystem GameObject using the Editor menu
36+
string menuItem = "GameObject/UI/Event System";
37+
Assert.AreEqual(2, m_Scene.rootCount);
38+
EditorApplication.ExecuteMenuItem(menuItem);
39+
Assert.AreEqual(3, m_Scene.rootCount);
40+
41+
// Get the EventSystem GameObject from the scene to check that it has the correct input module
42+
var rootGameObjects = m_Scene.GetRootGameObjects();
43+
GameObject eventSystem = rootGameObjects[2].GetComponent<EventSystem>().gameObject;
44+
45+
Assert.IsNotNull(eventSystem);
46+
Assert.IsNotNull(eventSystem.GetComponent<InputSystemUIInputModule>());
47+
}
48+
}
49+
50+
#endif

Assets/Tests/InputSystem.Editor/UGUITests.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Packages/com.unity.inputsystem/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ however, it has to be formatted properly to pass verification tests.
1515
- NullReferenceException thrown when right-clicking an empty Action Map list in Input Actions Editor windows [ISXB-833](https://issuetracker.unity3d.com/product/unity/issues/guid/ISXB-833).
1616
- Fixed an issue where `System.ObjectDisposedException` would be thrown when deleting the last ActionMap item in the Input Actions Asset editor.
1717

18+
### Changed
19+
- For Unity 6.0 and above, when an `EventSystem` GameObject is created in the Editor it will have the
20+
`InputSystemUIInputModule` by default if the Input System package is installed and enabled.
21+
1822
## [1.8.1] - 2024-03-14
1923

2024
### Fixed

Packages/com.unity.inputsystem/InputSystem/Plugins/UI/InputSystemUIInputModuleEditor.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,25 @@
22
using System;
33
using System.Linq;
44
using UnityEditor;
5+
using UnityEditor.EventSystems;
6+
using UnityEngine.InputSystem.Editor;
57

68
////TODO: add button to automatically set up gamepad mouse cursor support
79

810
namespace UnityEngine.InputSystem.UI.Editor
911
{
1012
[CustomEditor(typeof(InputSystemUIInputModule))]
13+
[InitializeOnLoad]
1114
internal class InputSystemUIInputModuleEditor : UnityEditor.Editor
1215
{
16+
static InputSystemUIInputModuleEditor()
17+
{
18+
#if UNITY_6000_0_OR_NEWER && ENABLE_INPUT_SYSTEM
19+
InputModuleComponentFactory.SetInputModuleComponentOverride(
20+
go => ObjectFactory.AddComponent<InputSystemUIInputModule>(go));
21+
#endif
22+
}
23+
1324
private static InputActionReference GetActionReferenceFromAssets(InputActionReference[] actions, params string[] actionNames)
1425
{
1526
foreach (var actionName in actionNames)

0 commit comments

Comments
 (0)