ShopSystemMigrator.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.SceneManagement;
  4. using UnityEngine.SceneManagement;
  5. using UnityEngine.UIElements;
  6. public class ShopSystemMigrator
  7. {
  8. [MenuItem("RPG/Migrate Shop System")]
  9. public static void MigrateShopSystem()
  10. {
  11. // Load the MainTeamSelectScene
  12. Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity");
  13. if (!scene.IsValid())
  14. {
  15. Debug.LogError("Could not load MainTeamSelectScene.unity");
  16. return;
  17. }
  18. bool changesMade = false;
  19. // Find all SimpleShopManager components in the scene
  20. SimpleShopManager[] oldShopManagers = Object.FindObjectsByType<SimpleShopManager>(FindObjectsSortMode.None);
  21. foreach (SimpleShopManager oldManager in oldShopManagers)
  22. {
  23. GameObject shopObject = oldManager.gameObject;
  24. // Store the old settings
  25. string shopName = oldManager.shopName;
  26. var uiDocument = shopObject.GetComponent<UIDocument>();
  27. Debug.Log($"Converting SimpleShopManager on '{shopObject.name}' to ItemShopManager");
  28. // Remove the old component
  29. Object.DestroyImmediate(oldManager);
  30. // Add the new component
  31. ItemShopManager newManager = shopObject.AddComponent<ItemShopManager>();
  32. newManager.shopName = shopName;
  33. changesMade = true;
  34. }
  35. if (changesMade)
  36. {
  37. // Mark the scene as dirty so Unity knows it needs to be saved
  38. EditorSceneManager.MarkSceneDirty(scene);
  39. // Save the scene
  40. EditorSceneManager.SaveScene(scene);
  41. Debug.Log($"Successfully migrated {oldShopManagers.Length} SimpleShopManager(s) to ItemShopManager in MainTeamSelectScene");
  42. Debug.Log("The scene has been saved with the changes.");
  43. }
  44. else
  45. {
  46. Debug.Log("No SimpleShopManager components found in MainTeamSelectScene to migrate.");
  47. }
  48. }
  49. [MenuItem("RPG/Setup New Shop System")]
  50. public static void SetupNewShopSystem()
  51. {
  52. // Load the MainTeamSelectScene
  53. Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity");
  54. if (!scene.IsValid())
  55. {
  56. Debug.LogError("Could not load MainTeamSelectScene.unity");
  57. return;
  58. }
  59. // Check if there's already an ItemShopManager
  60. ItemShopManager[] existingManagers = Object.FindObjectsByType<ItemShopManager>(FindObjectsSortMode.None);
  61. if (existingManagers.Length > 0)
  62. {
  63. Debug.Log($"Found {existingManagers.Length} existing ItemShopManager(s) in the scene.");
  64. return;
  65. }
  66. // Find objects that might need a shop manager (look for UIDocument with shop-related names)
  67. UIDocument[] uiDocuments = Object.FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
  68. GameObject shopObject = null;
  69. // Look for a GameObject that might be the shop
  70. foreach (UIDocument uiDoc in uiDocuments)
  71. {
  72. if (uiDoc.gameObject.name.ToLower().Contains("shop"))
  73. {
  74. shopObject = uiDoc.gameObject;
  75. break;
  76. }
  77. }
  78. // If no shop object found, create a new one
  79. if (shopObject == null)
  80. {
  81. shopObject = new GameObject("ShopManager");
  82. // Add UIDocument component
  83. UIDocument uiDocument = shopObject.AddComponent<UIDocument>();
  84. // Try to find ShopUI.uxml
  85. string[] guids = AssetDatabase.FindAssets("ShopUI t:VisualTreeAsset");
  86. if (guids.Length > 0)
  87. {
  88. string path = AssetDatabase.GUIDToAssetPath(guids[0]);
  89. var visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(path);
  90. uiDocument.visualTreeAsset = visualTreeAsset;
  91. Debug.Log($"Assigned ShopUI.uxml to UIDocument: {path}");
  92. }
  93. else
  94. {
  95. Debug.LogWarning("ShopUI.uxml not found. You'll need to assign it manually to the UIDocument component.");
  96. }
  97. }
  98. // Add ItemShopManager component
  99. ItemShopManager shopManager = shopObject.AddComponent<ItemShopManager>();
  100. shopManager.shopName = "General Store";
  101. // Mark the scene as dirty and save
  102. EditorSceneManager.MarkSceneDirty(scene);
  103. EditorSceneManager.SaveScene(scene);
  104. Debug.Log($"Created ItemShopManager on '{shopObject.name}' in MainTeamSelectScene");
  105. Debug.Log("Don't forget to:");
  106. Debug.Log("1. Assign ShopUI.uxml to the UIDocument component if not already done");
  107. Debug.Log("2. Run 'RPG > Create Sample Items' to create some items to sell");
  108. Debug.Log("3. Create your own Item ScriptableObjects using the Create menu (RPG > Items)");
  109. }
  110. [MenuItem("RPG/Verify Shop Setup")]
  111. public static void VerifyShopSetup()
  112. {
  113. // Load the MainTeamSelectScene
  114. Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity");
  115. if (!scene.IsValid())
  116. {
  117. Debug.LogError("Could not load MainTeamSelectScene.unity");
  118. return;
  119. }
  120. Debug.Log("=== Shop System Verification ===");
  121. // Check for ItemShopManager
  122. ItemShopManager[] shopManagers = Object.FindObjectsByType<ItemShopManager>(FindObjectsSortMode.None);
  123. Debug.Log($"ItemShopManager components found: {shopManagers.Length}");
  124. // Check for remaining SimpleShopManager (should be 0)
  125. SimpleShopManager[] oldManagers = Object.FindObjectsByType<SimpleShopManager>(FindObjectsSortMode.None);
  126. if (oldManagers.Length > 0)
  127. {
  128. Debug.LogWarning($"Found {oldManagers.Length} SimpleShopManager components that still need migration!");
  129. }
  130. // Check for items in Resources
  131. Item[] items = Resources.LoadAll<Item>("Items");
  132. Debug.Log($"Item ScriptableObjects found in Resources/Items: {items.Length}");
  133. if (items.Length == 0)
  134. {
  135. Debug.LogWarning("No Item ScriptableObjects found! Run 'RPG > Create Sample Items' to create some.");
  136. }
  137. else
  138. {
  139. Debug.Log("Available items:");
  140. foreach (Item item in items)
  141. {
  142. Debug.Log($" - {item.itemName} ({item.itemType})");
  143. }
  144. }
  145. // Check UIDocument setup
  146. foreach (ItemShopManager manager in shopManagers)
  147. {
  148. UIDocument uiDoc = manager.GetComponent<UIDocument>();
  149. if (uiDoc == null)
  150. {
  151. Debug.LogWarning($"ItemShopManager on '{manager.gameObject.name}' is missing UIDocument component!");
  152. }
  153. else if (uiDoc.visualTreeAsset == null)
  154. {
  155. Debug.LogWarning($"UIDocument on '{manager.gameObject.name}' is missing Visual Tree Asset (ShopUI.uxml)!");
  156. }
  157. else
  158. {
  159. Debug.Log($"✓ ItemShopManager on '{manager.gameObject.name}' appears to be properly configured");
  160. }
  161. }
  162. Debug.Log("=== Verification Complete ===");
  163. }
  164. }