hybridclr_unity/Editor/Settings/HybridCLRSettingProvider.cs

151 lines
7.2 KiB
C#
Raw Normal View History

using System;
using System.Reflection;
using UnityEditor;
using UnityEditor.Presets;
using UnityEngine;
using UnityEngine.UIElements;
namespace HybridCLR.Editor
{
public class HybridCLRSettingsProvider : SettingsProvider
{
2022-10-18 13:51:38 +08:00
private static SerializedObject _serializedObject;
private SerializedProperty _enable;
private SerializedProperty _useGlobalIl2cpp;
private SerializedProperty _cloneFromGitee;
private SerializedProperty _hotUpdateAssemblyDefinitions;
private SerializedProperty _hotUpdateAssemblies;
private SerializedProperty _preserveHotUpdateAssemblies;
private SerializedProperty _patchAOTAssemblies;
2022-10-18 13:51:38 +08:00
private SerializedProperty _collectAssetReferenceTypes;
private SerializedProperty _outputLinkFile;
private SerializedProperty _outputAOTGenericReferenceFile;
private SerializedProperty _maxGenericReferenceIteration;
private SerializedProperty _maxMethodBridgeGenericIteration;
private GUIStyle buttonStyle;
public HybridCLRSettingsProvider() : base("Project/HybridCLR Settings", SettingsScope.Project) { }
public override void OnActivate(string searchContext, VisualElement rootElement)
{
2022-10-09 20:53:13 +08:00
HybridCLRSettings.Instance.Save();
var setting = HybridCLRSettings.Instance;
setting.hideFlags &= ~HideFlags.NotEditable;
2022-10-18 13:51:38 +08:00
_serializedObject = _serializedObject ?? new SerializedObject(setting);
_enable = _serializedObject.FindProperty("enable");
_useGlobalIl2cpp = _serializedObject.FindProperty("useGlobalIl2cpp");
_cloneFromGitee = _serializedObject.FindProperty("cloneFromGitee");
_hotUpdateAssemblyDefinitions = _serializedObject.FindProperty("hotUpdateAssemblyDefinitions");
_hotUpdateAssemblies = _serializedObject.FindProperty("hotUpdateAssemblies");
_preserveHotUpdateAssemblies = _serializedObject.FindProperty("preserveHotUpdateAssemblies");
_patchAOTAssemblies = _serializedObject.FindProperty("patchAOTAssemblies");
2022-10-18 13:51:38 +08:00
_collectAssetReferenceTypes = _serializedObject.FindProperty("collectAssetReferenceTypes");
_outputLinkFile = _serializedObject.FindProperty("outputLinkFile");
_outputAOTGenericReferenceFile = _serializedObject.FindProperty("outputAOTGenericReferenceFile");
_maxGenericReferenceIteration = _serializedObject.FindProperty("maxGenericReferenceIteration");
_maxMethodBridgeGenericIteration = _serializedObject.FindProperty("maxMethodBridgeGenericIteration");
}
public override void OnTitleBarGUI()
{
base.OnTitleBarGUI();
var rect = GUILayoutUtility.GetLastRect();
buttonStyle = buttonStyle ?? GUI.skin.GetStyle("IconButton");
#region 绘制官方网站跳转按钮
var w = rect.x + rect.width;
rect.x = w - 58;
rect.y += 6;
rect.width = rect.height = 18;
var content = EditorGUIUtility.IconContent("_Help");
content.tooltip = "点击访问 HybridCLR 官方文档";
if (GUI.Button(rect, content, buttonStyle))
{
Application.OpenURL("https://focus-creative-games.github.io/hybridclr/");
}
#endregion
#region 绘制 Preset
rect.x += 19;
content = EditorGUIUtility.IconContent("Preset.Context");
content.tooltip = "点击存储或加载 Preset .";
if (GUI.Button(rect, content, buttonStyle))
{
2022-10-09 20:53:13 +08:00
var target = HybridCLRSettings.Instance;
var receiver = ScriptableObject.CreateInstance<SettingsPresetReceiver>();
receiver.Init(target);
PresetSelector.ShowSelector(target, null, true, receiver);
}
#endregion
#region 绘制 Reset
rect.x += 19;
content = EditorGUIUtility.IconContent("_Popup");
content.tooltip = "Reset";
if (GUI.Button(rect, content, buttonStyle))
{
GenericMenu menu = new GenericMenu();
menu.AddItem(new GUIContent("Reset"), false, () =>
{
2022-10-09 20:53:13 +08:00
Undo.RecordObject(HybridCLRSettings.Instance, "Capture Value for Reset");
var dv = ScriptableObject.CreateInstance<HybridCLRSettings>();
var json = EditorJsonUtility.ToJson(dv);
2022-10-09 20:53:13 +08:00
EditorJsonUtility.FromJsonOverwrite(json,HybridCLRSettings.Instance);
HybridCLRSettings.Instance.Save();
});
menu.ShowAsContext();
}
#endregion
}
public override void OnGUI(string searchContext)
{
using (CreateSettingsWindowGUIScope())
{
2022-10-18 13:51:38 +08:00
if (_serializedObject == null || !_serializedObject.targetObject)
{
2022-10-18 13:51:38 +08:00
_serializedObject = null;
_serializedObject = new SerializedObject(HybridCLRSettings.Instance);
}
2022-10-18 13:51:38 +08:00
_serializedObject.Update();
EditorGUI.BeginChangeCheck();
2022-10-18 13:51:38 +08:00
EditorGUILayout.PropertyField(_enable);
EditorGUILayout.PropertyField(_cloneFromGitee);
EditorGUILayout.PropertyField(_useGlobalIl2cpp);
EditorGUILayout.PropertyField(_hotUpdateAssemblyDefinitions);
EditorGUILayout.PropertyField(_hotUpdateAssemblies);
EditorGUILayout.PropertyField(_preserveHotUpdateAssemblies);
EditorGUILayout.PropertyField(_patchAOTAssemblies);
2022-10-18 13:51:38 +08:00
EditorGUILayout.PropertyField(_collectAssetReferenceTypes);
EditorGUILayout.PropertyField(_outputLinkFile);
EditorGUILayout.PropertyField(_outputAOTGenericReferenceFile);
EditorGUILayout.PropertyField(_maxGenericReferenceIteration);
EditorGUILayout.PropertyField(_maxMethodBridgeGenericIteration);
if (EditorGUI.EndChangeCheck())
{
2022-10-18 13:51:38 +08:00
_serializedObject.ApplyModifiedProperties();
2022-10-09 20:53:13 +08:00
HybridCLRSettings.Instance.Save();
}
}
}
private IDisposable CreateSettingsWindowGUIScope()
{
var unityEditorAssembly = Assembly.GetAssembly(typeof(EditorWindow));
var type = unityEditorAssembly.GetType("UnityEditor.SettingsWindow+GUIScope");
return Activator.CreateInstance(type) as IDisposable;
}
public override void OnDeactivate()
{
base.OnDeactivate();
2022-10-09 20:53:13 +08:00
HybridCLRSettings.Instance.Save();
2022-10-18 13:51:38 +08:00
_serializedObject = null;
}
[SettingsProvider]
public static SettingsProvider CreateMyCustomSettingsProvider()
{
2022-10-09 20:53:13 +08:00
if (HybridCLRSettings.Instance)
{
var provider = new HybridCLRSettingsProvider
{
2022-10-18 13:51:38 +08:00
keywords = GetSearchKeywordsFromSerializedObject(_serializedObject = _serializedObject ?? new SerializedObject(HybridCLRSettings.Instance))
};
return provider;
}
return null;
}
}
}