Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ namespace SmartAddresser.Editor.Core.Models.Shared.AssetGroups.AssetFilterImpl
public sealed class ExtensionBasedAssetFilter : AssetFilterBase
{
[SerializeField] private StringListableProperty _extension = new StringListableProperty();
[SerializeField] private bool _invertMatch;
private List<string> _extensions = new List<string>();
private bool _hasEmptyExtension;

Expand All @@ -27,6 +28,15 @@ public sealed class ExtensionBasedAssetFilter : AssetFilterBase
/// </summary>
public StringListableProperty Extension => _extension;

/// <summary>
/// If true, the result of the match will be inverted.
/// </summary>
public bool InvertMatch
{
get => _invertMatch;
set => _invertMatch = value;
}

public override void SetupForMatching()
{
_extensions.Clear();
Expand Down Expand Up @@ -63,12 +73,16 @@ public override bool IsMatch(string assetPath, Type assetType, bool isFolder)
{
if (string.IsNullOrEmpty(assetPath)) return false;

var matchFound = false;
foreach (var extension in _extensions)
// Return true if any of the extensions match.
if (assetPath.EndsWith(extension, StringComparison.Ordinal))
return true;
{
matchFound = true;
break;
}

return false;
return matchFound ^ _invertMatch;
}

public override string GetDescription()
Expand Down Expand Up @@ -96,6 +110,14 @@ public override string GetDescription()
result.Insert(0, "Extension: ");
}

if (_invertMatch)
{
if (result.Length == 0)
return "Not ( Nothing )";

result.Insert(0, "Not ");
}

return result.ToString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public sealed class TypeBasedAssetFilter : AssetFilterBase
{
[SerializeField] private TypeReferenceListableProperty _type = new TypeReferenceListableProperty();
[SerializeField] private bool _matchWithDerivedTypes = true;
[SerializeField] private bool _invertMatch;
private List<string> _invalidAssemblyQualifiedNames = new List<string>();

private Dictionary<Type, bool> _resultCache = new Dictionary<Type, bool>();
Expand All @@ -33,6 +34,15 @@ public bool MatchWithDerivedTypes
set => _matchWithDerivedTypes = value;
}

/// <summary>
/// If true, the result of the match will be inverted.
/// </summary>
public bool InvertMatch
{
get => _invertMatch;
set => _invertMatch = value;
}

public override void SetupForMatching()
{
_types.Clear();
Expand Down Expand Up @@ -79,8 +89,9 @@ public override bool IsMatch(string assetPath, Type assetType, bool isFolder)
return false;

if (_resultCache.TryGetValue(assetType, out var result))
return result;
return result ^ _invertMatch;

result = false;
foreach (var type in _types)
{
if (type == assetType)
Expand All @@ -101,7 +112,7 @@ public override bool IsMatch(string assetPath, Type assetType, bool isFolder)
_resultCache.Add(assetType, result);
}

return result;
return result ^ _invertMatch;
}

public override string GetDescription()
Expand Down Expand Up @@ -134,6 +145,14 @@ public override string GetDescription()
if (MatchWithDerivedTypes)
result.Append(" and derived types");

if (_invertMatch)
{
if (result.Length == 0)
return "Not ( Nothing )";

result.Insert(0, "Not ");
}

return result.ToString();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public override void Setup(object target)

protected override void GUILayout(ExtensionBasedAssetFilter target)
{
target.InvertMatch =
EditorGUILayout.Toggle(ObjectNames.NicifyVariableName(nameof(Target.InvertMatch)),
Target.InvertMatch);
_listablePropertyGUI.DoLayout();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public override void Setup(object target)

protected override void GUILayout(TypeBasedAssetFilter target)
{
target.InvertMatch =
EditorGUILayout.Toggle(ObjectNames.NicifyVariableName(nameof(Target.InvertMatch)),
Target.InvertMatch);
target.MatchWithDerivedTypes = EditorGUILayout.Toggle(
ObjectNames.NicifyVariableName(nameof(Target.MatchWithDerivedTypes)), Target.MatchWithDerivedTypes);
_listablePropertyGUI.DoLayout();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ public void IsMatch_RegisterExtensionsAndNotContainsMatched_ReturnFalse()
Assert.That(filter.IsMatch("Test.png", typeof(Texture2D), false), Is.False);
}

[Test]
public void IsMatch_InvertMatchAndRegisterMatchedExtension_ReturnFalse()
{
var filter = new ExtensionBasedAssetFilter();
filter.Extension.Value = "png";
filter.InvertMatch = true;
filter.SetupForMatching();
Assert.That(filter.IsMatch("Test.png", typeof(Texture2D), false), Is.False);
}

[Test]
public void IsMatch_InvertMatchAndRegisterNotMatchedExtension_ReturnTrue()
{
var filter = new ExtensionBasedAssetFilter();
filter.Extension.Value = "jpg";
filter.InvertMatch = true;
filter.SetupForMatching();
Assert.That(filter.IsMatch("Test.png", typeof(Texture2D), false), Is.True);
}

[Test]
public void Validate_ValidExtension_ReturnTrue()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ public void IsMatch_NotContainsMatched_ReturnTrue()
Assert.That(filter.IsMatch("Assets/Test.png", typeof(Texture2DArray), false), Is.False);
}

[Test]
public void IsMatch_InvertMatchAndSetMatchedType_ReturnFalse()
{
var filter = new TypeBasedAssetFilter();
filter.Type.Value = TypeReference.Create(typeof(Texture2D));
filter.InvertMatch = true;
filter.SetupForMatching();
Assert.That(filter.IsMatch("Assets/Test.png", typeof(Texture2D), false), Is.False);
}

[Test]
public void IsMatch_InvertMatchAndSetNotMatchedType_ReturnTrue()
{
var filter = new TypeBasedAssetFilter();
filter.Type.Value = TypeReference.Create(typeof(Texture3D));
filter.InvertMatch = true;
filter.SetupForMatching();
Assert.That(filter.IsMatch("Assets/Test.png", typeof(Texture2D), false), Is.True);
}

[Test]
public void Validate_Valid_ReturnTrue()
{
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -669,9 +669,9 @@ public static class Example
| Name | Overview and Description |
|-------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| Object Filter | Filters by specifying the asset directly.<br>Use case: Target only assets under the Characters folder.<br><br>**Folder Targeting Mode**<br>Specify how folders are handled.<br>* Included Assets (Exclude Folders): Targets only assets in the folders.<br>* Self: Targets only the folders themselves.<br>* Both: Targets the folders and the included assets.<br><br>**Object**<br>Target asset. If you specify a folder, target all assets in the folder. Multiple items can be specified by using the right toggle. |
| Type Filter | Filters by asset type.<br>Use case: Targets only Texture2d type assets.<br><br>**Match With Derived Type**<br>If checked, derived types are also covered.<br><br>**Type**<br>Target type. Multiple items can be specified by using the right toggle. |
| Type Filter | Filters by asset type.<br>Use case: Targets only Texture2d type assets.<br><br>**Match With Derived Type**<br>If checked, derived types are also covered.<br><br>**Type**<br>Target type. Multiple items can be specified by using the right toggle.<br><br>**Invert Match**<br>If checked, assets that do not match the conditions are targeted. |
| Asset Path Filter | Filters by asset path.<br>Use case 1: Target assets contained in the folder named "Assets/Sample[Any 3 characters]/".<br>Use case 2: Exclude assets that contain "Dummy" in the file name.<br><br>**Match With Folders**<br>Targets the folders or not.<br><br>**Asset Path (Regex)**<br>Target asset path. Assets whose paths partially match these will be targeted. You can also use regular expressions. Multiple items can be specified by using the right toggle.<br><br>**Condition**<br>Specify how to handle multiple Asset Paths.<br>* Contains Matched: Target if any asset path matches.<br>* Match All: Target if all asset paths match.<br>* Contains Unmatched: Target if any one asset path does not match.<br>* Not Match All: Target if all asset paths do not match. |
| Extension Filter | Filters by extension.<br>Use case: Target assets that have png or jpg extension.<br><br>**Extension**<br>Target extension. Multiple items can be specified by using the right toggle. |
| Extension Filter | Filters by extension.<br>Use case: Target assets that have png or jpg extension.<br><br>**Extension**<br>Target extension. Multiple items can be specified by using the right toggle.<br><br>**Invert Match**<br>If checked, assets that do not match the conditions are targeted. |
| Dependent Object Filter | Filters the assets that are referenced by the specified asset.<br>Use case: Targets the textures that are referenced by the prefab.<br><br>**Only Direct Dependencies**<br>Targets the assets that are referenced directly.<br><br>**Object**<br>Referer Assets. |
| Find Assets Filter | Filters by using AssetDatabase.FindAssets().<br/><br/>**Filter**<br>Filter string to be passed to AssetDatabase.Find()<br><br>**Target Folders**<br>The folder to be searched.<br>If not specified, all folders will be searched. |

Expand Down
Loading