Files
AppleHillsProduction/Assets/Editor/DeveloperSettingsEditorWindow.cs

212 lines
8.2 KiB
C#

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace AppleHills.Core.Settings.Editor
{
public class DeveloperSettingsEditorWindow : EditorWindow
{
private Vector2 scrollPosition;
private List<BaseDeveloperSettings> allDeveloperSettings = new List<BaseDeveloperSettings>();
private string[] tabNames = new string[] { "Diving", "Debug", "Other Systems" }; // Added Debug tab
private int selectedTab = 0;
private Dictionary<string, SerializedObject> serializedSettingsObjects = new Dictionary<string, SerializedObject>();
private GUIStyle headerStyle;
[MenuItem("AppleHills/Developer Settings Editor")]
public static void ShowWindow()
{
GetWindow<DeveloperSettingsEditorWindow>("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<BaseDeveloperSettings>(path);
if (settings != null)
{
allDeveloperSettings.Add(settings);
serializedSettingsObjects[settings.GetType().Name] = new SerializedObject(settings);
}
}
// If any settings are missing, create them
CreateSettingsIfMissing<DivingDeveloperSettings>("DivingDeveloperSettings");
CreateSettingsIfMissing<DebugSettings>("DebugSettings");
// Add more developer settings types here as needed
// CreateSettingsIfMissing<OtherDeveloperSettings>("OtherDeveloperSettings");
}
private void CreateSettingsIfMissing<T>(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<T>();
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<T>(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<DivingDeveloperSettings>();
break;
case 1: // Debug
DrawSettingsEditor<DebugSettings>();
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<T>() 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);
// 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;
// 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();
}
}
}
// 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();
}
}
}