From 6d8022cfc56ef845dd540243f758d2410e82de30 Mon Sep 17 00:00:00 2001 From: Mefodei Date: Tue, 28 Jul 2020 19:18:36 +0300 Subject: [PATCH] feat: ODIN inspector search filter (#35) * - add optional Odin inspector support - move Button Attribute to runtime asm.def * - add optional Odin inspector support - move Button Attribute to runtime asm.def * - add FilePath attribue for file usage improvement * - update odin Addressable Import Rule drawer * - update odin Addressable Import Rule drawer * - fix Folder Path attribute * - fix Force backslashes * - fix Force backslashes * https://github.com/favoyang/unity-addressable-importer/pull/33 fixes * - add search filter for Odin inspector * - update addressables import serach filter * https://github.com/favoyang/unity-addressable-importer/pull/33 - review fixes * - fix review comments * https://github.com/favoyang/unity-addressable-importer/pull/33 * - fix odin serach filter support * - fix importer search filter * - fix ApplyChanges index * - fix duplicate class def --- Editor/AddressableImportRule.cs | 6 +- Editor/AddressableImportSettings.cs | 6 +- .../Helper/AddressableImportSettingsEditor.cs | 15 +- .../Helper/AddressableImporterOdinHandler.cs | 44 ----- Editor/OdinSupport.meta | 8 + .../AddressableImporterOdinHandler.cs | 65 +++++++ .../AddressableImporterOdinHandler.cs.meta | 2 +- .../AddressablesImporterFilterOdinHandler.cs | 163 ++++++++++++++++++ ...ressablesImporterFilterOdinHandler.cs.meta | 3 + 9 files changed, 257 insertions(+), 55 deletions(-) delete mode 100644 Editor/Helper/AddressableImporterOdinHandler.cs create mode 100644 Editor/OdinSupport.meta create mode 100644 Editor/OdinSupport/AddressableImporterOdinHandler.cs rename Editor/{Helper => OdinSupport}/AddressableImporterOdinHandler.cs.meta (83%) create mode 100644 Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs create mode 100644 Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs.meta diff --git a/Editor/AddressableImportRule.cs b/Editor/AddressableImportRule.cs index 4f9e46a..09f4c59 100644 --- a/Editor/AddressableImportRule.cs +++ b/Editor/AddressableImportRule.cs @@ -42,7 +42,7 @@ public class AddressableImportRule /// Path pattern. /// [Tooltip("The assets in this path will be processed.")] - public string path; + public string path = string.Empty; /// /// Method used to parse the Path. @@ -54,7 +54,7 @@ public class AddressableImportRule /// The group the asset will be added. /// [Tooltip("The group name in which the Addressable will be added. Leave blank for the default group.")] - public string groupName; + public string groupName = string.Empty; /// /// Cleaned group name. @@ -100,7 +100,7 @@ string CleanedGroupName { /// [Tooltip("Replacement address string for regex matches.")] [ConditionalField("matchType", AddressableImportRuleMatchType.Regex, "simplified", false)] - public string addressReplacement; + public string addressReplacement = string.Empty; public bool HasLabel { diff --git a/Editor/AddressableImportSettings.cs b/Editor/AddressableImportSettings.cs index 2ac3b88..df49fac 100644 --- a/Editor/AddressableImportSettings.cs +++ b/Editor/AddressableImportSettings.cs @@ -22,19 +22,19 @@ public class AddressableImportSettings : ScriptableObject public List rules; [ButtonMethod] - private void Save() + public void Save() { AssetDatabase.SaveAssets(); } [ButtonMethod] - private void Documentation() + public void Documentation() { Application.OpenURL("https://github.com/favoyang/unity-addressable-importer/blob/master/Documentation~/AddressableImporter.md"); } [ButtonMethod] - private void CleanEmptyGroup() + public void CleanEmptyGroup() { var settings = AddressableAssetSettingsDefaultObject.Settings; if (settings == null) diff --git a/Editor/Helper/AddressableImportSettingsEditor.cs b/Editor/Helper/AddressableImportSettingsEditor.cs index bf6c42a..313f2c1 100644 --- a/Editor/Helper/AddressableImportSettingsEditor.cs +++ b/Editor/Helper/AddressableImportSettingsEditor.cs @@ -17,17 +17,19 @@ namespace UnityAddressableImporter.Helper.Internal public class AddressableImportSettingsEditor : Editor { private List _methods; - private ScriptableObject _target; + private AddressableImportSettings _target; private AddressableImporterOdinHandler _drawer; + private void OnEnable() { - _target = target as ScriptableObject; + _target = target as AddressableImportSettings; _drawer = _drawer ?? new AddressableImporterOdinHandler(); if (_target == null) return; - _drawer.Initialize(target); + _drawer.Initialize(_target); _methods = AddressableImporterMethodHandler.CollectValidMembers(_target.GetType()); + } private void OnDisable() @@ -38,10 +40,15 @@ private void OnDisable() public override void OnInspectorGUI() { DrawBaseEditor(); - + +#if !ODIN_INSPECTOR if (_methods == null) return; AddressableImporterMethodHandler.OnInspectorGUI(_target, _methods); +#endif + + serializedObject.ApplyModifiedProperties(); + } private void DrawBaseEditor() diff --git a/Editor/Helper/AddressableImporterOdinHandler.cs b/Editor/Helper/AddressableImporterOdinHandler.cs deleted file mode 100644 index e63ad5e..0000000 --- a/Editor/Helper/AddressableImporterOdinHandler.cs +++ /dev/null @@ -1,44 +0,0 @@ -namespace UnityAddressableImporter.Editor.Helper -{ - using System; - using Object = UnityEngine.Object; - -#if ODIN_INSPECTOR - - using Sirenix.OdinInspector.Editor; - using Sirenix.OdinInspector; - - public class AddressableImporterOdinHandler : IDisposable - { - private Object _target; - private PropertyTree _drawer; - - public void Initialize(Object target) - { - _target = target; - _drawer = PropertyTree.Create(_target); - } - - public void Draw() => _drawer?.Draw(false); - - public void Dispose() - { - _target = null; - _drawer?.Dispose(); - _drawer = null; - } - } - -#else - - public class AddressableImporterOdinHandler : IDisposable - { - public void Initialize(Object target) { } - - public void Draw() { } - - public void Dispose() { } - } - -#endif -} diff --git a/Editor/OdinSupport.meta b/Editor/OdinSupport.meta new file mode 100644 index 0000000..83970b6 --- /dev/null +++ b/Editor/OdinSupport.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b5ac705c19021894d9e4582e2125e037 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Editor/OdinSupport/AddressableImporterOdinHandler.cs b/Editor/OdinSupport/AddressableImporterOdinHandler.cs new file mode 100644 index 0000000..87a024c --- /dev/null +++ b/Editor/OdinSupport/AddressableImporterOdinHandler.cs @@ -0,0 +1,65 @@ +namespace UnityAddressableImporter.Editor.Helper +{ + using System; + using Object = UnityEngine.Object; +#if ODIN_INSPECTOR + using Sirenix.OdinInspector.Editor; + using Sirenix.OdinInspector; + using UnityEditor; + using UnityEngine; + + public class AddressableImporterOdinHandler : IDisposable + { + private AddressableImportSettings _settings; + private AddressablesImporterFilterOdinHandler _importRulesContainer; + private GUIContent _searchFieldLabel; + private string _searchField; + + public void Initialize(AddressableImportSettings target) + { + _settings = target; + _importRulesContainer = CreateDrawer(_settings); + } + + public void Draw() + { + DrawInspectorTree(_searchField); + + EditorUtility.SetDirty(_settings); + } + + public void Dispose() + { + _settings = null; + if (_importRulesContainer) { + Object.DestroyImmediate(_importRulesContainer); + _importRulesContainer = null; + } + } + + private AddressablesImporterFilterOdinHandler CreateDrawer(AddressableImportSettings settings) + { + _importRulesContainer = ScriptableObject.CreateInstance(); + _importRulesContainer.Initialize(settings); + return _importRulesContainer; + } + + private void DrawInspectorTree(string filter) + { + _importRulesContainer?.Draw(); + } + } + +#else + + public class AddressablesImporterOdinHandler : IDisposable + { + public void Initialize(AddressableImportSettings target) { } + + public void Draw() { } + + public void Dispose() { } + } + +#endif +} diff --git a/Editor/Helper/AddressableImporterOdinHandler.cs.meta b/Editor/OdinSupport/AddressableImporterOdinHandler.cs.meta similarity index 83% rename from Editor/Helper/AddressableImporterOdinHandler.cs.meta rename to Editor/OdinSupport/AddressableImporterOdinHandler.cs.meta index 736d8d1..0f2085e 100644 --- a/Editor/Helper/AddressableImporterOdinHandler.cs.meta +++ b/Editor/OdinSupport/AddressableImporterOdinHandler.cs.meta @@ -1,5 +1,5 @@ fileFormatVersion: 2 -guid: 36b62799a4bc2d14ea554520750092db +guid: c66c75c18ca488c4fb7bf9c6b7cfdf4b MonoImporter: externalObjects: {} serializedVersion: 2 diff --git a/Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs b/Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs new file mode 100644 index 0000000..afa792f --- /dev/null +++ b/Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs @@ -0,0 +1,163 @@ +namespace UnityAddressableImporter.Editor.Helper +{ +#if ODIN_INSPECTOR + + using System; + using System.Collections.Generic; + using System.Linq; + using Sirenix.OdinInspector; + using Sirenix.OdinInspector.Editor; + using UnityEditor; + using UnityEngine; + + public class AddressablesImporterFilterOdinHandler : ScriptableObject + { + private AddressableImportSettings _importSettings; + private PropertyTree _drawerTree; + private List> _filters; + //private List _filteredRules; + private bool _sourceChanged = false; + + [SerializeField] + [HideLabel] + [OnValueChanged("OnFilterChanged")] + private string _searchFilter; + + [SerializeField] + [ListDrawerSettings( + HideRemoveButton = true, + Expanded = true, + CustomAddFunction = nameof(CustomAddFunction), + OnEndListElementGUI = nameof(EndOfListItemGui), + CustomRemoveElementFunction = nameof(CustomRemoveElementFunction), + CustomRemoveIndexFunction = nameof(CustomRemoveIndexFunction), + ShowPaging = true + )] + private List rules = new List(); + + public void Initialize(AddressableImportSettings importSettings) + { + _importSettings = importSettings; + _drawerTree = PropertyTree.Create(this); + + _filters = new List>() { + ValidateAddressableGroupName, + ValidateRulePath, + ValidateLabelsPath, + }; + + _drawerTree.OnPropertyValueChanged += (property, index) => EditorUtility.SetDirty(_importSettings); + } + + public void Draw() + { + try { + FilterRules(_searchFilter); + _drawerTree.Draw(); + ApplyChanges(); + } + catch (Exception e) { + Debug.LogError(e); + } + + } + + [Button] + public void Save() => _importSettings.Save(); + + [Button] + public void Documentation() => _importSettings.Documentation(); + + [Button] + public void CleanEmptyGroup() => _importSettings.CleanEmptyGroup(); + + #region private methods + + private void OnFilterChanged() + { + + } + + private bool ValidateRule(AddressableImportRule rule,string filter) + { + return string.IsNullOrEmpty(filter) || _filters.Any(x => x(rule,filter)); + } + + private bool ValidateAddressableGroupName(AddressableImportRule rule, string filter) + { + return rule.groupName.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0; + } + + private bool ValidateRulePath(AddressableImportRule rule, string filter) + { + return rule.path.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0; + } + + private bool ValidateLabelsPath(AddressableImportRule rule, string filter) + { + return rule.labels.Any(x => x.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0); + } + + private void FilterRules(string filter) + { + rules = new List(); + var filteredRules = _importSettings.rules. + Where(x => ValidateRule(x, filter)); + rules.AddRange(filteredRules); + } + + private void ApplyChanges() + { + _drawerTree.ApplyChanges(); + + for (var i = 0; i < rules.Count; i++) { + var rule = rules[i]; + var index = _importSettings.rules.IndexOf(rule); + if(index < 0) continue; + _importSettings.rules[index] = rules[i]; + } + + } + + private void CustomAddFunction() + { + _importSettings.rules.Add(new AddressableImportRule()); + _sourceChanged = true; + } + + private void CustomRemoveIndexFunction(int index) + { + var removeResult = _importSettings.rules.Remove(rules[index]); + _sourceChanged = true; + } + + private void CustomRemoveElementFunction(AddressableImportRule item) + { + var index = rules.IndexOf(item); + CustomRemoveIndexFunction(index); + } + + private void EndOfListItemGui(int item) + { + if (GUILayout.Button("remove")) { + CustomRemoveIndexFunction(item); + } + } + + private void OnDisable() + { + if (_drawerTree == null) return; + _drawerTree.OnPropertyValueChanged -= OnPropertyChanged; + _drawerTree.Dispose(); + } + + private void OnPropertyChanged(InspectorProperty property, int index) + { + if (_importSettings == null) return; + EditorUtility.SetDirty(_importSettings); + } + + #endregion + } +#endif +} \ No newline at end of file diff --git a/Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs.meta b/Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs.meta new file mode 100644 index 0000000..861b38d --- /dev/null +++ b/Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 8853cac954044041ba446d5edf848584 +timeCreated: 1591853980 \ No newline at end of file