Skip to content

Commit

Permalink
feat: ODIN inspector search filter (#35)
Browse files Browse the repository at this point in the history
* - 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

* #33 fixes

* - add search filter for Odin inspector

* - update addressables import serach filter

* #33
- review fixes

* - fix review comments

* #33

* - fix odin serach filter support

* - fix importer search filter

* - fix ApplyChanges index

* - fix duplicate class def
  • Loading branch information
Mefodei authored Jul 28, 2020
1 parent 4e69be0 commit 6d8022c
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 55 deletions.
6 changes: 3 additions & 3 deletions Editor/AddressableImportRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class AddressableImportRule
/// Path pattern.
/// </summary>
[Tooltip("The assets in this path will be processed.")]
public string path;
public string path = string.Empty;

/// <summary>
/// Method used to parse the Path.
Expand All @@ -54,7 +54,7 @@ public class AddressableImportRule
/// The group the asset will be added.
/// </summary>
[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;

/// <summary>
/// Cleaned group name.
Expand Down Expand Up @@ -100,7 +100,7 @@ string CleanedGroupName {
/// </summary>
[Tooltip("Replacement address string for regex matches.")]
[ConditionalField("matchType", AddressableImportRuleMatchType.Regex, "simplified", false)]
public string addressReplacement;
public string addressReplacement = string.Empty;

public bool HasLabel
{
Expand Down
6 changes: 3 additions & 3 deletions Editor/AddressableImportSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ public class AddressableImportSettings : ScriptableObject
public List<AddressableImportRule> 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)
Expand Down
15 changes: 11 additions & 4 deletions Editor/Helper/AddressableImportSettingsEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@ namespace UnityAddressableImporter.Helper.Internal
public class AddressableImportSettingsEditor : Editor
{
private List<MethodInfo> _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()
Expand All @@ -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()
Expand Down
44 changes: 0 additions & 44 deletions Editor/Helper/AddressableImporterOdinHandler.cs

This file was deleted.

8 changes: 8 additions & 0 deletions Editor/OdinSupport.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 65 additions & 0 deletions Editor/OdinSupport/AddressableImporterOdinHandler.cs
Original file line number Diff line number Diff line change
@@ -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<AddressablesImporterFilterOdinHandler>();
_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
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

163 changes: 163 additions & 0 deletions Editor/OdinSupport/AddressablesImporterFilterOdinHandler.cs
Original file line number Diff line number Diff line change
@@ -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<Func<AddressableImportRule, string, bool>> _filters;
//private List<AddressableImportRule> _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<AddressableImportRule> rules = new List<AddressableImportRule>();

public void Initialize(AddressableImportSettings importSettings)
{
_importSettings = importSettings;
_drawerTree = PropertyTree.Create(this);

_filters = new List<Func<AddressableImportRule, string, bool>>() {
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<AddressableImportRule>();
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
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6d8022c

Please sign in to comment.