ShopSystemSetup.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. /// <summary>
  4. /// Helper component to ensure the shop system is properly configured
  5. /// Add this to any GameObject in the MainTeamSelectScene to automatically setup the shop
  6. /// </summary>
  7. public class ShopSystemSetup : MonoBehaviour
  8. {
  9. [Header("Shop Configuration")]
  10. [Tooltip("The name of the shop")]
  11. public string shopName = "General Store";
  12. [Header("Auto-Setup Options")]
  13. [Tooltip("Automatically create an ItemShopManager if none exists")]
  14. public bool autoCreateShopManager = true;
  15. [Tooltip("Automatically load and verify items on start")]
  16. public bool autoVerifyItems = true;
  17. [Header("Debug Options")]
  18. [Tooltip("Show debug information in console")]
  19. public bool enableDebugLogging = true;
  20. void Start()
  21. {
  22. if (autoCreateShopManager)
  23. {
  24. SetupShopManager();
  25. }
  26. if (autoVerifyItems)
  27. {
  28. VerifyItems();
  29. }
  30. }
  31. void SetupShopManager()
  32. {
  33. // Check if ItemShopManager already exists
  34. ItemShopManager existingManager = FindFirstObjectByType<ItemShopManager>();
  35. if (existingManager != null)
  36. {
  37. if (enableDebugLogging)
  38. Debug.Log($"ItemShopManager already exists on '{existingManager.gameObject.name}'");
  39. return;
  40. }
  41. // Check for old SimpleShopManager and warn
  42. SimpleShopManager oldManager = FindFirstObjectByType<SimpleShopManager>();
  43. if (oldManager != null)
  44. {
  45. Debug.LogWarning($"Found SimpleShopManager on '{oldManager.gameObject.name}'. Please use the migration tool: RPG > Migrate Shop System");
  46. return;
  47. }
  48. // Find a suitable GameObject to add the shop to
  49. GameObject shopObject = null;
  50. // Look for existing shop-related GameObjects
  51. GameObject[] allObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
  52. foreach (GameObject obj in allObjects)
  53. {
  54. if (obj.name.ToLower().Contains("shop"))
  55. {
  56. shopObject = obj;
  57. break;
  58. }
  59. }
  60. // If no shop object found, use this GameObject
  61. if (shopObject == null)
  62. {
  63. shopObject = this.gameObject;
  64. }
  65. // Add ItemShopManager if it doesn't exist
  66. ItemShopManager shopManager = shopObject.GetComponent<ItemShopManager>();
  67. if (shopManager == null)
  68. {
  69. shopManager = shopObject.AddComponent<ItemShopManager>();
  70. shopManager.shopName = shopName;
  71. if (enableDebugLogging)
  72. Debug.Log($"Created ItemShopManager on '{shopObject.name}'");
  73. }
  74. // Ensure UIDocument exists
  75. UIDocument uiDocument = shopObject.GetComponent<UIDocument>();
  76. if (uiDocument == null)
  77. {
  78. uiDocument = shopObject.AddComponent<UIDocument>();
  79. if (enableDebugLogging)
  80. Debug.Log($"Added UIDocument component to '{shopObject.name}'");
  81. }
  82. // Try to find and assign ShopUI.uxml
  83. if (uiDocument.visualTreeAsset == null)
  84. {
  85. var shopUI = Resources.Load<VisualTreeAsset>("UI/ShopUI");
  86. if (shopUI == null)
  87. {
  88. // Try alternative paths
  89. string[] possiblePaths = {
  90. "ShopUI",
  91. "UI/ShopUI",
  92. "TeamSelectOverview/ShopUI"
  93. };
  94. foreach (string path in possiblePaths)
  95. {
  96. shopUI = Resources.Load<VisualTreeAsset>(path);
  97. if (shopUI != null)
  98. {
  99. if (enableDebugLogging)
  100. Debug.Log($"Found ShopUI at path: {path}");
  101. break;
  102. }
  103. }
  104. }
  105. if (shopUI != null)
  106. {
  107. uiDocument.visualTreeAsset = shopUI;
  108. if (enableDebugLogging)
  109. Debug.Log("Assigned ShopUI.uxml to UIDocument");
  110. }
  111. else
  112. {
  113. Debug.LogWarning("Could not find ShopUI.uxml. Please assign it manually to the UIDocument component.");
  114. }
  115. }
  116. }
  117. void VerifyItems()
  118. {
  119. // Load all items from Resources
  120. Item[] items = Resources.LoadAll<Item>("Items");
  121. if (items.Length == 0)
  122. {
  123. if (enableDebugLogging)
  124. {
  125. Debug.LogWarning("No Item ScriptableObjects found in Resources/Items folder.");
  126. Debug.Log("To create sample items, use: RPG > Create Sample Items");
  127. Debug.Log("Or create your own items using: Create > RPG > Items");
  128. }
  129. }
  130. else
  131. {
  132. if (enableDebugLogging)
  133. {
  134. Debug.Log($"Found {items.Length} items for the shop:");
  135. foreach (Item item in items)
  136. {
  137. Debug.Log($" - {item.itemName} ({item.itemType}) - {item.GetCostString()}");
  138. }
  139. }
  140. }
  141. // Check for items in the direct Resources folder as well
  142. Item[] directItems = Resources.LoadAll<Item>("");
  143. int additionalItems = 0;
  144. foreach (Item item in directItems)
  145. {
  146. bool alreadyCounted = false;
  147. foreach (Item folderItem in items)
  148. {
  149. if (item == folderItem)
  150. {
  151. alreadyCounted = true;
  152. break;
  153. }
  154. }
  155. if (!alreadyCounted)
  156. {
  157. additionalItems++;
  158. if (enableDebugLogging)
  159. Debug.Log($" - {item.itemName} (found in Resources root)");
  160. }
  161. }
  162. if (additionalItems > 0 && enableDebugLogging)
  163. {
  164. Debug.Log($"Also found {additionalItems} additional items in Resources root folder");
  165. }
  166. }
  167. [ContextMenu("Force Setup Shop")]
  168. public void ForceSetupShop()
  169. {
  170. SetupShopManager();
  171. }
  172. [ContextMenu("Verify Items")]
  173. public void ForceVerifyItems()
  174. {
  175. VerifyItems();
  176. }
  177. [ContextMenu("Full Shop Verification")]
  178. public void FullVerification()
  179. {
  180. Debug.Log("=== Shop System Full Verification ===");
  181. SetupShopManager();
  182. VerifyItems();
  183. // Additional checks
  184. ItemShopManager shopManager = FindFirstObjectByType<ItemShopManager>();
  185. if (shopManager != null)
  186. {
  187. UIDocument uiDoc = shopManager.GetComponent<UIDocument>();
  188. if (uiDoc != null && uiDoc.visualTreeAsset != null)
  189. {
  190. Debug.Log("✓ Shop system appears to be fully configured");
  191. }
  192. else
  193. {
  194. Debug.LogWarning("⚠ Shop system needs UI configuration");
  195. }
  196. }
  197. else
  198. {
  199. Debug.LogError("✗ No ItemShopManager found");
  200. }
  201. Debug.Log("=== Verification Complete ===");
  202. }
  203. // Cleanup method to prevent interference
  204. void OnValidate()
  205. {
  206. // Ensure this component doesn't interfere with the actual shop
  207. if (string.IsNullOrEmpty(shopName))
  208. {
  209. shopName = "General Store";
  210. }
  211. }
  212. }