QuickShopSetup.cs 11 KB

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