Skip to content

Commit af79144

Browse files
authored
Merge pull request #68 from konistehrad/include-exclude
Support conditional include / exclude based on globs
2 parents edd7ec9 + b0d75b7 commit af79144

9 files changed

Lines changed: 269 additions & 10 deletions

Assets/Editor/SpriteAssistSettings.asset

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ MonoBehaviour:
1717
prefabRelativePath:
1818
defaultTransparentShaderName: Unlit/Transparent
1919
defaultOpaqueShaderName: Unlit/Texture
20-
defaultThickness: 0
2120
defaultTag: Untagged
2221
defaultLayer: 0
2322
defaultSortingLayerId: 0
2423
defaultSortingOrder: 0
2524
maxThumbnailPreviewCount: 10
2625
enableRenameMeshPrefabAutomatically: 0
26+
inclusionMode: 0
27+
inclusionGlobs:
28+
- Example/Excluded/*

Assets/Example/Excluded.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
53.8 KB
Loading

Assets/Example/Excluded/service-pnp-cph-3b20000-3b24000-3b24100-3b24113r.jpg.meta

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/SpriteAssist/Editor/Import/SpritePostProcessor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@ namespace SpriteAssist
55
{
66
public class SpritePostProcessor : AssetPostprocessor
77
{
8-
private void OnPostprocessSprites(Texture2D _, Sprite[] sprites)
8+
private void OnPostprocessSprites(Texture2D tex, Sprite[] sprites)
99
{
10+
string path = AssetDatabase.GetAssetPath(tex);
11+
if(!SpriteAssistSettings.instance.ShouldProcessSprite(path)) return;
12+
1013
SpriteInspector.isSpriteReloaded = true;
1114

1215
TextureImporter textureImporter = assetImporter as TextureImporter;

Assets/SpriteAssist/Editor/Inspector/SpriteInspector.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,32 @@ public class SpriteInspector : UnityInternalEditor<Sprite>
2121
protected override void OnEnable()
2222
{
2323
base.OnEnable();
24-
25-
SetSpriteProcessor(target, AssetDatabase.GetAssetPath(target));
26-
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
24+
if(SpriteAssistSettings.instance.ShouldProcessSprite(target as Sprite))
25+
{
26+
SetSpriteProcessor(target, AssetDatabase.GetAssetPath(target));
27+
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
28+
}
2729
}
2830

2931
protected override void OnDisable()
3032
{
3133
base.OnDisable();
3234

33-
SpriteProcessor?.Dispose();
34-
AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
35+
if(SpriteProcessor != null)
36+
{
37+
SpriteProcessor?.Dispose();
38+
AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
39+
}
3540
}
3641

3742
public override void OnInspectorGUI()
3843
{
44+
if(SpriteProcessor == null)
45+
{
46+
base.OnInspectorGUI();
47+
return;
48+
}
49+
3950
using (new EditorGUI.DisabledGroupScope(Application.isPlaying))
4051
{
4152
using (var scroll = new EditorGUILayout.ScrollViewScope(_scrollPosition))
@@ -58,7 +69,7 @@ public override void OnPreviewGUI(Rect rect, GUIStyle background)
5869
base.OnPreviewGUI(rect, background);
5970

6071
Sprite sprite = target as Sprite;
61-
if (sprite == null)
72+
if (SpriteProcessor == null || sprite == null)
6273
{
6374
return;
6475
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
namespace SpriteAssist
6+
{
7+
/// <summary>
8+
/// The include/exclude mode for SpriteAssist.
9+
/// </summary>
10+
public enum SpriteAssistInclusionMode
11+
{
12+
/// <summary>
13+
/// All sprites except those listed in settings will be processed
14+
/// </summary>
15+
Exclude,
16+
17+
/// <summary>
18+
/// Only sprites explicitly matched by those in settings will be processed
19+
/// </summary>
20+
Include
21+
}
22+
}

Assets/SpriteAssist/Editor/Settings/SpriteAssistInclusionMode.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/SpriteAssist/Editor/Settings/SpriteAssistSettings.cs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using UnityEditor;
1+
using System.Collections.Generic;
2+
using System.Text.RegularExpressions;
3+
using UnityEditor;
4+
using UnityEngine;
25

36
namespace SpriteAssist
47
{
@@ -27,5 +30,77 @@ public class SpriteAssistSettings : ScriptableSingleton<SpriteAssistSettings>
2730
public int maxThumbnailPreviewCount = THUMBNAIL_COUNT;
2831

2932
public bool enableRenameMeshPrefabAutomatically;
33+
34+
[Tooltip("Controls if sprites are automatically or explicitly marked for processing")]
35+
public SpriteAssistInclusionMode inclusionMode;
36+
37+
[Tooltip("Pattern matching globs to describe included/excluded files. All relative to Assets/.")]
38+
public string[] inclusionGlobs = {};
39+
40+
protected Dictionary<string,Regex> compiledRegexes = new Dictionary<string, Regex>();
41+
42+
public bool ShouldProcessSprite(Sprite s)
43+
{
44+
if(s == null)
45+
{
46+
return false;
47+
}
48+
string path = AssetDatabase.GetAssetPath(s);
49+
return ShouldProcessSprite(path);
50+
}
51+
52+
public bool ShouldProcessSprite(string path)
53+
{
54+
if(string.IsNullOrEmpty(path) || !path.StartsWith("Assets"))
55+
{
56+
return false;
57+
}
58+
59+
foreach(string glob in inclusionGlobs)
60+
{
61+
Regex r = GetRegex(glob);
62+
if(r.IsMatch(path))
63+
{
64+
// Debug.Log($"[SpriteAssistSettings] {path} matches {glob}!");
65+
// return true if explicit inclusion is selected, otherwise false for explicit exclusion
66+
return inclusionMode == SpriteAssistInclusionMode.Include;
67+
}
68+
else
69+
{
70+
// Debug.Log($"[SpriteAssistSettings] {path} does not match {glob}");
71+
}
72+
}
73+
// fallthru: return true if "include by default", otherwise false
74+
// Debug.Log($"[SpriteAssistSettings] No match for {path} found");
75+
return inclusionMode == SpriteAssistInclusionMode.Exclude;
76+
}
77+
78+
protected void OnValidate()
79+
{
80+
// we don't have granular information to decide if the globs have updated
81+
// so just blow these away
82+
compiledRegexes.Clear();
83+
}
84+
85+
private Regex GetRegex(string glob)
86+
{
87+
Regex result = null;
88+
if(!compiledRegexes.TryGetValue(glob, out result))
89+
{
90+
result = CompileGlob(glob);
91+
compiledRegexes.Add(glob, result);
92+
}
93+
return result;
94+
}
95+
96+
private Regex CompileGlob(string glob)
97+
{
98+
glob = "Assets/" + glob;
99+
// Debug.Log($"[SpriteAssistSettings] compiling `${glob}`");
100+
return new Regex(
101+
"^" + Regex.Escape(glob).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
102+
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled
103+
);
104+
}
30105
}
31-
}
106+
}

0 commit comments

Comments
 (0)