Created
November 29, 2020 20:08
-
-
Save Donnotron666/0e34c1b5ee43883c53ce5b040abd9f80 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.U2D; | |
using UnityEngine; | |
namespace Editor.Assets | |
{ | |
public class SpriteImporterAtlasRebuild : AssetPostprocessor | |
{ | |
static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths) | |
{ | |
foreach (var i in importedAssets) | |
{ | |
if (IsSprite(i, out var sprite)) | |
{ | |
UnityEditor.Sprites.Packer.GetAtlasDataForSprite(sprite, out var atlasName, out var tex); | |
if (IsAtlased(sprite, out var atlas)) | |
{ | |
UnityEditor.U2D.SpriteAtlasUtility.PackAtlases(new[] { atlas }, BuildTarget.StandaloneWindows); | |
} | |
} | |
} | |
} | |
static bool IsSprite(string path, out Sprite sprite) | |
{ | |
sprite = AssetDatabase.LoadAssetAtPath<Sprite>(path); | |
return sprite != null; | |
} | |
static bool IsAtlased(Sprite sprite, out UnityEngine.U2D.SpriteAtlas atlas) | |
{ | |
var spritePath = AssetDatabase.GetAssetPath(sprite.GetInstanceID()); | |
foreach (var assetPath in AssetDatabase.FindAssets("t:SpriteAtlas").Select(p => AssetDatabase.GUIDToAssetPath(p))) | |
{ | |
var at = AssetDatabase.LoadAssetAtPath<UnityEngine.U2D.SpriteAtlas>(assetPath); | |
var packed = new List<Object>(at.GetPackables()); | |
foreach( var p in packed) | |
{ | |
if(p == sprite) | |
{ | |
atlas = at; | |
return true; | |
} else if (p is DefaultAsset da) | |
{ | |
//look for folder refs! | |
var folderPath = AssetDatabase.GetAssetPath(p); | |
if(spritePath.Contains(folderPath)) | |
{ | |
atlas = at; | |
return true; | |
} | |
} | |
} | |
} | |
atlas = null; | |
return false; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment