ShopSystemMigrator.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. // Remove the old component
  28. Object.DestroyImmediate(oldManager);
  29. // Add the new component
  30. ItemShopManager newManager = shopObject.AddComponent<ItemShopManager>();
  31. newManager.shopName = shopName;
  32. changesMade = true;
  33. }
  34. if (changesMade)
  35. {
  36. // Mark the scene as dirty so Unity knows it needs to be saved
  37. EditorSceneManager.MarkSceneDirty(scene);
  38. // Save the scene
  39. EditorSceneManager.SaveScene(scene);
  40. }
  41. }
  42. [MenuItem("RPG/Setup New Shop System")]
  43. public static void SetupNewShopSystem()
  44. {
  45. // Load the MainTeamSelectScene
  46. Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity");
  47. if (!scene.IsValid())
  48. {
  49. Debug.LogError("Could not load MainTeamSelectScene.unity");
  50. return;
  51. }
  52. // Check if there's already an ItemShopManager
  53. ItemShopManager[] existingManagers = Object.FindObjectsByType<ItemShopManager>(FindObjectsSortMode.None);
  54. if (existingManagers.Length > 0)
  55. {
  56. return;
  57. }
  58. // Find objects that might need a shop manager (look for UIDocument with shop-related names)
  59. UIDocument[] uiDocuments = Object.FindObjectsByType<UIDocument>(FindObjectsSortMode.None);
  60. GameObject shopObject = null;
  61. // Look for a GameObject that might be the shop
  62. foreach (UIDocument uiDoc in uiDocuments)
  63. {
  64. if (uiDoc.gameObject.name.ToLower().Contains("shop"))
  65. {
  66. shopObject = uiDoc.gameObject;
  67. break;
  68. }
  69. }
  70. // If no shop object found, create a new one
  71. if (shopObject == null)
  72. {
  73. shopObject = new GameObject("ShopManager");
  74. // Add UIDocument component
  75. UIDocument uiDocument = shopObject.AddComponent<UIDocument>();
  76. // Try to find ShopUI.uxml
  77. string[] guids = AssetDatabase.FindAssets("ShopUI t:VisualTreeAsset");
  78. if (guids.Length > 0)
  79. {
  80. string path = AssetDatabase.GUIDToAssetPath(guids[0]);
  81. var visualTreeAsset = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>(path);
  82. uiDocument.visualTreeAsset = visualTreeAsset;
  83. }
  84. else
  85. {
  86. Debug.LogWarning("ShopUI.uxml not found. You'll need to assign it manually to the UIDocument component.");
  87. }
  88. }
  89. // Add ItemShopManager component
  90. ItemShopManager shopManager = shopObject.AddComponent<ItemShopManager>();
  91. shopManager.shopName = "General Store";
  92. // Mark the scene as dirty and save
  93. EditorSceneManager.MarkSceneDirty(scene);
  94. EditorSceneManager.SaveScene(scene);
  95. }
  96. [MenuItem("RPG/Verify Shop Setup")]
  97. public static void VerifyShopSetup()
  98. {
  99. // Load the MainTeamSelectScene
  100. Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity");
  101. if (!scene.IsValid())
  102. {
  103. Debug.LogError("Could not load MainTeamSelectScene.unity");
  104. return;
  105. }
  106. // Check for ItemShopManager
  107. ItemShopManager[] shopManagers = Object.FindObjectsByType<ItemShopManager>(FindObjectsSortMode.None);
  108. // Check for remaining SimpleShopManager (should be 0)
  109. SimpleShopManager[] oldManagers = Object.FindObjectsByType<SimpleShopManager>(FindObjectsSortMode.None);
  110. if (oldManagers.Length > 0)
  111. {
  112. Debug.LogWarning($"Found {oldManagers.Length} SimpleShopManager components that still need migration!");
  113. }
  114. // Check for items in Resources
  115. Item[] items = Resources.LoadAll<Item>("Items");
  116. if (items.Length == 0)
  117. {
  118. Debug.LogWarning("No Item ScriptableObjects found! Run 'RPG > Create Sample Items' to create some.");
  119. }
  120. // Check UIDocument setup
  121. foreach (ItemShopManager manager in shopManagers)
  122. {
  123. UIDocument uiDoc = manager.GetComponent<UIDocument>();
  124. if (uiDoc == null)
  125. {
  126. Debug.LogWarning($"ItemShopManager on '{manager.gameObject.name}' is missing UIDocument component!");
  127. }
  128. else if (uiDoc.visualTreeAsset == null)
  129. {
  130. Debug.LogWarning($"UIDocument on '{manager.gameObject.name}' is missing Visual Tree Asset (ShopUI.uxml)!");
  131. }
  132. }
  133. }
  134. }