QuickShopSetup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using UnityEngine;
  2. using UnityEngine.UIElements;
  3. #if UNITY_EDITOR
  4. using UnityEditor;
  5. #endif
  6. /// <summary>
  7. /// Quick setup component to add to any GameObject in the scene to automatically configure the shop
  8. /// Just add this component to any GameObject and it will set everything up for you
  9. /// </summary>
  10. public class QuickShopSetup : MonoBehaviour
  11. {
  12. [Header("Quick Setup")]
  13. [Tooltip("Click this in play mode to automatically setup the shop")]
  14. public bool autoSetupOnStart = true;
  15. [Header("Setup Results")]
  16. [SerializeField] private bool shopSetupComplete = false;
  17. [SerializeField] private string statusMessage = "Not setup yet";
  18. /// <summary>
  19. /// Gets the current status message for debugging purposes
  20. /// </summary>
  21. public string GetStatusMessage() => statusMessage;
  22. void Start()
  23. {
  24. if (autoSetupOnStart)
  25. {
  26. SetupShop();
  27. }
  28. }
  29. [ContextMenu("Setup Shop Now")]
  30. public void SetupShop()
  31. {
  32. Debug.Log("=== Quick Shop Setup Starting ===");
  33. // Check if ItemShopManager already exists
  34. ItemShopManager existingShop = FindFirstObjectByType<ItemShopManager>();
  35. if (existingShop != null)
  36. {
  37. // Check if the UIDocument has the UXML assigned
  38. UIDocument uiDoc = existingShop.GetComponent<UIDocument>();
  39. if (uiDoc != null && uiDoc.visualTreeAsset == null)
  40. {
  41. Debug.Log("Found ItemShopManager but UIDocument needs UXML assignment...");
  42. AssignShopUIXML(uiDoc);
  43. statusMessage = "Fixed UXML assignment for existing shop";
  44. shopSetupComplete = true;
  45. return;
  46. }
  47. else if (uiDoc != null && uiDoc.visualTreeAsset != null)
  48. {
  49. statusMessage = "Shop already exists and is configured";
  50. shopSetupComplete = true;
  51. Debug.Log("✓ ItemShopManager already exists and is properly configured on: " + existingShop.gameObject.name);
  52. return;
  53. }
  54. }
  55. // Check for old SimpleShopManager
  56. SimpleShopManager oldShop = FindFirstObjectByType<SimpleShopManager>();
  57. if (oldShop != null)
  58. {
  59. Debug.Log("Found old SimpleShopManager, converting to ItemShopManager...");
  60. ConvertOldShop(oldShop);
  61. statusMessage = "Converted old shop to new system";
  62. shopSetupComplete = true;
  63. return;
  64. }
  65. // No shop exists, create new one
  66. Debug.Log("No shop found, creating new ItemShopManager...");
  67. CreateNewShop();
  68. statusMessage = "Created new shop system";
  69. shopSetupComplete = true;
  70. }
  71. void ConvertOldShop(SimpleShopManager oldShop)
  72. {
  73. GameObject shopObject = oldShop.gameObject;
  74. string shopName = oldShop.shopName;
  75. // Remove old component
  76. DestroyImmediate(oldShop);
  77. // Add new component
  78. ItemShopManager newShop = shopObject.AddComponent<ItemShopManager>();
  79. newShop.shopName = shopName;
  80. // Set up UIDocument with proper panel settings
  81. UIDocument uiDoc = shopObject.GetComponent<UIDocument>();
  82. if (uiDoc == null)
  83. {
  84. uiDoc = shopObject.AddComponent<UIDocument>();
  85. }
  86. // Assign ShopUI.uxml
  87. AssignShopUIXML(uiDoc);
  88. // Set up panel settings
  89. SetupPanelSettings(uiDoc);
  90. Debug.Log("✓ Converted SimpleShopManager to ItemShopManager on: " + shopObject.name);
  91. }
  92. void CreateNewShop()
  93. {
  94. // Create shop GameObject
  95. GameObject shopObject = new GameObject("ShopManager");
  96. // Add UIDocument
  97. UIDocument uiDoc = shopObject.AddComponent<UIDocument>();
  98. // Assign ShopUI.uxml
  99. AssignShopUIXML(uiDoc);
  100. // Assign Panel Settings and set proper sorting order
  101. SetupPanelSettings(uiDoc);
  102. // Add ItemShopManager
  103. ItemShopManager shopManager = shopObject.AddComponent<ItemShopManager>();
  104. shopManager.shopName = "General Store";
  105. Debug.Log("✓ Created new shop system on: " + shopObject.name);
  106. }
  107. void AssignShopUIXML(UIDocument uiDoc)
  108. {
  109. // Try multiple paths to find ShopUI.uxml
  110. string[] possiblePaths = {
  111. "UI/TeamSelectOverview/ShopUI",
  112. "TeamSelectOverview/ShopUI",
  113. "UI/ShopUI",
  114. "ShopUI"
  115. };
  116. VisualTreeAsset shopUI = null;
  117. string foundPath = "";
  118. foreach (string path in possiblePaths)
  119. {
  120. shopUI = Resources.Load<VisualTreeAsset>(path);
  121. if (shopUI != null)
  122. {
  123. foundPath = path;
  124. break;
  125. }
  126. }
  127. // If not found in Resources, try to load directly from Assets
  128. if (shopUI == null)
  129. {
  130. #if UNITY_EDITOR
  131. string[] guids = UnityEditor.AssetDatabase.FindAssets("ShopUI t:VisualTreeAsset");
  132. if (guids.Length > 0)
  133. {
  134. string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[0]);
  135. shopUI = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(assetPath);
  136. foundPath = assetPath;
  137. Debug.Log($"Found ShopUI.uxml at: {assetPath}");
  138. }
  139. #endif
  140. }
  141. if (shopUI != null)
  142. {
  143. uiDoc.visualTreeAsset = shopUI;
  144. Debug.Log($"✓ Assigned ShopUI.uxml from {foundPath} to UIDocument");
  145. }
  146. else
  147. {
  148. Debug.LogError("❌ Could not find ShopUI.uxml! Please assign it manually in the UIDocument component.");
  149. }
  150. }
  151. void SetupPanelSettings(UIDocument uiDoc)
  152. {
  153. // First try to find and reuse existing panel settings from other UI
  154. var existingUI = GameObject.Find("TravelUI")?.GetComponent<UIDocument>();
  155. if (existingUI?.panelSettings != null)
  156. {
  157. uiDoc.panelSettings = existingUI.panelSettings;
  158. Debug.Log("✓ Panel Settings assigned from TravelUI");
  159. }
  160. else
  161. {
  162. // Try to find from MainTeamSelectScript or other UI components
  163. var mainTeamSelect = FindFirstObjectByType<MainTeamSelectScript>();
  164. if (mainTeamSelect != null)
  165. {
  166. var mainUIDoc = mainTeamSelect.GetComponent<UIDocument>();
  167. if (mainUIDoc?.panelSettings != null)
  168. {
  169. uiDoc.panelSettings = mainUIDoc.panelSettings;
  170. Debug.Log("✓ Panel Settings assigned from MainTeamSelectScript");
  171. }
  172. }
  173. }
  174. // If still no panel settings, try to find any PanelSettings asset
  175. if (uiDoc.panelSettings == null)
  176. {
  177. #if UNITY_EDITOR
  178. var panelSettingsGuids = UnityEditor.AssetDatabase.FindAssets("t:PanelSettings");
  179. if (panelSettingsGuids.Length > 0)
  180. {
  181. var path = UnityEditor.AssetDatabase.GUIDToAssetPath(panelSettingsGuids[0]);
  182. var settings = UnityEditor.AssetDatabase.LoadAssetAtPath<UnityEngine.UIElements.PanelSettings>(path);
  183. uiDoc.panelSettings = settings;
  184. Debug.Log($"✓ Panel Settings auto-assigned: {settings.name}");
  185. }
  186. #endif
  187. }
  188. // Set proper sorting order (> 2 as requested)
  189. uiDoc.sortingOrder = 5; // Higher than 2 to ensure shop displays on top
  190. if (uiDoc.panelSettings != null)
  191. {
  192. // Make sure panel settings also have a high sorting order
  193. uiDoc.panelSettings.sortingOrder = 5;
  194. Debug.Log($"✓ Shop UI sorting order set to {uiDoc.sortingOrder}");
  195. }
  196. else
  197. {
  198. Debug.LogWarning("⚠ Could not assign Panel Settings. You may need to assign it manually.");
  199. }
  200. }
  201. [ContextMenu("Create Sample Items")]
  202. public void CreateSampleItems()
  203. {
  204. Debug.Log("=== Creating Sample Items ===");
  205. // Check if sample items already exist
  206. Item[] existingItems = Resources.LoadAll<Item>("Items");
  207. if (existingItems.Length > 0)
  208. {
  209. Debug.Log($"Found {existingItems.Length} existing items - no need to create samples");
  210. return;
  211. }
  212. // Create sample items programmatically
  213. CreateRuntimeItems();
  214. Debug.Log("✓ Created sample items");
  215. }
  216. void CreateRuntimeItems()
  217. {
  218. // Note: These are runtime items, not asset files
  219. // For permanent items, use the editor script: RPG > Create Sample Items
  220. var sword = ScriptableObject.CreateInstance<WeaponItem>();
  221. sword.itemName = "Iron Sword";
  222. sword.description = "A sturdy iron sword";
  223. sword.goldCost = 15;
  224. sword.minDamage = 2;
  225. sword.maxDamage = 8;
  226. sword.weaponType = WeaponType.Sword;
  227. var bow = ScriptableObject.CreateInstance<WeaponItem>();
  228. bow.itemName = "Hunting Bow";
  229. bow.description = "A reliable hunting bow";
  230. bow.goldCost = 12;
  231. bow.minDamage = 1;
  232. bow.maxDamage = 6;
  233. bow.range = 150;
  234. bow.weaponType = WeaponType.Bow;
  235. var armor = ScriptableObject.CreateInstance<ArmorItem>();
  236. armor.itemName = "Leather Armor";
  237. armor.description = "Basic leather protection";
  238. armor.goldCost = 10;
  239. armor.armorClass = 1;
  240. armor.armorType = ArmorType.Light;
  241. armor.armorSlot = ArmorSlot.Chest;
  242. var potion = ScriptableObject.CreateInstance<MiscellaneousItem>();
  243. potion.itemName = "Health Potion";
  244. potion.description = "Restores health";
  245. potion.goldCost = 5;
  246. potion.isConsumable = true;
  247. potion.healthDiceCount = 1;
  248. potion.healthDiceType = 6;
  249. potion.healthBonus = 1;
  250. Debug.Log("Created runtime items: Iron Sword, Hunting Bow, Leather Armor, Health Potion");
  251. Debug.Log("Note: These are temporary runtime items. For permanent items, use RPG > Create Sample Items menu");
  252. }
  253. [ContextMenu("Test Shop")]
  254. public void TestShop()
  255. {
  256. ItemShopManager shop = FindFirstObjectByType<ItemShopManager>();
  257. if (shop == null)
  258. {
  259. Debug.LogError("No ItemShopManager found! Run Setup Shop first.");
  260. return;
  261. }
  262. // Try to find a character to test with
  263. var teamSelectScript = FindFirstObjectByType<MainTeamSelectScript>();
  264. if (teamSelectScript != null)
  265. {
  266. Debug.Log("✓ Found MainTeamSelectScript - shop should work with character selection");
  267. }
  268. else
  269. {
  270. Debug.LogWarning("⚠ No MainTeamSelectScript found - shop might not have character context");
  271. }
  272. Debug.Log("Shop test complete - try clicking on character equipment slots to open shop");
  273. }
  274. void OnValidate()
  275. {
  276. if (Application.isPlaying && autoSetupOnStart && !shopSetupComplete)
  277. {
  278. SetupShop();
  279. }
  280. }
  281. }