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
4 changes: 3 additions & 1 deletion Assets/Editor/SpriteAssistSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@ MonoBehaviour:
prefabRelativePath:
defaultTransparentShaderName: Unlit/Transparent
defaultOpaqueShaderName: Unlit/Texture
defaultThickness: 0
defaultTag: Untagged
defaultLayer: 0
defaultSortingLayerId: 0
defaultSortingOrder: 0
maxThumbnailPreviewCount: 10
enableRenameMeshPrefabAutomatically: 0
inclusionMode: 0
inclusionGlobs:
- Example/Excluded/*
8 changes: 8 additions & 0 deletions Assets/Example/Excluded.meta

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

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

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

5 changes: 4 additions & 1 deletion Assets/SpriteAssist/Editor/Import/SpritePostProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ namespace SpriteAssist
{
public class SpritePostProcessor : AssetPostprocessor
{
private void OnPostprocessSprites(Texture2D _, Sprite[] sprites)
private void OnPostprocessSprites(Texture2D tex, Sprite[] sprites)
{
string path = AssetDatabase.GetAssetPath(tex);
if(!SpriteAssistSettings.instance.ShouldProcessSprite(path)) return;

SpriteInspector.isSpriteReloaded = true;

TextureImporter textureImporter = assetImporter as TextureImporter;
Expand Down
23 changes: 17 additions & 6 deletions Assets/SpriteAssist/Editor/Inspector/SpriteInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,32 @@ public class SpriteInspector : UnityInternalEditor<Sprite>
protected override void OnEnable()
{
base.OnEnable();

SetSpriteProcessor(target, AssetDatabase.GetAssetPath(target));
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
if(SpriteAssistSettings.instance.ShouldProcessSprite(target as Sprite))
{
SetSpriteProcessor(target, AssetDatabase.GetAssetPath(target));
AssemblyReloadEvents.afterAssemblyReload += OnAfterAssemblyReload;
}
}

protected override void OnDisable()
{
base.OnDisable();

SpriteProcessor?.Dispose();
AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
if(SpriteProcessor != null)
{
SpriteProcessor?.Dispose();
AssemblyReloadEvents.afterAssemblyReload -= OnAfterAssemblyReload;
}
}

public override void OnInspectorGUI()
{
if(SpriteProcessor == null)
{
base.OnInspectorGUI();
return;
}

using (new EditorGUI.DisabledGroupScope(Application.isPlaying))
{
using (var scroll = new EditorGUILayout.ScrollViewScope(_scrollPosition))
Expand All @@ -58,7 +69,7 @@ public override void OnPreviewGUI(Rect rect, GUIStyle background)
base.OnPreviewGUI(rect, background);

Sprite sprite = target as Sprite;
if (sprite == null)
if (SpriteProcessor == null || sprite == null)
{
return;
}
Expand Down
22 changes: 22 additions & 0 deletions Assets/SpriteAssist/Editor/Settings/SpriteAssistInclusionMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace SpriteAssist
{
/// <summary>
/// The include/exclude mode for SpriteAssist.
/// </summary>
public enum SpriteAssistInclusionMode
{
/// <summary>
/// All sprites except those listed in settings will be processed
/// </summary>
Exclude,

/// <summary>
/// Only sprites explicitly matched by those in settings will be processed
/// </summary>
Include
}
}

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

81 changes: 78 additions & 3 deletions Assets/SpriteAssist/Editor/Settings/SpriteAssistSettings.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using UnityEditor;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;

namespace SpriteAssist
{
[FilePath(SETTINGS_PATH, FilePathAttribute.Location.PreferencesFolder)]
[FilePath(SETTINGS_PATH, FilePathAttribute.Location.ProjectFolder)]
public class SpriteAssistSettings : ScriptableSingleton<SpriteAssistSettings>
{
private const string SETTINGS_PATH = "Assets/Editor/SpriteAssistSettings.asset";
Expand All @@ -27,5 +30,77 @@ public class SpriteAssistSettings : ScriptableSingleton<SpriteAssistSettings>
public int maxThumbnailPreviewCount = THUMBNAIL_COUNT;

public bool enableRenameMeshPrefabAutomatically;

[Tooltip("Controls if sprites are automatically or explicitly marked for processing")]
public SpriteAssistInclusionMode inclusionMode;

[Tooltip("Pattern matching globs to describe included/excluded files. All relative to Assets/.")]
public string[] inclusionGlobs = {};

protected Dictionary<string,Regex> compiledRegexes = new Dictionary<string, Regex>();

public bool ShouldProcessSprite(Sprite s)
{
if(s == null)
{
return false;
}
string path = AssetDatabase.GetAssetPath(s);
return ShouldProcessSprite(path);
}

public bool ShouldProcessSprite(string path)
{
if(string.IsNullOrEmpty(path) || !path.StartsWith("Assets"))
{
return false;
}

foreach(string glob in inclusionGlobs)
{
Regex r = GetRegex(glob);
if(r.IsMatch(path))
{
// Debug.Log($"[SpriteAssistSettings] {path} matches {glob}!");
// return true if explicit inclusion is selected, otherwise false for explicit exclusion
return inclusionMode == SpriteAssistInclusionMode.Include;
}
else
{
// Debug.Log($"[SpriteAssistSettings] {path} does not match {glob}");
}
}
// fallthru: return true if "include by default", otherwise false
// Debug.Log($"[SpriteAssistSettings] No match for {path} found");
return inclusionMode == SpriteAssistInclusionMode.Exclude;
}

protected void OnValidate()
{
// we don't have granular information to decide if the globs have updated
// so just blow these away
compiledRegexes.Clear();
}

private Regex GetRegex(string glob)
{
Regex result = null;
if(!compiledRegexes.TryGetValue(glob, out result))
{
result = CompileGlob(glob);
compiledRegexes.Add(glob, result);
}
return result;
}

private Regex CompileGlob(string glob)
{
glob = "Assets/" + glob;
// Debug.Log($"[SpriteAssistSettings] compiling `${glob}`");
return new Regex(
"^" + Regex.Escape(glob).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled
);
}
}
}
}