using UnityEngine; using UnityEngine.UIElements; /// /// Helper script to automatically set up the Map Location Names and Legend system. /// Add this to a GameObject in MapScene2 to automatically configure all components. /// public class MapLocationSystemSetup : MonoBehaviour { [Header("Auto Setup Configuration")] public bool setupOnStart = true; public bool findExistingComponents = true; [Header("Feature Generation Settings")] public bool enableAutoGeneration = true; public bool enableDebugMode = false; [Header("Legend Settings")] public bool legendStartVisible = true; public KeyCode legendToggleKey = KeyCode.L; [Header("Name Display Settings")] public bool showSettlements = true; public bool showForests = true; public bool showLakes = true; public bool showPlains = true; public bool showMountains = true; public bool showRivers = true; [Header("References (Optional - will auto-find if not set)")] public UIDocument mapUIDocument; private void Start() { if (setupOnStart) { SetupMapLocationSystem(); } } [ContextMenu("Setup Map Location System")] public void SetupMapLocationSystem() { Debug.Log("πŸš€ Setting up Map Location Names and Legend System..."); SetupGeographicFeatureManager(); SetupMapLocationNameDisplay(); SetupMapLegendUI(); ConnectLegendToNameDisplay(); SetupClickManager(); Debug.Log("βœ… Map Location System setup complete!"); } [ContextMenu("Force Refresh All Components")] public void ForceRefreshAllComponents() { Debug.Log("πŸ”„ Force refreshing all map location components..."); // Force setup even for existing components bool originalFindExisting = findExistingComponents; findExistingComponents = false; SetupMapLocationSystem(); findExistingComponents = originalFindExisting; // Force refresh names RefreshLocationNames(); Debug.Log("βœ… Force refresh complete!"); } private void ConnectLegendToNameDisplay() { var legendUI = FindFirstObjectByType(); if (legendUI != null) { legendUI.RefreshNameDisplayConnection(); Debug.Log("πŸ”— Connected legend to name display"); } } private void SetupGeographicFeatureManager() { var existing = FindFirstObjectByType(); if (existing != null && findExistingComponents) { Debug.Log("πŸ“ Found existing GeographicFeatureManager"); ConfigureFeatureManager(existing); return; } // Create GeographicFeatureManager GameObject featureManagerGO = new GameObject("GeographicFeatureManager"); var featureManager = featureManagerGO.AddComponent(); ConfigureFeatureManager(featureManager); Debug.Log("πŸ“ Created GeographicFeatureManager"); } private void ConfigureFeatureManager(GeographicFeatureManager manager) { manager.autoGenerateFeatures = enableAutoGeneration; manager.debugMode = enableDebugMode; } private void SetupMapLocationNameDisplay() { var existing = FindFirstObjectByType(); if (existing != null && findExistingComponents) { Debug.Log("🏷️ Found existing MapLocationNameDisplay"); ConfigureNameDisplay(existing); // Ensure existing component has proper UIDocument setup var existingUIDocument = existing.GetComponent(); if (existingUIDocument != null) { SetupUIDocumentForNameDisplay(existingUIDocument); } return; } // Create MapLocationNameDisplay GameObject nameDisplayGO = new GameObject("MapLocationNameDisplay"); var nameDisplay = nameDisplayGO.AddComponent(); // Add UIDocument component for the name display var nameUIDocument = nameDisplayGO.AddComponent(); SetupUIDocumentForNameDisplay(nameUIDocument); ConfigureNameDisplay(nameDisplay); Debug.Log("🏷️ Created MapLocationNameDisplay"); } private void SetupUIDocumentForNameDisplay(UIDocument uiDocument) { uiDocument.sortingOrder = 1; // Above map terrain, below UI panels // Set PanelSettings using fallback approach SetupPanelSettings(uiDocument, "Name Display"); // Load the UXML template var nameDisplayUXML = Resources.Load("UI/MapLocationNameDisplay"); if (nameDisplayUXML != null) { uiDocument.visualTreeAsset = nameDisplayUXML; Debug.Log("βœ… Loaded UXML template for Name Display"); } else { Debug.LogWarning("Could not find MapLocationNameDisplay.uxml template in Resources/UI/"); } } private void ConfigureNameDisplay(MapLocationNameDisplay display) { // Configure the display settings display.showSettlementNames = showSettlements; display.showForestNames = showForests; display.showLakeNames = showLakes; display.showPlainNames = showPlains; display.showMountainNames = showMountains; display.showRiverNames = showRivers; } private void SetupMapLegendUI() { var existing = FindFirstObjectByType(); if (existing != null && findExistingComponents) { Debug.Log("πŸ—ΊοΈ Found existing MapSceneLegendUI"); ConfigureLegendUI(existing); // Ensure existing component has proper UIDocument setup var existingUIDocument = existing.GetComponent(); if (existingUIDocument != null) { SetupUIDocumentForLegend(existingUIDocument); } return; } // Create MapSceneLegendUI GameObject legendGO = new GameObject("MapSceneLegendUI"); var uiDocument = legendGO.AddComponent(); var legendUI = legendGO.AddComponent(); SetupUIDocumentForLegend(uiDocument); legendUI.uiDocument = uiDocument; ConfigureLegendUI(legendUI); Debug.Log("πŸ—ΊοΈ Created MapSceneLegendUI"); } private void SetupUIDocumentForLegend(UIDocument uiDocument) { // Set sorting order to be above name display but below modal UIs uiDocument.sortingOrder = 2; // Above name display (1), below other UI panels (3+) // Set PanelSettings using fallback approach SetupPanelSettings(uiDocument, "Legend UI"); // Load the UXML template for the legend var legendUXML = Resources.Load("UI/MapLegend"); if (legendUXML != null) { uiDocument.visualTreeAsset = legendUXML; Debug.Log("βœ… Loaded UXML template for Legend UI"); } else { Debug.LogWarning("Could not find MapLegend.uxml template in Resources/UI/"); } } private void ConfigureLegendUI(MapSceneLegendUI legendUI) { legendUI.startVisible = legendStartVisible; legendUI.toggleKey = legendToggleKey; } private void SetupClickManager() { // ClickManager is a singleton that creates itself, just ensure it exists var clickManager = ClickManager.Instance; if (clickManager != null) { Debug.Log("πŸ–±οΈ ClickManager ready"); } else { Debug.LogWarning("⚠️ ClickManager could not be initialized"); } } private void SetupPanelSettings(UIDocument uiDocument, string componentName) { // Try multiple fallback approaches to find PanelSettings UnityEngine.UIElements.PanelSettings panelSettings = null; // 1. Try loading from Resources (original approach) panelSettings = Resources.Load("MainSettings"); if (panelSettings != null) { uiDocument.panelSettings = panelSettings; Debug.Log($"βœ… Set Panel Settings for {componentName} from Resources"); return; } // 2. Try getting from TravelUI GameObject var travelUI = FindFirstObjectByType(); if (travelUI != null && travelUI.uiDocument != null && travelUI.uiDocument.panelSettings != null) { uiDocument.panelSettings = travelUI.uiDocument.panelSettings; Debug.Log($"βœ… Set Panel Settings for {componentName} from TravelUI"); return; } // 3. Try getting from any existing UIDocument with valid PanelSettings var allUIDocuments = FindObjectsByType(FindObjectsSortMode.None); foreach (var existingUIDoc in allUIDocuments) { if (existingUIDoc.panelSettings != null) { uiDocument.panelSettings = existingUIDoc.panelSettings; Debug.Log($"βœ… Set Panel Settings for {componentName} from existing UIDocument"); return; } } Debug.LogWarning($"⚠️ Could not find any PanelSettings for {componentName}"); } [ContextMenu("Find Map UI Document")] public void FindMapUIDocument() { var uiDocs = FindObjectsByType(FindObjectsSortMode.None); foreach (var doc in uiDocs) { if (doc.rootVisualElement != null) { mapUIDocument = doc; Debug.Log($"πŸ“„ Found UIDocument on {doc.gameObject.name}"); break; } } if (mapUIDocument == null) { Debug.LogWarning("⚠️ No suitable UIDocument found in scene"); } } [ContextMenu("Regenerate All Features")] public void RegenerateAllFeatures() { var featureManager = FindFirstObjectByType(); if (featureManager != null) { featureManager.RegenerateFeatures(); Debug.Log("πŸ”„ Regenerated geographic features"); } else { Debug.LogWarning("⚠️ GeographicFeatureManager not found"); } } [ContextMenu("Refresh Location Names")] public void RefreshLocationNames() { var nameDisplay = FindFirstObjectByType(); if (nameDisplay != null) { nameDisplay.RefreshLocationNames(); Debug.Log("πŸ”„ Refreshed location names"); } else { Debug.LogWarning("⚠️ MapLocationNameDisplay not found"); } } [ContextMenu("Test Click Blocking")] public void TestClickBlocking() { var clickManager = ClickManager.Instance; if (clickManager != null) { bool isBlocked = clickManager.IsClickBlocked(Input.mousePosition); Debug.Log($"πŸ–±οΈ Click at mouse position {Input.mousePosition} is {(isBlocked ? "BLOCKED" : "ALLOWED")}"); Debug.Log($"πŸ“Š Active blockers: {clickManager.GetRegisteredBlockersCount()}"); } else { Debug.LogWarning("⚠️ ClickManager not available"); } } }