Skip to content

Commit 980abc0

Browse files
committed
Fixed a bug where selection wouldn't be restored if cancelling rebinding overlay with a gamepad
1 parent baf6180 commit 980abc0

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Assets/Samples/RebindingUI/CanvasGroupModifier.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using UnityEngine.EventSystems;
2+
13
namespace UnityEngine.InputSystem.Samples.RebindUI
24
{
35
/// <summary>
@@ -12,11 +14,15 @@ public class CanvasGroupModifier : MonoBehaviour
1214
public bool interactable = false;
1315

1416
private bool m_SavedInteractable;
17+
private GameObject m_SelectedObject;
1518

1619
void OnEnable()
1720
{
1821
if (canvasGroup != null)
1922
{
23+
// Store selection to make sure it is not changed when switching "windows".
24+
m_SelectedObject = EventSystem.current.currentSelectedGameObject;
25+
2026
// Save current setting and override
2127
m_SavedInteractable = canvasGroup.interactable;
2228
canvasGroup.interactable = interactable;
@@ -27,8 +33,14 @@ void OnDisable()
2733
{
2834
if (canvasGroup != null)
2935
{
30-
// Restore previous setting
36+
// Restore previous setting.
3137
canvasGroup.interactable = m_SavedInteractable;
38+
39+
// Restore previous selection.
40+
if (m_SelectedObject != null)
41+
EventSystem.current.SetSelectedGameObject(m_SelectedObject);
42+
else if (EventSystem.current.currentSelectedGameObject == null)
43+
EventSystem.current.SetSelectedGameObject(EventSystem.current.firstSelectedGameObject);
3244
}
3345
}
3446
}

0 commit comments

Comments
 (0)