[change] 跨版本兼容问题优化,菜单新增文档 (#14)

1. 添加 EditorStatusWatcher 解决 git 等外部修改了配置不能同步的问题。
2. Reset 按钮 icon 2021版本有区别,同步之
main
雨落随风 2022-10-25 17:36:32 +08:00 committed by GitHub
parent 1b69c9ca78
commit c12f1ee8e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 35 deletions

View File

@ -0,0 +1,31 @@
using HybridCLR.Editor;
using System;
using UnityEditor;
using UnityEditorInternal;
/// <summary>
/// 监听编辑器状态,当编辑器重新 focus 时,重新加载实例,避免某些情景下 svn 、git 等外部修改了数据却无法同步的异常。
/// </summary>
[InitializeOnLoad]
public static class EditorStatusWatcher
{
public static Action OnEditorFocused;
static bool isFocused;
static EditorStatusWatcher() => EditorApplication.update += Update;
static void Update()
{
//当编辑器 focus 后如果优先发生编译,则等待其完成编译再刷新内部逻辑(猜想&防御)
if (!EditorApplication.isCompiling)
{
if (isFocused != InternalEditorUtility.isApplicationActive)
{
isFocused = InternalEditorUtility.isApplicationActive;
if (isFocused)
{
HybridCLRSettings.LoadOrCreate();
OnEditorFocused?.Invoke();
}
}
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 093a9e6c1e7399244bbcd8983fdbfdee
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -2,6 +2,7 @@ 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
@ -27,9 +28,9 @@ namespace HybridCLR.Editor
public HybridCLRSettingsProvider() : base("Project/HybridCLR Settings", SettingsScope.Project) { } public HybridCLRSettingsProvider() : base("Project/HybridCLR Settings", SettingsScope.Project) { }
public override void OnActivate(string searchContext, VisualElement rootElement) public override void OnActivate(string searchContext, VisualElement rootElement)
{ {
EditorStatusWatcher.OnEditorFocused += OnEditorFocused;
HybridCLRSettings.Instance.Save(); HybridCLRSettings.Instance.Save();
var setting = HybridCLRSettings.Instance; var setting = HybridCLRSettings.Instance;
setting.hideFlags &= ~HideFlags.NotEditable;
_serializedObject = _serializedObject ?? new SerializedObject(setting); _serializedObject = _serializedObject ?? new SerializedObject(setting);
_enable = _serializedObject.FindProperty("enable"); _enable = _serializedObject.FindProperty("enable");
_useGlobalIl2cpp = _serializedObject.FindProperty("useGlobalIl2cpp"); _useGlobalIl2cpp = _serializedObject.FindProperty("useGlobalIl2cpp");
@ -46,6 +47,13 @@ namespace HybridCLR.Editor
_maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration"); _maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration");
_maxMethodBridgeGenericIteration = _serializedObject.FindProperty("maxMethodBridgeGenericIteration"); _maxMethodBridgeGenericIteration = _serializedObject.FindProperty("maxMethodBridgeGenericIteration");
} }
private void OnEditorFocused()
{
_serializedObject = new SerializedObject(HybridCLRSettings.Instance);
SettingsService.NotifySettingsProviderChanged();
}
public override void OnTitleBarGUI() public override void OnTitleBarGUI()
{ {
base.OnTitleBarGUI(); base.OnTitleBarGUI();
@ -54,7 +62,7 @@ namespace HybridCLR.Editor
#region 绘制官方网站跳转按钮 #region 绘制官方网站跳转按钮
var w = rect.x + rect.width; var w = rect.x + rect.width;
rect.x = w - 58; rect.x = w - 57;
rect.y += 6; rect.y += 6;
rect.width = rect.height = 18; rect.width = rect.height = 18;
var content = EditorGUIUtility.IconContent("_Help"); var content = EditorGUIUtility.IconContent("_Help");
@ -64,7 +72,6 @@ namespace HybridCLR.Editor
Application.OpenURL("https://focus-creative-games.github.io/hybridclr/"); Application.OpenURL("https://focus-creative-games.github.io/hybridclr/");
} }
#endregion #endregion
#region 绘制 Preset #region 绘制 Preset
rect.x += 19; rect.x += 19;
content = EditorGUIUtility.IconContent("Preset.Context"); content = EditorGUIUtility.IconContent("Preset.Context");
@ -73,13 +80,19 @@ namespace HybridCLR.Editor
{ {
var target = HybridCLRSettings.Instance; var target = HybridCLRSettings.Instance;
var receiver = ScriptableObject.CreateInstance<SettingsPresetReceiver>(); var receiver = ScriptableObject.CreateInstance<SettingsPresetReceiver>();
receiver.Init(target); receiver.Init(target, this);
PresetSelector.ShowSelector(target, null, true, receiver); PresetSelector.ShowSelector(target, null, true, receiver);
} }
#endregion #endregion
#region 绘制 Reset #region 绘制 Reset
rect.x += 19; rect.x += 19;
content = EditorGUIUtility.IconContent("_Popup"); content = EditorGUIUtility.IconContent(
#if UNITY_2021_3_OR_NEWER
"pane options"
#else
"_Popup"
#endif
);
content.tooltip = "Reset"; content.tooltip = "Reset";
if (GUI.Button(rect, content, buttonStyle)) if (GUI.Button(rect, content, buttonStyle))
{ {
@ -89,6 +102,7 @@ namespace HybridCLR.Editor
Undo.RecordObject(HybridCLRSettings.Instance, "Capture Value for Reset"); Undo.RecordObject(HybridCLRSettings.Instance, "Capture Value for Reset");
var dv = ScriptableObject.CreateInstance<HybridCLRSettings>(); var dv = ScriptableObject.CreateInstance<HybridCLRSettings>();
var json = EditorJsonUtility.ToJson(dv); var json = EditorJsonUtility.ToJson(dv);
UnityEngine.Object.DestroyImmediate(dv);
EditorJsonUtility.FromJsonOverwrite(json, HybridCLRSettings.Instance); EditorJsonUtility.FromJsonOverwrite(json, HybridCLRSettings.Instance);
HybridCLRSettings.Instance.Save(); HybridCLRSettings.Instance.Save();
}); });
@ -100,9 +114,9 @@ namespace HybridCLR.Editor
{ {
using (CreateSettingsWindowGUIScope()) using (CreateSettingsWindowGUIScope())
{ {
//防止配置文件意外删除
if (_serializedObject == null || !_serializedObject.targetObject) if (_serializedObject == null || !_serializedObject.targetObject)
{ {
_serializedObject = null;
_serializedObject = new SerializedObject(HybridCLRSettings.Instance); _serializedObject = new SerializedObject(HybridCLRSettings.Instance);
} }
_serializedObject.Update(); _serializedObject.Update();
@ -137,6 +151,7 @@ namespace HybridCLR.Editor
public override void OnDeactivate() public override void OnDeactivate()
{ {
base.OnDeactivate(); base.OnDeactivate();
EditorStatusWatcher.OnEditorFocused -= OnEditorFocused;
HybridCLRSettings.Instance.Save(); HybridCLRSettings.Instance.Save();
_serializedObject = null; _serializedObject = null;
} }

View File

@ -5,9 +5,17 @@ public static class MenuProvider
{ {
[MenuItem("HybridCLR/Settings", priority = 200)] [MenuItem("HybridCLR/Settings", priority = 200)]
public static void OpenSettings() => SettingsService.OpenProjectSettings("Project/HybridCLR Settings"); public static void OpenSettings() => SettingsService.OpenProjectSettings("Project/HybridCLR Settings");
[MenuItem("HybridCLR/About us", priority = 400)] [MenuItem("HybridCLR/Documents/", menuItem = "HybridCLR/Documents/Quick Start")]
public static void AboutUs() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/about/"); public static void OpenQuickStart() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/start_up/");
[MenuItem("HybridCLR/Documents/", menuItem = "HybridCLR/Documents/Benchmark")]
[MenuItem("HybridCLR/Documents", priority = 399)] public static void OpenBenchmark() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/benchmark/");
public static void OpenDoc() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/"); [MenuItem("HybridCLR/Documents/", menuItem = "HybridCLR/Documents/FAQ")]
public static void OpenFAQ() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/faq/");
[MenuItem("HybridCLR/Documents/", menuItem = "HybridCLR/Documents/Common Errors")]
public static void OpenCommonErrors() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/common_errors/");
[MenuItem("HybridCLR/Documents/", menuItem = "HybridCLR/Documents/Bug Report")]
public static void OpenBugReport() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/bug_reporter/");
[MenuItem("HybridCLR/Documents/", menuItem = "HybridCLR/Documents/About HybridCLR")]
public static void OpenAbout() => Application.OpenURL("https://focus-creative-games.github.io/hybridclr/about/");
} }

View File

@ -1,5 +1,6 @@
using System; using System;
using System.IO; using System.IO;
using System.Linq;
using UnityEditor; using UnityEditor;
using UnityEditorInternal; using UnityEditorInternal;
using UnityEngine; using UnityEngine;
@ -15,18 +16,18 @@ namespace HybridCLR.Editor
{ {
if (!s_Instance) if (!s_Instance)
{ {
CreateAndLoad(); LoadOrCreate();
} }
return s_Instance; return s_Instance;
} }
} }
private static void CreateAndLoad() public static void LoadOrCreate()
{ {
string filePath = GetFilePath(); string filePath = GetFilePath();
if (!string.IsNullOrEmpty(filePath)) if (!string.IsNullOrEmpty(filePath))
{ {
var arr = InternalEditorUtility.LoadSerializedFileAndForget(filePath); var arr = InternalEditorUtility.LoadSerializedFileAndForget(filePath);
s_Instance = arr.Length > 0 ? arr[0] as T : CreateInstance<T>(); s_Instance = arr.Length > 0 ? arr[0] as T : s_Instance??CreateInstance<T>();
} }
else else
{ {
@ -56,18 +57,10 @@ namespace HybridCLR.Editor
} }
protected static string GetFilePath() protected static string GetFilePath()
{ {
Type typeFromHandle = typeof(T); return typeof(T).GetCustomAttributes(inherit: true)
object[] customAttributes = typeFromHandle.GetCustomAttributes(inherit: true); .Cast<FilePathAttribute>()
object[] array = customAttributes; .FirstOrDefault(v => v != null)
foreach (object obj in array) ?.filepath;
{
if (obj is FilePathAttribute)
{
FilePathAttribute filePathAttribute = obj as FilePathAttribute;
return filePathAttribute.filepath;
}
}
return string.Empty;
} }
} }
[AttributeUsage(AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Class)]

View File

@ -8,11 +8,13 @@ namespace HybridCLR.Editor
{ {
private Object m_Target; private Object m_Target;
private Preset m_InitialValue; private Preset m_InitialValue;
private SettingsProvider m_Provider;
internal void Init(Object target) internal void Init(Object target, SettingsProvider provider)
{ {
m_Target = target; m_Target = target;
m_InitialValue = new Preset(target); m_InitialValue = new Preset(target);
m_Provider = provider;
} }
public override void OnSelectionChanged(Preset selection) public override void OnSelectionChanged(Preset selection)
{ {
@ -26,9 +28,7 @@ namespace HybridCLR.Editor
Undo.RecordObject(m_Target, "Cancel Preset"); Undo.RecordObject(m_Target, "Cancel Preset");
m_InitialValue.ApplyTo(m_Target); m_InitialValue.ApplyTo(m_Target);
} }
#if UNITY_2020_1_OR_NEWER m_Provider.Repaint();
SettingsService.RepaintAllSettingsWindow();
#endif
} }
public override void OnSelectionClosed(Preset selection) public override void OnSelectionClosed(Preset selection)
{ {