| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243 |
- using UnityEngine;
- using UnityEngine.UIElements;
- /// <summary>
- /// Helper component to ensure the shop system is properly configured
- /// Add this to any GameObject in the MainTeamSelectScene to automatically setup the shop
- /// </summary>
- public class ShopSystemSetup : MonoBehaviour
- {
- [Header("Shop Configuration")]
- [Tooltip("The name of the shop")]
- public string shopName = "General Store";
- [Header("Auto-Setup Options")]
- [Tooltip("Automatically create an ItemShopManager if none exists")]
- public bool autoCreateShopManager = true;
- [Tooltip("Automatically load and verify items on start")]
- public bool autoVerifyItems = true;
- [Header("Debug Options")]
- [Tooltip("Show debug information in console")]
- public bool enableDebugLogging = true;
- void Start()
- {
- if (autoCreateShopManager)
- {
- SetupShopManager();
- }
- if (autoVerifyItems)
- {
- VerifyItems();
- }
- }
- void SetupShopManager()
- {
- // Check if ItemShopManager already exists
- ItemShopManager existingManager = FindFirstObjectByType<ItemShopManager>();
- if (existingManager != null)
- {
- if (enableDebugLogging)
- Debug.Log($"ItemShopManager already exists on '{existingManager.gameObject.name}'");
- return;
- }
- // Check for old SimpleShopManager and warn
- SimpleShopManager oldManager = FindFirstObjectByType<SimpleShopManager>();
- if (oldManager != null)
- {
- Debug.LogWarning($"Found SimpleShopManager on '{oldManager.gameObject.name}'. Please use the migration tool: RPG > Migrate Shop System");
- return;
- }
- // Find a suitable GameObject to add the shop to
- GameObject shopObject = null;
- // Look for existing shop-related GameObjects
- GameObject[] allObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
- foreach (GameObject obj in allObjects)
- {
- if (obj.name.ToLower().Contains("shop"))
- {
- shopObject = obj;
- break;
- }
- }
- // If no shop object found, use this GameObject
- if (shopObject == null)
- {
- shopObject = this.gameObject;
- }
- // Add ItemShopManager if it doesn't exist
- ItemShopManager shopManager = shopObject.GetComponent<ItemShopManager>();
- if (shopManager == null)
- {
- shopManager = shopObject.AddComponent<ItemShopManager>();
- shopManager.shopName = shopName;
- if (enableDebugLogging)
- Debug.Log($"Created ItemShopManager on '{shopObject.name}'");
- }
- // Ensure UIDocument exists
- UIDocument uiDocument = shopObject.GetComponent<UIDocument>();
- if (uiDocument == null)
- {
- uiDocument = shopObject.AddComponent<UIDocument>();
- if (enableDebugLogging)
- Debug.Log($"Added UIDocument component to '{shopObject.name}'");
- }
- // Try to find and assign ShopUI.uxml
- if (uiDocument.visualTreeAsset == null)
- {
- var shopUI = Resources.Load<VisualTreeAsset>("UI/ShopUI");
- if (shopUI == null)
- {
- // Try alternative paths
- string[] possiblePaths = {
- "ShopUI",
- "UI/ShopUI",
- "TeamSelectOverview/ShopUI"
- };
- foreach (string path in possiblePaths)
- {
- shopUI = Resources.Load<VisualTreeAsset>(path);
- if (shopUI != null)
- {
- if (enableDebugLogging)
- Debug.Log($"Found ShopUI at path: {path}");
- break;
- }
- }
- }
- if (shopUI != null)
- {
- uiDocument.visualTreeAsset = shopUI;
- if (enableDebugLogging)
- Debug.Log("Assigned ShopUI.uxml to UIDocument");
- }
- else
- {
- Debug.LogWarning("Could not find ShopUI.uxml. Please assign it manually to the UIDocument component.");
- }
- }
- }
- void VerifyItems()
- {
- // Load all items from Resources
- Item[] items = Resources.LoadAll<Item>("Items");
- if (items.Length == 0)
- {
- if (enableDebugLogging)
- {
- Debug.LogWarning("No Item ScriptableObjects found in Resources/Items folder.");
- Debug.Log("To create sample items, use: RPG > Create Sample Items");
- Debug.Log("Or create your own items using: Create > RPG > Items");
- }
- }
- else
- {
- if (enableDebugLogging)
- {
- Debug.Log($"Found {items.Length} items for the shop:");
- foreach (Item item in items)
- {
- Debug.Log($" - {item.itemName} ({item.itemType}) - {item.GetCostString()}");
- }
- }
- }
- // Check for items in the direct Resources folder as well
- Item[] directItems = Resources.LoadAll<Item>("");
- int additionalItems = 0;
- foreach (Item item in directItems)
- {
- bool alreadyCounted = false;
- foreach (Item folderItem in items)
- {
- if (item == folderItem)
- {
- alreadyCounted = true;
- break;
- }
- }
- if (!alreadyCounted)
- {
- additionalItems++;
- if (enableDebugLogging)
- Debug.Log($" - {item.itemName} (found in Resources root)");
- }
- }
- if (additionalItems > 0 && enableDebugLogging)
- {
- Debug.Log($"Also found {additionalItems} additional items in Resources root folder");
- }
- }
- [ContextMenu("Force Setup Shop")]
- public void ForceSetupShop()
- {
- SetupShopManager();
- }
- [ContextMenu("Verify Items")]
- public void ForceVerifyItems()
- {
- VerifyItems();
- }
- [ContextMenu("Full Shop Verification")]
- public void FullVerification()
- {
- Debug.Log("=== Shop System Full Verification ===");
- SetupShopManager();
- VerifyItems();
- // Additional checks
- ItemShopManager shopManager = FindFirstObjectByType<ItemShopManager>();
- if (shopManager != null)
- {
- UIDocument uiDoc = shopManager.GetComponent<UIDocument>();
- if (uiDoc != null && uiDoc.visualTreeAsset != null)
- {
- Debug.Log("✓ Shop system appears to be fully configured");
- }
- else
- {
- Debug.LogWarning("⚠ Shop system needs UI configuration");
- }
- }
- else
- {
- Debug.LogError("✗ No ItemShopManager found");
- }
- Debug.Log("=== Verification Complete ===");
- }
- // Cleanup method to prevent interference
- void OnValidate()
- {
- // Ensure this component doesn't interfere with the actual shop
- if (string.IsNullOrEmpty(shopName))
- {
- shopName = "General Store";
- }
- }
- }
|