using UnityEngine; using UnityEditor; using System.Collections.Generic; using System.Linq; namespace AppleHills.Core.Settings.Editor { public class DeveloperSettingsEditorWindow : EditorWindow { private Vector2 scrollPosition; private List allDeveloperSettings = new List(); private string[] tabNames = new string[] { "Diving", "Debug", "Other Systems" }; // Added Debug tab private int selectedTab = 0; private Dictionary serializedSettingsObjects = new Dictionary(); private GUIStyle headerStyle; [MenuItem("AppleHills/Settings/Developer Settings Editor")] public static void ShowWindow() { GetWindow("Developer Settings"); } private void OnEnable() { LoadAllSettings(); } private void LoadAllSettings() { allDeveloperSettings.Clear(); serializedSettingsObjects.Clear(); // Find all developer settings assets string[] guids = AssetDatabase.FindAssets("t:BaseDeveloperSettings"); foreach (string guid in guids) { string path = AssetDatabase.GUIDToAssetPath(guid); BaseDeveloperSettings settings = AssetDatabase.LoadAssetAtPath(path); if (settings != null) { allDeveloperSettings.Add(settings); serializedSettingsObjects[settings.GetType().Name] = new SerializedObject(settings); } } // If any settings are missing, create them CreateSettingsIfMissing("DivingDeveloperSettings"); CreateSettingsIfMissing("DebugSettings"); // Add more developer settings types here as needed // CreateSettingsIfMissing("OtherDeveloperSettings"); } private void CreateSettingsIfMissing(string fileName) where T : BaseDeveloperSettings { if (!allDeveloperSettings.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 string settingsFolder = "Assets/Settings/Developer"; if (!AssetDatabase.IsValidFolder("Assets/Settings")) { AssetDatabase.CreateFolder("Assets", "Settings"); } if (!AssetDatabase.IsValidFolder(settingsFolder)) { AssetDatabase.CreateFolder("Assets/Settings", "Developer"); } // Create new settings asset T settings = CreateInstance(); string path = $"{settingsFolder}/{fileName}.asset"; AssetDatabase.CreateAsset(settings, path); AssetDatabase.SaveAssets(); allDeveloperSettings.Add(settings); serializedSettingsObjects[typeof(T).Name] = new SerializedObject(settings); Debug.Log($"Created missing developer settings asset: {path}"); } else { // Load existing asset string path = AssetDatabase.GUIDToAssetPath(guids[0]); T settings = AssetDatabase.LoadAssetAtPath(path); allDeveloperSettings.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 Developer Settings", headerStyle); EditorGUILayout.HelpBox("This editor is for technical settings intended for developers. For gameplay settings, use the Game Settings editor.", MessageType.Info); EditorGUILayout.Space(10); selectedTab = GUILayout.Toolbar(selectedTab, tabNames); scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition); switch (selectedTab) { case 0: // Diving DrawSettingsEditor(); break; case 1: // Debug DrawSettingsEditor(); break; case 2: // Other Systems EditorGUILayout.HelpBox("Other developer settings will appear here as they are added.", MessageType.Info); break; // Add additional cases as more developer settings types are added } 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(); Debug.Log("All developer settings saved!"); } EditorGUILayout.EndHorizontal(); } private void DrawSettingsEditor() where T : BaseDeveloperSettings { BaseDeveloperSettings settings = allDeveloperSettings.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); // Special handling for DebugSettings if (settings is DebugSettings) { DrawDebugSettingsEditor(serializedObj, settings as DebugSettings); } else { // Draw all properties for other settings types SerializedProperty property = serializedObj.GetIterator(); bool enterChildren = true; while (property.NextVisible(enterChildren)) { enterChildren = false; // Skip the script field if (property.name == "m_Script") continue; // Group headers if (property.isArray && property.propertyType == SerializedPropertyType.Generic) { EditorGUILayout.LabelField(property.displayName, EditorStyles.boldLabel); } EditorGUILayout.PropertyField(property, true); } } // Apply changes if (serializedObj.ApplyModifiedProperties()) { EditorUtility.SetDirty(settings); // Trigger OnValidate on the asset if (settings != null) { settings.OnValidate(); } } } private void DrawDebugSettingsEditor(SerializedObject serializedObj, DebugSettings debugSettings) { SerializedProperty property = serializedObj.GetIterator(); bool enterChildren = true; bool useSaveLoadSystem = debugSettings.UseSaveLoadSystem; while (property.NextVisible(enterChildren)) { enterChildren = false; // Skip the script field if (property.name == "m_Script") continue; // Check if this property should be disabled bool shouldDisable = false; if (!useSaveLoadSystem) { // Disable save-load related options when useSaveLoadSystem is false if (property.name == "autoClearSaves" || property.name == "dontSaveOnQuit") { shouldDisable = true; } } // Disable GUI for dependent fields EditorGUI.BeginDisabledGroup(shouldDisable); EditorGUILayout.PropertyField(property, true); EditorGUI.EndDisabledGroup(); } } // Helper method to highlight important fields private void DrawHighlightedProperty(SerializedProperty property, string tooltip = null) { GUI.backgroundColor = new Color(0.8f, 0.9f, 1f); // Light blue for developer settings EditorGUILayout.BeginVertical(EditorStyles.helpBox); GUI.backgroundColor = Color.white; EditorGUILayout.PropertyField(property, new GUIContent(property.displayName, tooltip)); EditorGUILayout.EndVertical(); } } }