Skip to content

Commit ad4b4cc

Browse files
committed
fix some event handlers that required a selectable
1 parent 66bd591 commit ad4b4cc

File tree

5 files changed

+62
-6
lines changed

5 files changed

+62
-6
lines changed

Runtime/EventHandlers/CancelHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using UnityEngine;
33
using UnityEngine.EventSystems;
4+
using UnityEngine.UI;
45

56
namespace ReactUnity.EventHandlers
67
{
8+
[RequireComponent(typeof(Selectable))]
79
public class CancelHandler : MonoBehaviour, ICancelHandler, IEventHandler
810
{
911
public event Action<BaseEventData> OnEvent = default;

Runtime/EventHandlers/KeyDownHandler.cs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using UnityEngine;
33
using UnityEngine.EventSystems;
44
using UnityEngine.UI;
5+
#if REACT_INPUT_SYSTEM
6+
using UnityEngine.InputSystem;
7+
#endif
58

69
namespace ReactUnity.EventHandlers
710
{
@@ -12,18 +15,37 @@ public class KeyDownHandler : MonoBehaviour, ISelectHandler, IDeselectHandler, I
1215

1316
private bool selected = false;
1417

15-
public void ClearListeners()
18+
#if REACT_INPUT_SYSTEM
19+
private InputAction action;
20+
21+
private void OnEnable()
1622
{
17-
OnEvent = null;
23+
if (action == null)
24+
{
25+
action = new InputAction(binding: "/*/<button>");
26+
action.performed += (ctx) =>
27+
{
28+
if (selected) OnEvent(new KeyEventData(EventSystem.current, ctx));
29+
};
30+
}
31+
action.Enable();
1832
}
1933

34+
private void OnDisable()
35+
{
36+
action?.Disable();
37+
}
38+
#endif
39+
40+
#if !REACT_INPUT_SYSTEM
2041
private void Update()
2142
{
2243
if (selected && Input.anyKeyDown)
2344
{
2445
OnEvent(new KeyEventData(EventSystem.current));
2546
}
2647
}
48+
#endif
2749

2850
public void OnSelect(BaseEventData eventData)
2951
{
@@ -34,17 +56,38 @@ public void OnDeselect(BaseEventData eventData)
3456
{
3557
selected = false;
3658
}
59+
60+
public void ClearListeners()
61+
{
62+
OnEvent = null;
63+
}
3764
}
3865

3966
public class KeyEventData : BaseEventData
4067
{
4168
public string key;
4269
public Type input;
70+
public bool inputSystem;
71+
72+
public InputAction.CallbackContext ctx;
73+
74+
75+
public KeyEventData(EventSystem eventSystem, bool inputSystem = false) : base(eventSystem)
76+
{
77+
this.inputSystem = inputSystem;
78+
79+
if (!inputSystem)
80+
{
81+
input = typeof(Input);
82+
key = Input.inputString;
83+
}
84+
}
4385

44-
public KeyEventData(EventSystem eventSystem) : base(eventSystem)
86+
public KeyEventData(EventSystem eventSystem, InputAction.CallbackContext ctx) : base(eventSystem)
4587
{
46-
input = typeof(Input);
47-
key = Input.inputString;
88+
this.inputSystem = true;
89+
this.ctx = ctx;
90+
key = ctx.control.name;
4891
}
4992
}
5093
}

Runtime/EventHandlers/MoveHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using UnityEngine;
33
using UnityEngine.EventSystems;
4+
using UnityEngine.UI;
45

56
namespace ReactUnity.EventHandlers
67
{
8+
[RequireComponent(typeof(Selectable))]
79
public class MoveHandler : MonoBehaviour, IMoveHandler, IEventHandler
810
{
911
public event Action<BaseEventData> OnEvent = default;

Runtime/EventHandlers/SubmitHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System;
22
using UnityEngine;
33
using UnityEngine.EventSystems;
4+
using UnityEngine.UI;
45

56
namespace ReactUnity.EventHandlers
67
{
8+
[RequireComponent(typeof(Selectable))]
79
public class SubmitHandler : MonoBehaviour, ISubmitHandler, IEventHandler
810
{
911
public event Action<BaseEventData> OnEvent = default;

Runtime/ReactUnity.asmdef

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
{
22
"name": "ReactUnity",
3+
"rootNamespace": "",
34
"references": [
45
"GUID:6055be8ebefd69e48b49212b09b47b2f",
5-
"GUID:68550284b645f4b9894995579f34290a"
6+
"GUID:68550284b645f4b9894995579f34290a",
7+
"GUID:75469ad4d38634e559750d17036d5f7c"
68
],
79
"includePlatforms": [],
810
"excludePlatforms": [],
@@ -16,6 +18,11 @@
1618
"name": "com.unity.vectorgraphics",
1719
"expression": "",
1820
"define": "REACT_VECTOR_GRAPHICS"
21+
},
22+
{
23+
"name": "com.unity.inputsystem",
24+
"expression": "",
25+
"define": "REACT_INPUT_SYSTEM"
1926
}
2027
],
2128
"noEngineReferences": false

0 commit comments

Comments
 (0)