Skip to content

Commit

Permalink
Trick out the sprite atlas to update
Browse files Browse the repository at this point in the history
  • Loading branch information
Seanba committed Apr 8, 2019
1 parent 0fa8e69 commit 88782ef
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 11 deletions.
Expand Up @@ -6,22 +6,55 @@
using UnityEditor;
using UnityEditor.U2D;
using UnityEngine.U2D;
using System.IO;

namespace Aseprite2Unity.Editor
{
public class AseAssetPostprocessor : AssetPostprocessor
{
// We need to use a "marker" to trick-out the sprite atlas into updating itself
// Note: Unity has a fix for, reportedly coming in 2019.2.0a11

private const string MarkerFileNameWithoutExtension = "aseprite2unity-atlas-marker";
private static string[] OurAssetExtensions = new string[] { ".ase", ".aseprite" };

private void OnPreprocessAsset()
{
// Sprite atlases will not update themselves trough script unless we "dirty" our atlas marker
// (Remove this hack when Unity releases a better fix)
if (assetPath.ToLower().Contains(MarkerFileNameWithoutExtension))
{
// Simply add some unique user data to the marker.
// Use current time as context to a human combined with a unique GUID.
assetImporter.userData = string.Format("{{ {0}, {1} }}", Guid.NewGuid(), DateTime.Now.ToString());
}
}

private static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
{
#if UNITY_2018_3_OR_NEWER
foreach (var ase in importedAssets.Where(a => IsAseAsset(a)))
foreach (var ase in importedAssets.Where(a => IsOurAsset(a)))
{
// Does our aseprite asset use sprite atlases?
var atlases = AssetDatabase.GetDependencies(ase).Where(d => IsSpriteAtlasAsset(d));

if (atlases.Count() > 0)
{
// Add the sprites within our Ase asset to each sprite atlas
var sprites = AssetDatabase.LoadAllAssetsAtPath(ase).OfType<Sprite>();
// Add the sprites within our Ase asset to the sprite atlas
var sprites = AssetDatabase.LoadAllAssetsAtPath(ase).OfType<Sprite>().ToList();

// Make sure our special marker is added to the sprite atlas
// Future edits to the aseprite file will not be added to the sprite atlas without this
// (This is a bit of a hack that Unity is fixing in future versions)
var marker = FindSpriteAtlasMarker();
if (marker == null)
{
Debug.LogErrorFormat("Aseprite2Unity marker '{0}' not found. Check your Aseprite2Unity installation. Sprite Atlases may not work correctly without this marker.", MarkerFileNameWithoutExtension);
}
else
{
sprites.Add(marker);
}

foreach (var path in atlases)
{
Expand All @@ -30,21 +63,52 @@ private static void OnPostprocessAllAssets(string[] importedAssets, string[] del
// Do not add sprites already in the atlas
var alreadedPacked = atlas.GetPackables().OfType<Sprite>();
var spritesToAdd = sprites.Except(alreadedPacked).ToArray();
atlas.Add(spritesToAdd);
atlas.Add(spritesToAdd.ToArray());
}

// The marker needs to be re-imported so that the sprite atlas is updated
if (marker != null)
{
var markerPath = AssetDatabase.GetAssetPath(marker);
AssetDatabase.ImportAsset(markerPath, ImportAssetOptions.ForceUpdate);
}
}
}
#endif
}

private static bool IsAseAsset(string path)
private static bool IsOurAsset(string path)
{
return path.EndsWith(".ase", StringComparison.OrdinalIgnoreCase) || path.EndsWith(".aseprite", StringComparison.OrdinalIgnoreCase);
foreach (var ext in OurAssetExtensions)
{
if (path.EndsWith(ext, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}

return false;
}

private static bool IsSpriteAtlasAsset(string path)
{
return path.EndsWith(".spriteatlas", StringComparison.OrdinalIgnoreCase);
}

private static Sprite FindSpriteAtlasMarker()
{
foreach (var guid in AssetDatabase.FindAssets(MarkerFileNameWithoutExtension))
{
var path = AssetDatabase.GUIDToAssetPath(guid);
var marker = AssetDatabase.LoadAssetAtPath<Sprite>(path);

if (marker != null)
{
return marker;
}
}

return null;
}
}
}
Expand Up @@ -23,7 +23,8 @@ public AseColorProfileChunk(AseFrame frame, AseReader reader)
// Next 8 bytes are reserved
reader.ReadBYTEs(8);

// fixit - what to do with color profile data?
// Do we need to do anything with color profile data?
// For now, just keep on truckin'
if (ColorProfileType == ColorProfileType.EmbeddedICC)
{
var length = (int)reader.ReadDWORD();
Expand All @@ -33,7 +34,6 @@ public AseColorProfileChunk(AseFrame frame, AseReader reader)

public override void Visit(IAseVisitor visitor)
{
// fixit - nothing for now
}
}
}
Expand Up @@ -115,7 +115,7 @@ public void BeginFrameVisit(AseFrame frame)
m_Texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
m_Texture2D.wrapMode = TextureWrapMode.Clamp;
m_Texture2D.filterMode = FilterMode.Point;
m_Texture2D.name = string.Format("tex2d_{0}", m_Frames.Count);
m_Texture2D.name = string.Format("{0}_tex2d_{1}", Path.GetFileNameWithoutExtension(assetPath), m_Frames.Count);

// Texture starts off blank
m_Texture2D.SetPixels(0, 0, width, height, m_ClearPixels);
Expand All @@ -135,7 +135,7 @@ public void EndFrameVisit(AseFrame frame)
// Make a sprite out of the texture
var pivot = m_Pivot ?? new Vector2(0.5f, 0.5f);
var sprite = Sprite.Create(m_Texture2D, new Rect(0, 0, m_Texture2D.width, m_Texture2D.height), pivot, m_PixelsPerUnit);
sprite.name = string.Format("sprite_{0}", m_Sprites.Count);
sprite.name = string.Format("{0}_sprite_{1}", Path.GetFileNameWithoutExtension(assetPath), m_Sprites.Count);
m_Sprites.Add(sprite);
m_Context.AddObjectToAsset(sprite.name, sprite);
}
Expand Down
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.

6 changes: 6 additions & 0 deletions Aseprite2Unity/ProjectSettings/ProjectSettings.asset
Expand Up @@ -106,6 +106,11 @@ PlayerSettings:
xboxOneDisableEsram: 0
xboxOnePresentImmediateThreshold: 0
switchQueueCommandMemory: 0
switchQueueControlMemory: 16384
switchQueueComputeMemory: 262144
switchNVNShaderPoolsGranularity: 33554432
switchNVNDefaultPoolsGranularity: 16777216
switchNVNOtherPoolsGranularity: 16777216
vulkanEnableSetSRGBWrite: 0
m_SupportedAspectRatios:
4:3: 1
Expand Down Expand Up @@ -389,6 +394,7 @@ PlayerSettings:
switchAllowsRuntimeAddOnContentInstall: 0
switchDataLossConfirmation: 0
switchUserAccountLockEnabled: 0
switchSystemResourceMemory: 16777216
switchSupportedNpadStyles: 3
switchNativeFsCacheSize: 32
switchIsHoldTypeHorizontal: 0
Expand Down
2 changes: 1 addition & 1 deletion Aseprite2Unity/ProjectSettings/ProjectVersion.txt
@@ -1 +1 @@
m_EditorVersion: 2018.3.0f2
m_EditorVersion: 2018.3.5f1

0 comments on commit 88782ef

Please sign in to comment.