study/first_study/Library/PackageCache/com.unity.inputsystem@02433b2481ab/Samples~/RebindingUI/CanvasGroupModifier.cs
jh04010421 739d49f1a0 Unity | 2026.01.20
수업 실습 파일
2026-01-20 11:01:57 +09:00

52 lines
1.8 KiB
C#

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);
}
}
}
}
}