73 lines
2.4 KiB
C#
73 lines
2.4 KiB
C#
using UnityEngine;
|
||
using UnityEngine.InputSystem;
|
||
|
||
public class MenuActivating : MonoBehaviour
|
||
{
|
||
[Header("Wiring")]
|
||
[SerializeField] private InputActionReference openMenuAction;
|
||
[SerializeField] private GameObject menuRoot; // can start inactive
|
||
[SerializeField] private MainMenuController menuController; // on menuRoot (can be null; we’ll fetch)
|
||
|
||
[Header("Behavior")]
|
||
[SerializeField] private bool deactivateOnClose = false;
|
||
|
||
private void OnEnable()
|
||
{
|
||
if (openMenuAction == null)
|
||
{
|
||
Debug.LogError("[MenuActivating] openMenuAction is NULL.");
|
||
return;
|
||
}
|
||
|
||
var a = openMenuAction.action;
|
||
Debug.Log($"[MenuActivating] Bind -> Action:{a.name} Map:{a.actionMap?.name} Interactions:{a.interactions}");
|
||
a.actionMap?.Enable();
|
||
a.performed += OnOpenMenu; // <-- use performed (works with Press / Release Only)
|
||
a.Enable();
|
||
Debug.Log($"[MenuActivating] After enable -> MapEnabled:{a.actionMap?.enabled} ActionEnabled:{a.enabled}");
|
||
}
|
||
|
||
private void OnDisable()
|
||
{
|
||
if (openMenuAction == null) return;
|
||
var a = openMenuAction.action;
|
||
a.performed -= OnOpenMenu; // match subscription
|
||
a.Disable();
|
||
}
|
||
|
||
private void OnOpenMenu(InputAction.CallbackContext _)
|
||
{
|
||
// make sure we have the controller (menu may have been inactive at assign time)
|
||
if (!menuController && menuRoot) menuController = menuRoot.GetComponent<MainMenuController>();
|
||
|
||
Debug.Log($"[MenuActivating] Toggle pressed. menuRoot.activeSelf={menuRoot?.activeSelf}");
|
||
|
||
if (!menuRoot)
|
||
{
|
||
Debug.LogError("[MenuActivating] menuRoot is NULL. Assign it in the inspector.");
|
||
return;
|
||
}
|
||
|
||
if (!menuRoot.activeSelf)
|
||
{
|
||
// wake it and make it visible
|
||
menuRoot.SetActive(true);
|
||
if (menuController) menuController.OpenMenu();
|
||
else Debug.LogWarning("[MenuActivating] MainMenuController missing; menu may still be alpha=0.");
|
||
}
|
||
else
|
||
{
|
||
if (menuController && menuController.enabled)
|
||
{
|
||
menuController.CloseMenu();
|
||
if (deactivateOnClose) menuRoot.SetActive(false);
|
||
}
|
||
else
|
||
{
|
||
// fallback: just toggle SetActive if controller isn’t there
|
||
menuRoot.SetActive(false);
|
||
}
|
||
}
|
||
}
|
||
}
|