ShopSystemSetup.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. /// <summary>
  7. /// This script helps set up the shop/inventory system.
  8. /// Attach this to a GameObject in your scene to get started with the shop system.
  9. /// </summary>
  10. public class ShopSystemSetup : MonoBehaviour
  11. {
  12. [Header("Instructions")]
  13. [TextArea(5, 10)]
  14. public string instructions = @"Setup Instructions:
  15. 1. Create a GameObject and attach SimpleShopManager script
  16. 2. Create a UI Document with ShopUI.uxml as the Visual Tree Asset
  17. 3. Attach the UI Document to the same GameObject as SimpleShopManager
  18. 4. The shop will automatically populate with default items
  19. 5. Click 'Add Weapon', 'Add Armor', or 'Add Misc' buttons in character inventory to open shop";
  20. [Header("Quick Setup")]
  21. [SerializeField] private bool createShopGameObject = false;
  22. [Header("Manual Assignment")]
  23. [SerializeField] private VisualTreeAsset shopUIAsset;
  24. [Header("Testing")]
  25. [SerializeField] private bool testShopSystem = false;
  26. [SerializeField] private bool debugShopSetup = false;
  27. [SerializeField] private bool createPanelSettings = false;
  28. void Update()
  29. {
  30. // Handle inspector button presses
  31. if (testShopSystem)
  32. {
  33. testShopSystem = false;
  34. TestShopSystem();
  35. }
  36. if (debugShopSetup)
  37. {
  38. debugShopSetup = false;
  39. DebugShopSetup();
  40. }
  41. if (createPanelSettings)
  42. {
  43. createPanelSettings = false;
  44. CreatePanelSettings();
  45. }
  46. }
  47. [ContextMenu("Create Panel Settings")]
  48. void CreatePanelSettings()
  49. {
  50. #if UNITY_EDITOR
  51. // Ensure UI directory exists
  52. if (!AssetDatabase.IsValidFolder("Assets/UI"))
  53. {
  54. AssetDatabase.CreateFolder("Assets", "UI");
  55. }
  56. // Create Panel Settings asset
  57. var panelSettings = ScriptableObject.CreateInstance<PanelSettings>();
  58. // Set default values
  59. panelSettings.themeStyleSheet = null; // You can assign a theme if you have one
  60. panelSettings.targetTexture = null; // Will render to screen
  61. panelSettings.scaleMode = PanelScaleMode.ConstantPixelSize;
  62. panelSettings.scale = 1.0f;
  63. panelSettings.referenceResolution = new Vector2Int(1920, 1080);
  64. panelSettings.screenMatchMode = PanelScreenMatchMode.MatchWidthOrHeight;
  65. panelSettings.match = 0.5f;
  66. panelSettings.sortingOrder = 10; // Set higher than main UI (which has sortingOrder 0)
  67. // Save the asset
  68. string panelSettingsPath = "Assets/UI/PanelSettings.asset";
  69. AssetDatabase.CreateAsset(panelSettings, panelSettingsPath);
  70. AssetDatabase.SaveAssets();
  71. Debug.Log($"Created Panel Settings at {panelSettingsPath}");
  72. // Assign to existing shop manager
  73. var shopManager = FindFirstObjectByType<SimpleShopManager>();
  74. if (shopManager != null)
  75. {
  76. var uiDocument = shopManager.GetComponent<UIDocument>();
  77. if (uiDocument != null)
  78. {
  79. uiDocument.panelSettings = panelSettings;
  80. Debug.Log("Assigned Panel Settings to ShopManager UIDocument!");
  81. }
  82. }
  83. #else
  84. Debug.LogError("Panel Settings can only be created in the Editor!");
  85. #endif
  86. }
  87. [ContextMenu("Test Shop UI Visibility")]
  88. void TestShopUIVisibility()
  89. {
  90. var shopManager = FindFirstObjectByType<SimpleShopManager>();
  91. if (shopManager == null)
  92. {
  93. Debug.LogError("No ShopManager found in scene!");
  94. return;
  95. }
  96. var uiDocument = shopManager.GetComponent<UIDocument>();
  97. if (uiDocument == null)
  98. {
  99. Debug.LogError("ShopManager has no UIDocument!");
  100. return;
  101. }
  102. Debug.Log("=== Shop UI Visibility Test ===");
  103. Debug.Log($"UIDocument sortingOrder: {uiDocument.sortingOrder}");
  104. if (uiDocument.panelSettings != null)
  105. {
  106. Debug.Log($"Panel Settings sortingOrder: {uiDocument.panelSettings.sortingOrder}");
  107. }
  108. else
  109. {
  110. Debug.LogWarning("No Panel Settings assigned!");
  111. }
  112. // Compare with main UI
  113. var allUIDocuments = FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
  114. Debug.Log("All UIDocuments in scene:");
  115. foreach (var doc in allUIDocuments)
  116. {
  117. var panelSort = doc.panelSettings?.sortingOrder ?? -999;
  118. Debug.Log($" '{doc.gameObject.name}' - UIDoc sortOrder: {doc.sortingOrder}, Panel sortOrder: {panelSort}");
  119. }
  120. // Try to open the shop
  121. var mainTeamScript = FindFirstObjectByType<MainTeamSelectScript>();
  122. if (mainTeamScript != null)
  123. {
  124. // Create a dummy character for testing
  125. var dummyCharacter = new TeamCharacter();
  126. dummyCharacter.name = "Test Character";
  127. shopManager.OpenShop(dummyCharacter);
  128. Debug.Log("Shop opened for testing - check if it's visible above other UI!");
  129. }
  130. else
  131. {
  132. Debug.LogWarning("No MainTeamSelectScript found - cannot test shop opening");
  133. }
  134. }
  135. [ContextMenu("Fix Panel Settings Sort Order")]
  136. void FixPanelSettingsSortOrder()
  137. {
  138. #if UNITY_EDITOR
  139. // Check if Panel Settings already exists
  140. string panelSettingsPath = "Assets/UI/PanelSettings.asset";
  141. var existingPanelSettings = AssetDatabase.LoadAssetAtPath<PanelSettings>(panelSettingsPath);
  142. if (existingPanelSettings != null)
  143. {
  144. existingPanelSettings.sortingOrder = 10; // Set higher than main UI (which has sortingOrder 0)
  145. EditorUtility.SetDirty(existingPanelSettings);
  146. AssetDatabase.SaveAssets();
  147. Debug.Log($"Updated Panel Settings sortingOrder to 10 for UI layering!");
  148. }
  149. else
  150. {
  151. Debug.LogWarning("Panel Settings not found. Run 'Create Panel Settings' first.");
  152. }
  153. // Also update any UIDocument that might be using this Panel Settings
  154. var shopManager = FindFirstObjectByType<SimpleShopManager>();
  155. if (shopManager != null)
  156. {
  157. var uiDocument = shopManager.GetComponent<UIDocument>();
  158. if (uiDocument != null && uiDocument.panelSettings != null)
  159. {
  160. uiDocument.panelSettings.sortingOrder = 10;
  161. EditorUtility.SetDirty(uiDocument.panelSettings);
  162. Debug.Log("Updated UIDocument Panel Settings sortingOrder!");
  163. }
  164. }
  165. #else
  166. Debug.LogError("Panel Settings can only be modified in the Editor!");
  167. #endif
  168. }
  169. [ContextMenu("Assign Shop UI Asset")]
  170. void AssignShopUIAsset()
  171. {
  172. var shopManager = FindFirstObjectByType<SimpleShopManager>();
  173. if (shopManager == null)
  174. {
  175. Debug.LogError("No ShopManager found in scene. Create one first.");
  176. return;
  177. }
  178. var uiDocument = shopManager.GetComponent<UIDocument>();
  179. if (uiDocument == null)
  180. {
  181. Debug.LogError("ShopManager doesn't have a UIDocument component!");
  182. return;
  183. }
  184. if (shopUIAsset != null)
  185. {
  186. uiDocument.visualTreeAsset = shopUIAsset;
  187. Debug.Log("Successfully assigned ShopUI.uxml to UIDocument!");
  188. }
  189. else
  190. {
  191. Debug.LogError("No Shop UI Asset assigned! Please drag ShopUI.uxml to the Shop UI Asset field in the inspector.");
  192. }
  193. }
  194. void Start()
  195. {
  196. if (createShopGameObject)
  197. {
  198. CreateShopGameObject();
  199. createShopGameObject = false;
  200. }
  201. // Always ensure shop system is properly set up
  202. EnsureShopSystemSetup();
  203. }
  204. void EnsureShopSystemSetup()
  205. {
  206. var shopManager = FindFirstObjectByType<SimpleShopManager>();
  207. if (shopManager == null)
  208. {
  209. Debug.Log("No ShopManager found, creating one...");
  210. CreateShopGameObject();
  211. shopManager = FindFirstObjectByType<SimpleShopManager>();
  212. }
  213. if (shopManager != null)
  214. {
  215. var uiDocument = shopManager.GetComponent<UIDocument>();
  216. if (uiDocument != null)
  217. {
  218. // Check if Panel Settings is assigned
  219. if (uiDocument.panelSettings == null)
  220. {
  221. Debug.Log("No Panel Settings assigned to ShopManager, creating one...");
  222. CreatePanelSettings();
  223. }
  224. // Check if Visual Tree Asset is assigned
  225. if (uiDocument.visualTreeAsset == null)
  226. {
  227. Debug.Log("No Visual Tree Asset assigned to ShopManager, assigning ShopUI.uxml...");
  228. AssignShopUIAsset();
  229. }
  230. }
  231. }
  232. }
  233. [ContextMenu("Create Shop GameObject")]
  234. void CreateShopGameObject()
  235. {
  236. // Check if shop manager already exists
  237. var existingShop = FindFirstObjectByType<SimpleShopManager>();
  238. if (existingShop != null)
  239. {
  240. Debug.LogWarning("ShopManager already exists in scene!");
  241. return;
  242. }
  243. // Create shop GameObject
  244. GameObject shopGO = new GameObject("ShopManager");
  245. // Add UIDocument component FIRST (before SimpleShopManager)
  246. var uiDocument = shopGO.AddComponent<UIDocument>();
  247. // Add SimpleShopManager component AFTER UIDocument
  248. var shopManager = shopGO.AddComponent<SimpleShopManager>();
  249. Debug.Log("Created ShopManager GameObject. Now looking for ShopUI.uxml...");
  250. // Try to find and assign the UXML file - try multiple potential paths
  251. VisualTreeAsset uiAsset = null;
  252. #if UNITY_EDITOR
  253. // Try to load from Assets folder directly (Editor only)
  254. uiAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/TeamSelectOverview/ShopUI.uxml");
  255. if (uiAsset != null)
  256. {
  257. Debug.Log("Found ShopUI.uxml in Assets/UI/TeamSelectOverview/");
  258. uiDocument.visualTreeAsset = uiAsset;
  259. // Try to find and assign Panel Settings
  260. var panelSettings = AssetDatabase.LoadAssetAtPath<PanelSettings>("Assets/UI/PanelSettings.asset");
  261. if (panelSettings == null)
  262. {
  263. // Try common locations for Panel Settings
  264. var panelSettingsGuids = AssetDatabase.FindAssets("t:PanelSettings");
  265. if (panelSettingsGuids.Length > 0)
  266. {
  267. var panelSettingsPath = AssetDatabase.GUIDToAssetPath(panelSettingsGuids[0]);
  268. panelSettings = AssetDatabase.LoadAssetAtPath<PanelSettings>(panelSettingsPath);
  269. Debug.Log($"Found Panel Settings at: {panelSettingsPath}");
  270. }
  271. else
  272. {
  273. // No Panel Settings found, create one
  274. Debug.Log("No Panel Settings found, creating one...");
  275. CreatePanelSettings();
  276. panelSettings = AssetDatabase.LoadAssetAtPath<PanelSettings>("Assets/UI/PanelSettings.asset");
  277. }
  278. }
  279. if (panelSettings != null)
  280. {
  281. uiDocument.panelSettings = panelSettings;
  282. // Ensure the UIDocument also has a high sortingOrder for proper layering
  283. uiDocument.sortingOrder = 10;
  284. Debug.Log("Successfully assigned Panel Settings to UIDocument with sortingOrder 10!");
  285. }
  286. else
  287. {
  288. Debug.LogError("Failed to create or find Panel Settings!");
  289. }
  290. Debug.Log("Successfully assigned ShopUI.uxml to UIDocument!");
  291. }
  292. else
  293. {
  294. // Try Resources folder
  295. uiAsset = Resources.Load<VisualTreeAsset>("UI/TeamSelectOverview/ShopUI");
  296. if (uiAsset != null)
  297. {
  298. Debug.Log("Found ShopUI.uxml in Resources folder");
  299. uiDocument.visualTreeAsset = uiAsset;
  300. Debug.Log("Successfully assigned ShopUI.uxml to UIDocument!");
  301. }
  302. else
  303. {
  304. Debug.LogError("Could not find ShopUI.uxml! Please check that the file exists at Assets/UI/TeamSelectOverview/ShopUI.uxml");
  305. Debug.LogError("Manual assignment required: Select the ShopManager GameObject and assign ShopUI.uxml to the UIDocument component.");
  306. }
  307. }
  308. #else
  309. // In build, try Resources folder
  310. uiAsset = Resources.Load<VisualTreeAsset>("UI/TeamSelectOverview/ShopUI");
  311. if (uiAsset != null)
  312. {
  313. uiDocument.visualTreeAsset = uiAsset;
  314. Debug.Log("Successfully assigned ShopUI.uxml to UIDocument!");
  315. }
  316. else
  317. {
  318. Debug.LogError("Could not find ShopUI.uxml in Resources folder!");
  319. }
  320. #endif
  321. Debug.Log("The shop is now ready to use with the inventory system!");
  322. // Select the created GameObject in the hierarchy for easy inspection
  323. #if UNITY_EDITOR
  324. UnityEditor.Selection.activeGameObject = shopGO;
  325. #endif
  326. }
  327. [ContextMenu("Test Shop System")]
  328. void TestShopSystem()
  329. {
  330. var shopManager = FindFirstObjectByType<SimpleShopManager>();
  331. if (shopManager == null)
  332. {
  333. Debug.LogError("No ShopManager found in scene. Please create one first using 'Create Shop GameObject'.");
  334. return;
  335. }
  336. Debug.Log("Testing Shop System...");
  337. // Check if UIDocument is assigned
  338. var uiDocument = shopManager.GetComponent<UIDocument>();
  339. if (uiDocument == null)
  340. {
  341. Debug.LogError("ShopManager doesn't have a UIDocument component!");
  342. return;
  343. }
  344. // Check if UXML is assigned
  345. if (uiDocument.visualTreeAsset == null)
  346. {
  347. Debug.LogError("UIDocument doesn't have a Visual Tree Asset assigned! Please assign ShopUI.uxml manually or use 'Assign Shop UI Asset' context menu.");
  348. return;
  349. }
  350. Debug.Log($"UIDocument found with asset: {uiDocument.visualTreeAsset.name}");
  351. // Check if UI elements can be found
  352. var root = uiDocument.rootVisualElement;
  353. var shopContainer = root.Q<VisualElement>("ShopContainer");
  354. Debug.Log($"ShopContainer found in root: {shopContainer != null}");
  355. if (shopContainer == null)
  356. {
  357. Debug.LogError("ShopContainer not found! This means the UXML structure might be wrong or not loaded properly.");
  358. Debug.Log("Checking root element children...");
  359. Debug.Log($"Root element has {root.childCount} children");
  360. for (int i = 0; i < root.childCount; i++)
  361. {
  362. var child = root.ElementAt(i);
  363. Debug.Log($"Child {i}: {child.name} (type: {child.GetType().Name})");
  364. }
  365. return;
  366. }
  367. // Test opening the shop
  368. var testCharacter = new TeamCharacter("Test Hero", true);
  369. testCharacter.gold = 100; // Give some starting money
  370. Debug.Log("Opening shop for test character...");
  371. shopManager.OpenShop(testCharacter);
  372. // Check if shop is actually visible
  373. Debug.Log($"Shop container display style: {shopContainer.style.display}");
  374. Debug.Log($"Shop container resolved style display: {shopContainer.resolvedStyle.display}");
  375. Debug.Log($"Shop container world bound: {shopContainer.worldBound}");
  376. Debug.Log($"Shop container local bound: {shopContainer.localBound}");
  377. Debug.Log("Shop test completed! If you don't see the shop window, check that:");
  378. Debug.Log("1. Panel Settings is assigned to the UIDocument");
  379. Debug.Log("2. No other UI is covering the shop");
  380. Debug.Log("3. The shop might be outside the camera view");
  381. }
  382. [ContextMenu("Debug Shop Setup")]
  383. void DebugShopSetup()
  384. {
  385. Debug.Log("=== SHOP SETUP DEBUG ===");
  386. var shopManager = FindFirstObjectByType<SimpleShopManager>();
  387. Debug.Log($"ShopManager found: {shopManager != null}");
  388. if (shopManager != null)
  389. {
  390. var uiDocument = shopManager.GetComponent<UIDocument>();
  391. Debug.Log($"UIDocument found: {uiDocument != null}");
  392. if (uiDocument != null)
  393. {
  394. Debug.Log($"Visual Tree Asset assigned: {uiDocument.visualTreeAsset != null}");
  395. if (uiDocument.visualTreeAsset != null)
  396. {
  397. Debug.Log($"Asset name: {uiDocument.visualTreeAsset.name}");
  398. }
  399. }
  400. }
  401. // Check if UXML file exists
  402. #if UNITY_EDITOR
  403. var uiAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/UI/TeamSelectOverview/ShopUI.uxml");
  404. Debug.Log($"ShopUI.uxml exists at expected path: {uiAsset != null}");
  405. #endif
  406. Debug.Log("=== END DEBUG ===");
  407. }
  408. }