using System.Collections.Generic; using UnityEngine; /// /// Global click manager that handles click propagation between UI elements and the game world. /// UI components register themselves as click blockers to prevent map interaction when appropriate. /// public class ClickManager : MonoBehaviour { private static ClickManager _instance; public static ClickManager Instance { get { if (_instance == null) { // Try to find existing instance first _instance = FindFirstObjectByType(); if (_instance == null) { // Create a new ClickManager GameObject if none exists GameObject clickManagerObj = new GameObject("ClickManager"); _instance = clickManagerObj.AddComponent(); DontDestroyOnLoad(clickManagerObj); if (_instance.showDebugLogs) Debug.Log("✅ ClickManager auto-created"); } } return _instance; } } private readonly HashSet clickBlockers = new HashSet(); [Header("Debug")] public bool showDebugLogs = false; void Awake() { if (_instance == null) { _instance = this; DontDestroyOnLoad(gameObject); if (showDebugLogs) { Debug.Log("✅ ClickManager instance created"); } } else { if (showDebugLogs) { Debug.LogWarning("Multiple ClickManager instances found. Destroying this one."); } Destroy(gameObject); } } /// /// Registers a UI component that can block clicks. /// public void RegisterClickBlocker(IClickBlocker blocker) { if (blocker != null) { clickBlockers.Add(blocker); if (showDebugLogs) { Debug.Log($"📝 Registered click blocker: {blocker.GetType().Name}"); } } } /// /// Unregisters a UI component. /// public void UnregisterClickBlocker(IClickBlocker blocker) { if (blocker != null) { clickBlockers.Remove(blocker); if (showDebugLogs) { Debug.Log($"❌ Unregistered click blocker: {blocker.GetType().Name}"); } } } /// /// Checks if any registered UI component is blocking clicks at the given screen position. /// public bool IsClickBlocked(Vector2 screenPosition) { foreach (var blocker in clickBlockers) { if (blocker != null && blocker.IsBlockingClick(screenPosition)) { if (showDebugLogs) { Debug.Log($"🚫 Click blocked by: {blocker.GetType().Name} at position {screenPosition}"); } return true; } } if (showDebugLogs) { Debug.Log($"✅ Click allowed at position {screenPosition} (checked {clickBlockers.Count} blockers)"); } return false; } /// /// Gets the number of currently registered click blockers (for debugging) /// public int GetRegisteredBlockersCount() { return clickBlockers.Count; } /// /// Lists all registered click blockers (for debugging) /// public void ListRegisteredBlockers() { Debug.Log($"📋 Registered Click Blockers ({clickBlockers.Count}):"); foreach (var blocker in clickBlockers) { Debug.Log($" - {blocker?.GetType().Name ?? "NULL"}"); } } }