using UnityEngine; using UnityEditor; using UnityEditor.SceneManagement; using UnityEngine.SceneManagement; using UnityEngine.UIElements; public class ShopSystemMigrator { [MenuItem("RPG/Migrate Shop System")] public static void MigrateShopSystem() { // Load the MainTeamSelectScene Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity"); if (!scene.IsValid()) { Debug.LogError("Could not load MainTeamSelectScene.unity"); return; } bool changesMade = false; // Find all SimpleShopManager components in the scene SimpleShopManager[] oldShopManagers = Object.FindObjectsByType(FindObjectsSortMode.None); foreach (SimpleShopManager oldManager in oldShopManagers) { GameObject shopObject = oldManager.gameObject; // Store the old settings string shopName = oldManager.shopName; var uiDocument = shopObject.GetComponent(); // Remove the old component Object.DestroyImmediate(oldManager); // Add the new component ItemShopManager newManager = shopObject.AddComponent(); newManager.shopName = shopName; changesMade = true; } if (changesMade) { // Mark the scene as dirty so Unity knows it needs to be saved EditorSceneManager.MarkSceneDirty(scene); // Save the scene EditorSceneManager.SaveScene(scene); } } [MenuItem("RPG/Setup New Shop System")] public static void SetupNewShopSystem() { // Load the MainTeamSelectScene Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity"); if (!scene.IsValid()) { Debug.LogError("Could not load MainTeamSelectScene.unity"); return; } // Check if there's already an ItemShopManager ItemShopManager[] existingManagers = Object.FindObjectsByType(FindObjectsSortMode.None); if (existingManagers.Length > 0) { return; } // Find objects that might need a shop manager (look for UIDocument with shop-related names) UIDocument[] uiDocuments = Object.FindObjectsByType(FindObjectsSortMode.None); GameObject shopObject = null; // Look for a GameObject that might be the shop foreach (UIDocument uiDoc in uiDocuments) { if (uiDoc.gameObject.name.ToLower().Contains("shop")) { shopObject = uiDoc.gameObject; break; } } // If no shop object found, create a new one if (shopObject == null) { shopObject = new GameObject("ShopManager"); // Add UIDocument component UIDocument uiDocument = shopObject.AddComponent(); // Try to find ShopUI.uxml string[] guids = AssetDatabase.FindAssets("ShopUI t:VisualTreeAsset"); if (guids.Length > 0) { string path = AssetDatabase.GUIDToAssetPath(guids[0]); var visualTreeAsset = AssetDatabase.LoadAssetAtPath(path); uiDocument.visualTreeAsset = visualTreeAsset; } else { Debug.LogWarning("ShopUI.uxml not found. You'll need to assign it manually to the UIDocument component."); } } // Add ItemShopManager component ItemShopManager shopManager = shopObject.AddComponent(); shopManager.shopName = "General Store"; // Mark the scene as dirty and save EditorSceneManager.MarkSceneDirty(scene); EditorSceneManager.SaveScene(scene); } [MenuItem("RPG/Verify Shop Setup")] public static void VerifyShopSetup() { // Load the MainTeamSelectScene Scene scene = EditorSceneManager.OpenScene("Assets/Scenes/MainTeamSelectScene.unity"); if (!scene.IsValid()) { Debug.LogError("Could not load MainTeamSelectScene.unity"); return; } // Check for ItemShopManager ItemShopManager[] shopManagers = Object.FindObjectsByType(FindObjectsSortMode.None); // Check for remaining SimpleShopManager (should be 0) SimpleShopManager[] oldManagers = Object.FindObjectsByType(FindObjectsSortMode.None); if (oldManagers.Length > 0) { Debug.LogWarning($"Found {oldManagers.Length} SimpleShopManager components that still need migration!"); } // Check for items in Resources Item[] items = Resources.LoadAll("Items"); if (items.Length == 0) { Debug.LogWarning("No Item ScriptableObjects found! Run 'RPG > Create Sample Items' to create some."); } // Check UIDocument setup foreach (ItemShopManager manager in shopManagers) { UIDocument uiDoc = manager.GetComponent(); if (uiDoc == null) { Debug.LogWarning($"ItemShopManager on '{manager.gameObject.name}' is missing UIDocument component!"); } else if (uiDoc.visualTreeAsset == null) { Debug.LogWarning($"UIDocument on '{manager.gameObject.name}' is missing Visual Tree Asset (ShopUI.uxml)!"); } } } }