ProjectDDD/Packages/SLUnity/Editor/SLUnityInspector.cs
2025-06-25 11:33:17 +09:00

392 lines
14 KiB (Stored with Git LFS)
C#

using System.Collections.Generic;
using System.Linq;
using Superlazy.Loader;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
namespace Superlazy
{
[CustomEditor(typeof(SLUnity))]
public class SLUnityInspector : Editor
{
private SLUnity component;
private bool sessionRoot;
private readonly Dictionary<string, bool> sessionView = new Dictionary<string, bool>();
private bool dataRoot;
private readonly Dictionary<string, bool> dataView = new Dictionary<string, bool>();
private string sessionSearch;
[MenuItem("Window/SLUnity %#W")]
public static void ShowWindow()
{
Selection.activeObject = FindFirstObjectByType<SLUnity>();
}
private void OnEnable()
{
component = target as SLUnity;
//if (component.managers != null && component.managers.Length > 0 && component.managers[0] == "All")
//{
// var behaviors = new List<Type>();
// behaviors.CreateTypeList<SLManager>();
// foreach (var type in behaviors)
// {
// useManagers.Add(type.FullName);
// }
//}
//else
//{
// foreach (var managerName in component.managers)
// {
// useManagers.Add(managerName);
// }
//}
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
var requireSave = false;
//component.Loader
//SLUnityBoot.mobileData = EditorGUILayout.Toggle("Mobile Data", SLUnityBoot.mobileData);
//if (SLUnityBoot.mobileData == false)
//{
// SLUnityBoot.dataPath = EditorGUILayout.TextField("Data Path", SLUnityBoot.dataPath);
//}
//SLUnityBoot.UpdateData();
EditorGUILayout.Separator();
//var behaviors = new List<Type>();
//behaviors.CreateTypeList<SLManager>();
//var subject = "SLManager :";
//if (component.managers != null && component.managers.Length > 0 && component.managers[0] == "All")
//{
// subject += " All";
//}
//EditorGUILayout.LabelField(subject);
//var oldCount = useManagers.Count;
//foreach (var type in behaviors)
//{
// bool old = useManagers.Contains(type.FullName);
// var use = EditorGUILayout.Toggle(type.FullName, old);
// if (use != old)
// {
// requireSave = true;
// if (use == false)
// {
// useManagers.Remove(type.FullName);
// }
// else
// {
// useManagers.Add(type.FullName);
// }
// }
//}
//if (useManagers.Count != oldCount)
//{
// if (behaviors.Count == useManagers.Count)
// {
// component.managers = new string[] { "All" };
// }
// else
// {
// component.managers = useManagers.ToArray();
// }
//}
EditorGUILayout.Separator();
ShowDataTree();
EditorGUILayout.Separator();
sessionSearch = EditorGUILayout.TextField("Search", sessionSearch);
if (sessionSearch == null || sessionSearch == string.Empty)
{
ShowSessionTree();
}
else
{
ShowSessionSearch(sessionSearch);
}
//var caches = SLGame.SessionCacheKeys;
//sessionCache = EditorGUILayout.Foldout(sessionCache, "SessionCache(" + caches.Count() + ")");
//if (sessionCache)
//{
// foreach (var key in caches)
// {
// EditorGUILayout.LabelField(key);
// }
//}
ViewPlayerPref();
if (requireSave && Application.isPlaying == false)
{
EditorUtility.SetDirty(component);
EditorSceneManager.MarkAllScenesDirty();
}
}
private void ShowSessionTree()
{
sessionRoot = EditorGUILayout.Foldout(sessionRoot, "Session");
if (sessionRoot && SLGame.Session)
{
var changed = SLEntity.Empty;
ViewTree(SLGame.Session, "", 1, sessionView, changed);
ChangeView(SLGame.Session, changed);
}
Repaint();
}
private void ShowDataTree()
{
dataRoot = EditorGUILayout.Foldout(dataRoot, "Data");
if (dataRoot && SLSystem.Data)
{
var changed = SLEntity.Empty;
ViewTree(SLSystem.Data, "", 1, dataView, changed);
ChangeView(SLSystem.Data, changed);
}
Repaint();
}
private void ViewTree(SLEntity e, string prefix, int depth, Dictionary<string, bool> view, SLEntity changed)
{
foreach (var tree in e.OrderBy(child => child.IsValue ? "B" + child.ID : "A" + child.ID))
{
var id = prefix + tree.ID;
if (view.ContainsKey(id) == false)
{
view[id] = false;
}
if (tree.IsValue == false)
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("", GUILayout.Width(10 * depth));
view[id] = EditorGUILayout.Foldout(view[id], tree.ToString());
EditorGUILayout.EndHorizontal();
if (view[id])
{
ViewTree(tree, id + ".", depth + 1, view, changed[tree.ID]);
}
}
else
{
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("", GUILayout.Width(10 * depth));
string changeValue;
if (tree.ToString() == "True")
{
changeValue = EditorGUILayout.Toggle(tree.ID, e[tree.ID]).ToString();
}
else if (tree.IsNumeric)
{
changeValue = EditorGUILayout.DoubleField(tree.ID, e[tree.ID]).ToString();
}
else
{
changeValue = EditorGUILayout.TextField(tree.ID, e[tree.ID]);
}
if (changeValue != e[tree.ID])
{
changed[tree.ID] = changeValue;
}
EditorGUILayout.EndHorizontal();
}
}
}
private void ChangeView(SLEntity e, SLEntity changed)
{
if (changed)
{
foreach (var entity in changed)
{
if (entity.IsValue == false)
{
ChangeView(e[entity.ID], entity);
}
else
{
if (e[entity.ID].IsNumeric)
{
e[entity.ID] = double.Parse(entity);
}
else if (entity == "False")
{
e[entity.ID] = false;
}
else
{
e[entity.ID] = entity;
}
}
}
}
}
private void ShowSessionSearch(string sessionSearch)
{
SessionSearch(SLGame.Session, sessionSearch, null);
}
private void SessionSearch(SLEntity session, string sessionSearch, string path)
{
foreach (var child in session)
{
if (path != null)
{
var newPath = path + "." + child.ID;
if (child.IsValue)
{
if (newPath.Contains(sessionSearch))
{
EditorGUILayout.LabelField(newPath + ": " + child);
}
}
else
{
SessionSearch(child, sessionSearch, newPath);
}
}
else
{
if (child.IsValue)
{
if (child.ID.Contains(sessionSearch))
{
EditorGUILayout.LabelField(child.ID + ": " + child);
}
}
else
{
SessionSearch(child, sessionSearch, child.ID);
}
}
}
}
#if UNITY_EDITOR_WIN
private bool viewPref;
private SLEntity pref;
private Dictionary<string, Dictionary<string, bool>> prefView = new Dictionary<string, Dictionary<string, bool>>();
private Dictionary<string, bool> prefOn = new Dictionary<string, bool>();
#endif
private void ViewPlayerPref()
{
#if UNITY_EDITOR_WIN
if (Application.platform != RuntimePlatform.WindowsEditor) return;
viewPref = EditorGUILayout.Foldout(viewPref, "PlayerPref");
if (viewPref)
{
if (GUILayout.Button("Refresh"))
{
pref = null;
}
if (pref == null)
{
MakePref();
}
foreach (var value in pref)
{
if (value.ID.IsLeft("unity") || value.ID.IsLeft("Unity")) continue;
using (new EditorGUILayout.HorizontalScope())
{
if (value.IsValue)
{
EditorGUILayout.LabelField(value.ID + " " + value);
}
else
{
if (prefOn.ContainsKey(value.ID) == false) prefOn[value.ID] = false;
prefOn[value.ID] = EditorGUILayout.Foldout(prefOn[value.ID], value.ID);
if (prefOn[value.ID])
{
if (prefView.ContainsKey(value.ID) == false) prefView[value.ID] = new Dictionary<string, bool>();
SLBindingEntityView.UpdateView(value, "", 1, prefView[value.ID]);
Repaint();
}
}
if (GUILayout.Button("Remove", GUILayout.Width(200)))
{
PlayerPrefs.DeleteKey(value.ID);
pref = null;
return;
}
}
}
}
}
private void MakePref()
{
Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software\\Unity\\UnityEditor\\" + PlayerSettings.companyName + "\\" + PlayerSettings.productName);
if (registryKey != null)
{
string[] valueNames = registryKey.GetValueNames();
pref = SLEntity.Empty;
// Parse and convert the registry saved player prefs into our array
//int i = 0;
foreach (string valueName in valueNames)
{
string key = valueName;
// Remove the _h193410979 style suffix used on player pref keys in Windows registry
int index = key.LastIndexOf("_");
key = key.Remove(index, key.Length - index);
// Get the value from the registry
object ambiguousValue = registryKey.GetValue(valueName);
// Unfortunately floats will come back as an int (at least on 64 bit) because the float is stored as
// 64 bit but marked as 32 bit - which confuses the GetValue() method greatly!
if (ambiguousValue.GetType() == typeof(int))
{
// If the player pref is not actually an int then it must be a float, this will evaluate to true
// (impossible for it to be 0 and -1 at the same time)
if (PlayerPrefs.GetInt(key, -1) == -1 && PlayerPrefs.GetInt(key, 0) == 0)
{
// Fetch the float value from PlayerPrefs in memory
pref[key] = PlayerPrefs.GetFloat(key);
}
else
{
pref[key] = (int)ambiguousValue;
}
}
else if (ambiguousValue.GetType() == typeof(byte[]))
{
// On Unity 5 a string may be stored as binary, so convert it back to a string
pref[key] = System.Text.Encoding.UTF8.GetString((byte[])ambiguousValue);
if (pref[key].IsLeft("{"))
{
pref[key] = JsonLoader.LoadJson(pref[key]);
}
}
}
}
#endif
}
}
}