ShopSystemSetup.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. /// <summary>
  7. /// Helper component to ensure the shop system is properly configured
  8. /// Add this to any GameObject in the MainTeamSelectScene to automatically setup the shop
  9. /// </summary>
  10. public class ShopSystemSetup : MonoBehaviour
  11. {
  12. [Header("Shop Configuration")]
  13. [Tooltip("The name of the shop")]
  14. public string shopName = "General Store";
  15. [Header("Auto-Setup Options")]
  16. [Tooltip("Automatically create an ItemShopManager if none exists")]
  17. public bool autoCreateShopManager = true;
  18. [Tooltip("Automatically load and verify items on start")]
  19. public bool autoVerifyItems = true;
  20. [Header("Debug Options")]
  21. [Tooltip("Show debug information in console")]
  22. public bool enableDebugLogging = true;
  23. void Start()
  24. {
  25. if (autoCreateShopManager)
  26. {
  27. SetupShopManager();
  28. }
  29. if (autoVerifyItems)
  30. {
  31. VerifyItems();
  32. }
  33. }
  34. void SetupShopManager()
  35. {
  36. // Check if ItemShopManager already exists
  37. ItemShopManager existingManager = FindFirstObjectByType<ItemShopManager>();
  38. if (existingManager != null)
  39. {
  40. if (enableDebugLogging)
  41. Debug.Log($"ItemShopManager already exists on '{existingManager.gameObject.name}'");
  42. return;
  43. }
  44. // Check for old SimpleShopManager and warn
  45. SimpleShopManager oldManager = FindFirstObjectByType<SimpleShopManager>();
  46. if (oldManager != null)
  47. {
  48. Debug.LogWarning($"Found SimpleShopManager on '{oldManager.gameObject.name}'. Please use the migration tool: RPG > Migrate Shop System");
  49. return;
  50. }
  51. // Find a suitable GameObject to add the shop to
  52. GameObject shopObject = null;
  53. // Look for existing shop-related GameObjects
  54. GameObject[] allObjects = FindObjectsByType<GameObject>(FindObjectsSortMode.None);
  55. foreach (GameObject obj in allObjects)
  56. {
  57. if (obj.name.ToLower().Contains("shop"))
  58. {
  59. shopObject = obj;
  60. break;
  61. }
  62. }
  63. // If no shop object found, use this GameObject
  64. if (shopObject == null)
  65. {
  66. shopObject = this.gameObject;
  67. }
  68. // Add ItemShopManager if it doesn't exist
  69. ItemShopManager shopManager = shopObject.GetComponent<ItemShopManager>();
  70. if (shopManager == null)
  71. {
  72. shopManager = shopObject.AddComponent<ItemShopManager>();
  73. shopManager.shopName = shopName;
  74. if (enableDebugLogging)
  75. Debug.Log($"Created ItemShopManager on '{shopObject.name}'");
  76. }
  77. // Ensure UIDocument exists
  78. UIDocument uiDocument = shopObject.GetComponent<UIDocument>();
  79. if (uiDocument == null)
  80. {
  81. uiDocument = shopObject.AddComponent<UIDocument>();
  82. if (enableDebugLogging)
  83. Debug.Log($"Added UIDocument component to '{shopObject.name}'");
  84. }
  85. // Try to find and assign ShopUI.uxml
  86. if (uiDocument.visualTreeAsset == null)
  87. {
  88. var shopUI = Resources.Load<VisualTreeAsset>("UI/ShopUI");
  89. if (shopUI == null)
  90. {
  91. // Try alternative paths
  92. string[] possiblePaths = {
  93. "ShopUI",
  94. "UI/ShopUI",
  95. "TeamSelectOverview/ShopUI"
  96. };
  97. foreach (string path in possiblePaths)
  98. {
  99. shopUI = Resources.Load<VisualTreeAsset>(path);
  100. if (shopUI != null)
  101. {
  102. if (enableDebugLogging)
  103. Debug.Log($"Found ShopUI at path: {path}");
  104. break;
  105. }
  106. }
  107. }
  108. if (shopUI != null)
  109. {
  110. uiDocument.visualTreeAsset = shopUI;
  111. if (enableDebugLogging)
  112. Debug.Log("Assigned ShopUI.uxml to UIDocument");
  113. }
  114. else
  115. {
  116. Debug.LogWarning("Could not find ShopUI.uxml. Please assign it manually to the UIDocument component.");
  117. }
  118. }
  119. // Set up panel settings with proper sorting order
  120. SetupPanelSettings(uiDocument);
  121. }
  122. void SetupPanelSettings(UIDocument uiDoc)
  123. {
  124. // First try to find and reuse existing panel settings from other UI
  125. var existingUI = GameObject.Find("TravelUI")?.GetComponent<UIDocument>();
  126. if (existingUI?.panelSettings != null)
  127. {
  128. uiDoc.panelSettings = existingUI.panelSettings;
  129. if (enableDebugLogging)
  130. Debug.Log("✓ Panel Settings assigned from TravelUI");
  131. }
  132. else
  133. {
  134. // Try to find from MainTeamSelectScript or other UI components
  135. var mainTeamSelect = FindFirstObjectByType<MainTeamSelectScript>();
  136. if (mainTeamSelect != null)
  137. {
  138. var mainUIDoc = mainTeamSelect.GetComponent<UIDocument>();
  139. if (mainUIDoc?.panelSettings != null)
  140. {
  141. uiDoc.panelSettings = mainUIDoc.panelSettings;
  142. if (enableDebugLogging)
  143. Debug.Log("✓ Panel Settings assigned from MainTeamSelectScript");
  144. }
  145. }
  146. }
  147. // If still no panel settings, try to find any PanelSettings asset
  148. if (uiDoc.panelSettings == null)
  149. {
  150. #if UNITY_EDITOR
  151. var panelSettingsGuids = UnityEditor.AssetDatabase.FindAssets("t:PanelSettings");
  152. if (panelSettingsGuids.Length > 0)
  153. {
  154. var path = UnityEditor.AssetDatabase.GUIDToAssetPath(panelSettingsGuids[0]);
  155. var settings = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.UIElements.PanelSettings>(path);
  156. uiDoc.panelSettings = settings;
  157. if (enableDebugLogging)
  158. Debug.Log($"✓ Panel Settings auto-assigned: {settings.name}");
  159. }
  160. #endif
  161. }
  162. // Set proper sorting order (> 2 as requested)
  163. uiDoc.sortingOrder = 5; // Higher than 2 to ensure shop displays on top
  164. if (uiDoc.panelSettings != null)
  165. {
  166. // Make sure panel settings also have a high sorting order
  167. uiDoc.panelSettings.sortingOrder = 5;
  168. if (enableDebugLogging)
  169. Debug.Log($"✓ Shop UI sorting order set to {uiDoc.sortingOrder}");
  170. }
  171. else
  172. {
  173. Debug.LogWarning("⚠ Could not assign Panel Settings. You may need to assign it manually.");
  174. }
  175. }
  176. void VerifyItems()
  177. {
  178. // Load all items from Resources
  179. Item[] items = Resources.LoadAll<Item>("Items");
  180. if (items.Length == 0)
  181. {
  182. if (enableDebugLogging)
  183. {
  184. Debug.LogWarning("No Item ScriptableObjects found in Resources/Items folder.");
  185. Debug.Log("To create sample items, use: RPG > Create Sample Items");
  186. Debug.Log("Or create your own items using: Create > RPG > Items");
  187. }
  188. }
  189. else
  190. {
  191. if (enableDebugLogging)
  192. {
  193. Debug.Log($"Found {items.Length} items for the shop:");
  194. foreach (Item item in items)
  195. {
  196. Debug.Log($" - {item.itemName} ({item.itemType}) - {item.GetCostString()}");
  197. }
  198. }
  199. }
  200. // Check for items in the direct Resources folder as well
  201. Item[] directItems = Resources.LoadAll<Item>("");
  202. int additionalItems = 0;
  203. foreach (Item item in directItems)
  204. {
  205. bool alreadyCounted = false;
  206. foreach (Item folderItem in items)
  207. {
  208. if (item == folderItem)
  209. {
  210. alreadyCounted = true;
  211. break;
  212. }
  213. }
  214. if (!alreadyCounted)
  215. {
  216. additionalItems++;
  217. if (enableDebugLogging)
  218. Debug.Log($" - {item.itemName} (found in Resources root)");
  219. }
  220. }
  221. if (additionalItems > 0 && enableDebugLogging)
  222. {
  223. Debug.Log($"Also found {additionalItems} additional items in Resources root folder");
  224. }
  225. }
  226. [ContextMenu("Force Setup Shop")]
  227. public void ForceSetupShop()
  228. {
  229. SetupShopManager();
  230. }
  231. [ContextMenu("Verify Items")]
  232. public void ForceVerifyItems()
  233. {
  234. VerifyItems();
  235. }
  236. [ContextMenu("Full Shop Verification")]
  237. public void FullVerification()
  238. {
  239. Debug.Log("=== Shop System Full Verification ===");
  240. SetupShopManager();
  241. VerifyItems();
  242. // Additional checks
  243. ItemShopManager shopManager = FindFirstObjectByType<ItemShopManager>();
  244. if (shopManager != null)
  245. {
  246. UIDocument uiDoc = shopManager.GetComponent<UIDocument>();
  247. if (uiDoc != null && uiDoc.visualTreeAsset != null)
  248. {
  249. Debug.Log("✓ Shop system appears to be fully configured");
  250. }
  251. else
  252. {
  253. Debug.LogWarning("⚠ Shop system needs UI configuration");
  254. }
  255. }
  256. else
  257. {
  258. Debug.LogError("✗ No ItemShopManager found");
  259. }
  260. Debug.Log("=== Verification Complete ===");
  261. }
  262. // Cleanup method to prevent interference
  263. void OnValidate()
  264. {
  265. // Ensure this component doesn't interfere with the actual shop
  266. if (string.IsNullOrEmpty(shopName))
  267. {
  268. shopName = "General Store";
  269. }
  270. }
  271. }