Skip to content

Commit 513663e

Browse files
committed
Include / Exclude modes
1 parent 3091001 commit 513663e

8 files changed

Lines changed: 260 additions & 8 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/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: 72 additions & 1 deletion
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,73 @@ 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+
49+
string path = AssetDatabase.GetAssetPath(s);
50+
if(string.IsNullOrEmpty(path) || !path.StartsWith("Assets"))
51+
{
52+
return false;
53+
}
54+
55+
foreach(string glob in inclusionGlobs)
56+
{
57+
Regex r = GetRegex(glob);
58+
if(r.IsMatch(path))
59+
{
60+
Debug.Log($"[SpriteAssistSettings] {path} matches {glob}!");
61+
// return true if explicit inclusion is selected, otherwise false for explicit exclusion
62+
return inclusionMode == SpriteAssistInclusionMode.Include;
63+
}
64+
else
65+
{
66+
Debug.Log($"[SpriteAssistSettings] {path} does not match {glob}");
67+
}
68+
}
69+
// fallthru: return true if "include by default", otherwise false
70+
Debug.Log($"[SpriteAssistSettings] No match for {path} found");
71+
return inclusionMode == SpriteAssistInclusionMode.Exclude;
72+
}
73+
74+
protected void OnValidate()
75+
{
76+
// we don't have granular information to decide if the globs have updated
77+
// so just blow these away
78+
compiledRegexes.Clear();
79+
}
80+
81+
private Regex GetRegex(string glob)
82+
{
83+
Regex result = null;
84+
if(!compiledRegexes.TryGetValue(glob, out result))
85+
{
86+
result = CompileGlob(glob);
87+
compiledRegexes.Add(glob, result);
88+
}
89+
return result;
90+
}
91+
92+
private Regex CompileGlob(string glob)
93+
{
94+
glob = "Assets/" + glob;
95+
Debug.Log($"[SpriteAssistSettings] compiling `${glob}`");
96+
return new Regex(
97+
"^" + Regex.Escape(glob).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
98+
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled
99+
);
100+
}
30101
}
31102
}

0 commit comments

Comments
 (0)