[fix] 修复 SettingsProvider 卡编辑器的问题 (#16)

* [change] 跨版本兼容问题优化,菜单新增文档

1. 添加 EditorStatusWatcher 解决 git 等外部修改了配置不能同步的问题。
2. Reset 按钮 icon 2021版本有区别,同步之

* [fix] 解决 SettingProviders 卡编辑器的问题
main
雨落随风 2022-11-02 10:17:41 +08:00 committed by GitHub
parent bf8e5c4840
commit 3918b707e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 35 deletions

View File

@ -14,17 +14,13 @@ public static class EditorStatusWatcher
static EditorStatusWatcher() => EditorApplication.update += Update; static EditorStatusWatcher() => EditorApplication.update += Update;
static void Update() static void Update()
{ {
//当编辑器 focus 后如果优先发生编译,则等待其完成编译再刷新内部逻辑(猜想&防御) if (isFocused != InternalEditorUtility.isApplicationActive)
if (!EditorApplication.isCompiling)
{ {
if (isFocused != InternalEditorUtility.isApplicationActive) isFocused = InternalEditorUtility.isApplicationActive;
if (isFocused)
{ {
isFocused = InternalEditorUtility.isApplicationActive; HybridCLRSettings.LoadOrCreate();
if (isFocused) OnEditorFocused?.Invoke();
{
HybridCLRSettings.LoadOrCreate();
OnEditorFocused?.Invoke();
}
} }
} }
} }

View File

@ -2,14 +2,13 @@ using System;
using System.Reflection; using System.Reflection;
using UnityEditor; using UnityEditor;
using UnityEditor.Presets; using UnityEditor.Presets;
using UnityEditorInternal;
using UnityEngine; using UnityEngine;
using UnityEngine.UIElements; using UnityEngine.UIElements;
namespace HybridCLR.Editor namespace HybridCLR.Editor
{ {
public class HybridCLRSettingsProvider : SettingsProvider public class HybridCLRSettingsProvider : SettingsProvider
{ {
private static SerializedObject _serializedObject; private SerializedObject _serializedObject;
private SerializedProperty _enable; private SerializedProperty _enable;
private SerializedProperty _useGlobalIl2cpp; private SerializedProperty _useGlobalIl2cpp;
private SerializedProperty _cloneFromGitee; private SerializedProperty _cloneFromGitee;
@ -29,9 +28,13 @@ namespace HybridCLR.Editor
public override void OnActivate(string searchContext, VisualElement rootElement) public override void OnActivate(string searchContext, VisualElement rootElement)
{ {
EditorStatusWatcher.OnEditorFocused += OnEditorFocused; EditorStatusWatcher.OnEditorFocused += OnEditorFocused;
HybridCLRSettings.Instance.Save(); InitGUI();
var setting = HybridCLRSettings.Instance; }
_serializedObject = _serializedObject ?? new SerializedObject(setting); private void InitGUI()
{
var setting = HybridCLRSettings.LoadOrCreate();
_serializedObject?.Dispose();
_serializedObject = new SerializedObject(setting);
_enable = _serializedObject.FindProperty("enable"); _enable = _serializedObject.FindProperty("enable");
_useGlobalIl2cpp = _serializedObject.FindProperty("useGlobalIl2cpp"); _useGlobalIl2cpp = _serializedObject.FindProperty("useGlobalIl2cpp");
_cloneFromGitee = _serializedObject.FindProperty("cloneFromGitee"); _cloneFromGitee = _serializedObject.FindProperty("cloneFromGitee");
@ -47,13 +50,11 @@ namespace HybridCLR.Editor
_maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration"); _maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration");
_maxMethodBridgeGenericIteration = _serializedObject.FindProperty("maxMethodBridgeGenericIteration"); _maxMethodBridgeGenericIteration = _serializedObject.FindProperty("maxMethodBridgeGenericIteration");
} }
private void OnEditorFocused() private void OnEditorFocused()
{ {
_serializedObject = new SerializedObject(HybridCLRSettings.Instance); InitGUI();
SettingsService.NotifySettingsProviderChanged(); Repaint();
} }
public override void OnTitleBarGUI() public override void OnTitleBarGUI()
{ {
base.OnTitleBarGUI(); base.OnTitleBarGUI();
@ -104,7 +105,7 @@ namespace HybridCLR.Editor
var json = EditorJsonUtility.ToJson(dv); var json = EditorJsonUtility.ToJson(dv);
UnityEngine.Object.DestroyImmediate(dv); UnityEngine.Object.DestroyImmediate(dv);
EditorJsonUtility.FromJsonOverwrite(json, HybridCLRSettings.Instance); EditorJsonUtility.FromJsonOverwrite(json, HybridCLRSettings.Instance);
HybridCLRSettings.Instance.Save(); HybridCLRSettings.Save();
}); });
menu.ShowAsContext(); menu.ShowAsContext();
} }
@ -114,11 +115,6 @@ namespace HybridCLR.Editor
{ {
using (CreateSettingsWindowGUIScope()) using (CreateSettingsWindowGUIScope())
{ {
//防止配置文件意外删除
if (_serializedObject == null || !_serializedObject.targetObject)
{
_serializedObject = new SerializedObject(HybridCLRSettings.Instance);
}
_serializedObject.Update(); _serializedObject.Update();
EditorGUI.BeginChangeCheck(); EditorGUI.BeginChangeCheck();
EditorGUILayout.PropertyField(_enable); EditorGUILayout.PropertyField(_enable);
@ -138,7 +134,7 @@ namespace HybridCLR.Editor
if (EditorGUI.EndChangeCheck()) if (EditorGUI.EndChangeCheck())
{ {
_serializedObject.ApplyModifiedProperties(); _serializedObject.ApplyModifiedProperties();
HybridCLRSettings.Instance.Save(); HybridCLRSettings.Save();
} }
} }
} }
@ -152,21 +148,22 @@ namespace HybridCLR.Editor
{ {
base.OnDeactivate(); base.OnDeactivate();
EditorStatusWatcher.OnEditorFocused -= OnEditorFocused; EditorStatusWatcher.OnEditorFocused -= OnEditorFocused;
HybridCLRSettings.Instance.Save(); HybridCLRSettings.Save();
_serializedObject = null;
} }
static HybridCLRSettingsProvider provider;
[SettingsProvider] [SettingsProvider]
public static SettingsProvider CreateMyCustomSettingsProvider() public static SettingsProvider CreateMyCustomSettingsProvider()
{ {
if (HybridCLRSettings.Instance) if (HybridCLRSettings.Instance && provider == null)
{ {
var provider = new HybridCLRSettingsProvider provider = new HybridCLRSettingsProvider();
using (var so = new SerializedObject(HybridCLRSettings.Instance))
{ {
keywords = GetSearchKeywordsFromSerializedObject(_serializedObject = _serializedObject ?? new SerializedObject(HybridCLRSettings.Instance)) provider.keywords = GetSearchKeywordsFromSerializedObject(so);
}; }
return provider;
} }
return null; return provider;
} }
} }
} }

View File

@ -21,7 +21,7 @@ namespace HybridCLR.Editor
return s_Instance; return s_Instance;
} }
} }
public static void LoadOrCreate() public static T LoadOrCreate()
{ {
string filePath = GetFilePath(); string filePath = GetFilePath();
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
@ -33,9 +33,10 @@ namespace HybridCLR.Editor
{ {
Debug.LogError($"{nameof(ScriptableSingleton<T>)}: 请指定单例存档路径! "); Debug.LogError($"{nameof(ScriptableSingleton<T>)}: 请指定单例存档路径! ");
} }
return s_Instance;
} }
public void Save(bool saveAsText = true) public static void Save(bool saveAsText = true)
{ {
if (!s_Instance) if (!s_Instance)
{ {