2025-12-11 13:14:43 +01:00

73 lines
2.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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; well 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 isnt there
menuRoot.SetActive(false);
}
}
}
}