Migrate settings to a more manageable structure and implement Service Locator pattern for runtime Addressables retrieval
This commit is contained in:
190
Assets/Editor/SettingsEditorWindow.cs
Normal file
190
Assets/Editor/SettingsEditorWindow.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
using UnityEngine;
|
||||
using UnityEditor;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
|
||||
namespace AppleHills.Core.Settings.Editor
|
||||
{
|
||||
public class SettingsEditorWindow : EditorWindow
|
||||
{
|
||||
private Vector2 scrollPosition;
|
||||
private List<BaseSettings> allSettings = new List<BaseSettings>();
|
||||
private string[] tabNames = new string[] { "Player & Follower", "Interaction & Items", "Minigames" };
|
||||
private int selectedTab = 0;
|
||||
private Dictionary<string, SerializedObject> serializedSettingsObjects = new Dictionary<string, SerializedObject>();
|
||||
private GUIStyle headerStyle;
|
||||
|
||||
[MenuItem("AppleHills/Settings Editor")]
|
||||
public static void ShowWindow()
|
||||
{
|
||||
GetWindow<SettingsEditorWindow>("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<BaseSettings>(path);
|
||||
if (settings != null)
|
||||
{
|
||||
allSettings.Add(settings);
|
||||
serializedSettingsObjects[settings.GetType().Name] = new SerializedObject(settings);
|
||||
}
|
||||
}
|
||||
|
||||
// If any settings are missing, create them
|
||||
CreateSettingsIfMissing<PlayerFollowerSettings>("PlayerFollowerSettings");
|
||||
CreateSettingsIfMissing<InteractionSettings>("InteractionSettings");
|
||||
CreateSettingsIfMissing<MinigameSettings>("MinigameSettings");
|
||||
}
|
||||
|
||||
private void CreateSettingsIfMissing<T>(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<T>();
|
||||
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<T>(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<PlayerFollowerSettings>();
|
||||
break;
|
||||
case 1: // Interaction & Items
|
||||
DrawSettingsEditor<InteractionSettings>();
|
||||
break;
|
||||
case 2: // Minigames
|
||||
DrawSettingsEditor<MinigameSettings>();
|
||||
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();
|
||||
Debug.Log("All settings saved!");
|
||||
}
|
||||
|
||||
EditorGUILayout.EndHorizontal();
|
||||
}
|
||||
|
||||
private void DrawSettingsEditor<T>() 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user