using UnityEngine; using UnityEditor; using System.Collections.Generic; using System.Linq; using Core.Settings; namespace AppleHills.Core.Settings.Editor { public class SettingsEditorWindow : EditorWindow { private Vector2 scrollPosition; private List allSettings = new List(); private string[] tabNames = new string[] { "Player & Follower", "Interaction & Items", "Diving Minigame", "Card System", "Card Sorting", "Bird Pooper", "Statue Dressup", "Fort Fight" }; private int selectedTab = 0; private Dictionary serializedSettingsObjects = new Dictionary(); private GUIStyle headerStyle; [MenuItem("AppleHills/Settings/Settings Editor")] public static void ShowWindow() { GetWindow("Game Settings"); } private void OnEnable() { LoadAllSettings(); } private void LoadAllSettings() { allSettings.Clear(); serializedSettingsObjects.Clear(); // Find all settings assets string[] guids = AssetDatabase.FindAssets("t:BaseSettings"); foreach (string guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); BaseSettings settings = AssetDatabase.LoadAssetAtPath(path); if (settings != null) { allSettings.Add(settings); serializedSettingsObjects[settings.GetType().Name] = new SerializedObject(settings); } } // If any settings are missing, create them CreateSettingsIfMissing("PlayerFollowerSettings"); CreateSettingsIfMissing("InteractionSettings"); CreateSettingsIfMissing("DivingMinigameSettings"); CreateSettingsIfMissing("CardSystemSettings"); CreateSettingsIfMissing("CardSortingSettings"); CreateSettingsIfMissing("BirdPooperSettings"); CreateSettingsIfMissing("StatueDressupSettings"); CreateSettingsIfMissing("FortFightSettings"); } private void CreateSettingsIfMissing(string fileName) where T : BaseSettings { if (!allSettings.Any(s => s is T)) { // Check if the asset already exists string[] guids = AssetDatabase.FindAssets($"t:{typeof(T).Name}"); if (guids.Length == 0) { // Create the settings folder if it doesn't exist if (!AssetDatabase.IsValidFolder("Assets/Settings")) { AssetDatabase.CreateFolder("Assets", "Settings"); } // Create new settings asset T settings = CreateInstance(); string path = $"Assets/Settings/{fileName}.asset"; AssetDatabase.CreateAsset(settings, path); AssetDatabase.SaveAssets(); allSettings.Add(settings); serializedSettingsObjects[typeof(T).Name] = new SerializedObject(settings); Debug.Log($"Created missing settings asset: {path}"); } else { // Load existing asset string path = AssetDatabase.GUIDToAssetPath(guids[0]); T settings = AssetDatabase.LoadAssetAtPath(path); allSettings.Add(settings); serializedSettingsObjects[typeof(T).Name] = new SerializedObject(settings); } } } private void OnGUI() { if (headerStyle == null) { headerStyle = new GUIStyle(EditorStyles.boldLabel); headerStyle.fontSize = 14; headerStyle.margin = new RectOffset(0, 0, 10, 10); } EditorGUILayout.Space(10); EditorGUILayout.LabelField("Apple Hills Game Settings", headerStyle); EditorGUILayout.HelpBox("Use this window to modify game settings. Changes are saved automatically.", MessageType.Info); EditorGUILayout.Space(10); selectedTab = GUILayout.Toolbar(selectedTab, tabNames); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); switch (selectedTab) { case 0: // Player & Follower DrawSettingsEditor(); break; case 1: // Interaction & Items DrawSettingsEditor(); break; case 2: // Minigames DrawSettingsEditor(); break; case 3: // Card System DrawSettingsEditor(); break; case 4: // Card Sorting DrawSettingsEditor(); break; case 5: // Bird Pooper DrawSettingsEditor(); break; case 6: // Statue Dressup DrawSettingsEditor(); break; case 7: // Fort Fight DrawSettingsEditor(); break; } EditorGUILayout.EndScrollView(); EditorGUILayout.Space(10); EditorGUILayout.BeginHorizontal(); GUILayout.FlexibleSpace(); if (GUILayout.Button("Refresh", GUILayout.Width(100))) { LoadAllSettings(); } if (GUILayout.Button("Save All", GUILayout.Width(100))) { foreach (var serializedObj in serializedSettingsObjects.Values) { serializedObj.ApplyModifiedProperties(); EditorUtility.SetDirty(serializedObj.targetObject); } AssetDatabase.SaveAssets(); // Refresh editor settings after save AppleHills.Editor.EditorSettingsProvider.LoadAllSettings(); AppleHills.Editor.EditorSettingsProvider.RefreshSceneViews(); Debug.Log("All settings saved and editor views refreshed!"); } EditorGUILayout.EndHorizontal(); } private void DrawSettingsEditor() where T : BaseSettings { BaseSettings settings = allSettings.Find(s => s is T); if (settings == null) { EditorGUILayout.HelpBox($"No {typeof(T).Name} found. Click Refresh to create one.", MessageType.Warning); return; } SerializedObject serializedObj = serializedSettingsObjects[typeof(T).Name]; serializedObj.Update(); EditorGUILayout.Space(10); // Draw all properties SerializedProperty property = serializedObj.GetIterator(); bool enterChildren = true; while (property.NextVisible(enterChildren)) { enterChildren = false; // Skip the script field if (property.name == "m_Script") continue; EditorGUILayout.PropertyField(property, true); } // Apply changes if (serializedObj.ApplyModifiedProperties()) { EditorUtility.SetDirty(settings); } } // Helper method to highlight important fields private void DrawHighlightedProperty(SerializedProperty property, string tooltip = null) { GUI.backgroundColor = new Color(1f, 1f, 0.8f); EditorGUILayout.BeginVertical(EditorStyles.helpBox); GUI.backgroundColor = Color.white; EditorGUILayout.PropertyField(property, new GUIContent(property.displayName, tooltip)); EditorGUILayout.EndVertical(); } } }