-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathCanvasGroupModifier.cs
More file actions
51 lines (44 loc) · 1.77 KB
/
CanvasGroupModifier.cs
File metadata and controls
51 lines (44 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using UnityEngine.EventSystems;
namespace UnityEngine.InputSystem.Samples.RebindUI
{
/// <summary>
/// Simple utility that modifies a referenced CanvasGroup while being active.
/// </summary>
public class CanvasGroupModifier : MonoBehaviour
{
[Tooltip("The Canvas Group to be modified while this component is active")]
public CanvasGroup canvasGroup;
[Tooltip("The interactable setting to use for the Canvas Group while this component is active")]
public bool interactable = false;
private bool m_SavedInteractable;
private GameObject m_SelectedObject;
private void OnEnable()
{
if (canvasGroup != null)
{
// Store selection to make sure it is not changed when switching "windows".
m_SelectedObject = EventSystem.current.currentSelectedGameObject;
// Save current setting and override
m_SavedInteractable = canvasGroup.interactable;
canvasGroup.interactable = interactable;
}
}
private void OnDisable()
{
if (canvasGroup != null)
{
// Restore previous setting.
canvasGroup.interactable = m_SavedInteractable;
// Restore previous selection.
var eventSystem = EventSystem.current;
if (eventSystem != null)
{
if (m_SelectedObject != null)
eventSystem.SetSelectedGameObject(m_SelectedObject);
else if (EventSystem.current.currentSelectedGameObject == null)
eventSystem.SetSelectedGameObject(eventSystem.firstSelectedGameObject);
}
}
}
}
}