QuickShopSetup.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. Debug.Log("✓ Converted SimpleShopManager to ItemShopManager on: " + shopObject.name);
  77. }
  78. void CreateNewShop()
  79. {
  80. // Create shop GameObject
  81. GameObject shopObject = new GameObject("ShopManager");
  82. // Add UIDocument
  83. UIDocument uiDoc = shopObject.AddComponent<UIDocument>();
  84. // Assign ShopUI.uxml
  85. AssignShopUIXML(uiDoc);
  86. // Add ItemShopManager
  87. ItemShopManager shopManager = shopObject.AddComponent<ItemShopManager>();
  88. shopManager.shopName = "General Store";
  89. Debug.Log("✓ Created new shop system on: " + shopObject.name);
  90. }
  91. void AssignShopUIXML(UIDocument uiDoc)
  92. {
  93. // Try multiple paths to find ShopUI.uxml
  94. string[] possiblePaths = {
  95. "UI/TeamSelectOverview/ShopUI",
  96. "TeamSelectOverview/ShopUI",
  97. "UI/ShopUI",
  98. "ShopUI"
  99. };
  100. VisualTreeAsset shopUI = null;
  101. string foundPath = "";
  102. foreach (string path in possiblePaths)
  103. {
  104. shopUI = Resources.Load<VisualTreeAsset>(path);
  105. if (shopUI != null)
  106. {
  107. foundPath = path;
  108. break;
  109. }
  110. }
  111. // If not found in Resources, try to load directly from Assets
  112. if (shopUI == null)
  113. {
  114. #if UNITY_EDITOR
  115. string[] guids = UnityEditor.AssetDatabase.FindAssets("ShopUI t:VisualTreeAsset");
  116. if (guids.Length > 0)
  117. {
  118. string assetPath = UnityEditor.AssetDatabase.GUIDToAssetPath(guids[0]);
  119. shopUI = UnityEditor.AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(assetPath);
  120. foundPath = assetPath;
  121. Debug.Log($"Found ShopUI.uxml at: {assetPath}");
  122. }
  123. #endif
  124. }
  125. if (shopUI != null)
  126. {
  127. uiDoc.visualTreeAsset = shopUI;
  128. Debug.Log($"✓ Assigned ShopUI.uxml from {foundPath} to UIDocument");
  129. }
  130. else
  131. {
  132. Debug.LogError("❌ Could not find ShopUI.uxml! Please assign it manually in the UIDocument component.");
  133. }
  134. }
  135. [ContextMenu("Create Sample Items")]
  136. public void CreateSampleItems()
  137. {
  138. Debug.Log("=== Creating Sample Items ===");
  139. // Check if sample items already exist
  140. Item[] existingItems = Resources.LoadAll<Item>("Items");
  141. if (existingItems.Length > 0)
  142. {
  143. Debug.Log($"Found {existingItems.Length} existing items - no need to create samples");
  144. return;
  145. }
  146. // Create sample items programmatically
  147. CreateRuntimeItems();
  148. Debug.Log("✓ Created sample items");
  149. }
  150. void CreateRuntimeItems()
  151. {
  152. // Note: These are runtime items, not asset files
  153. // For permanent items, use the editor script: RPG > Create Sample Items
  154. var sword = ScriptableObject.CreateInstance<WeaponItem>();
  155. sword.itemName = "Iron Sword";
  156. sword.description = "A sturdy iron sword";
  157. sword.goldCost = 15;
  158. sword.minDamage = 2;
  159. sword.maxDamage = 8;
  160. sword.weaponType = WeaponType.Sword;
  161. var bow = ScriptableObject.CreateInstance<WeaponItem>();
  162. bow.itemName = "Hunting Bow";
  163. bow.description = "A reliable hunting bow";
  164. bow.goldCost = 12;
  165. bow.minDamage = 1;
  166. bow.maxDamage = 6;
  167. bow.range = 150;
  168. bow.weaponType = WeaponType.Bow;
  169. var armor = ScriptableObject.CreateInstance<ArmorItem>();
  170. armor.itemName = "Leather Armor";
  171. armor.description = "Basic leather protection";
  172. armor.goldCost = 10;
  173. armor.armorClass = 1;
  174. armor.armorType = ArmorType.Light;
  175. armor.armorSlot = ArmorSlot.Chest;
  176. var potion = ScriptableObject.CreateInstance<MiscellaneousItem>();
  177. potion.itemName = "Health Potion";
  178. potion.description = "Restores health";
  179. potion.goldCost = 5;
  180. potion.isConsumable = true;
  181. potion.healthDiceCount = 1;
  182. potion.healthDiceType = 6;
  183. potion.healthBonus = 1;
  184. Debug.Log("Created runtime items: Iron Sword, Hunting Bow, Leather Armor, Health Potion");
  185. Debug.Log("Note: These are temporary runtime items. For permanent items, use RPG > Create Sample Items menu");
  186. }
  187. [ContextMenu("Test Shop")]
  188. public void TestShop()
  189. {
  190. ItemShopManager shop = FindFirstObjectByType<ItemShopManager>();
  191. if (shop == null)
  192. {
  193. Debug.LogError("No ItemShopManager found! Run Setup Shop first.");
  194. return;
  195. }
  196. // Try to find a character to test with
  197. var teamSelectScript = FindFirstObjectByType<MainTeamSelectScript>();
  198. if (teamSelectScript != null)
  199. {
  200. Debug.Log("✓ Found MainTeamSelectScript - shop should work with character selection");
  201. }
  202. else
  203. {
  204. Debug.LogWarning("⚠ No MainTeamSelectScript found - shop might not have character context");
  205. }
  206. Debug.Log("Shop test complete - try clicking on character equipment slots to open shop");
  207. }
  208. void OnValidate()
  209. {
  210. if (Application.isPlaying && autoSetupOnStart && !shopSetupComplete)
  211. {
  212. SetupShop();
  213. }
  214. }
  215. }