Last active
March 15, 2024 15:08
-
-
Save gilzoide/7457e69e5f0f866edfb42822b92988ba to your computer and use it in GitHub Desktop.
Converts legacy sprite packing tags to a 2D Sprite Atlas
This file contains 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.IO; | |
using System.Linq; | |
using UnityEditor; | |
using UnityEditor.U2D; | |
using UnityEngine; | |
// How to use: | |
// 1. Select a texture that has a "Packing Tag" set | |
// 2. Open the context menu in the top-right corner of the inspector | |
// 3. Click "Convert Packing Tag to Atlas" | |
// 4. Wait... | |
// 5. There you go, your atlas was created with the same name as the | |
// packing tag and in the same directory as the texture selected | |
// 6. Edit the new Sprite Atlas configurations if necessary | |
public class SpritePackingTagToAtlas | |
{ | |
[MenuItem("CONTEXT/TextureImporter/Convert Packing Tag to Atlas")] | |
private static void TransformPackingTagIntoAtlas(MenuCommand command) | |
{ | |
TextureImporter thisImporter = (TextureImporter) command.context; | |
string spritePackingTag = thisImporter.spritePackingTag; | |
if (string.IsNullOrEmpty(spritePackingTag)) | |
{ | |
Debug.Log($"[{nameof(SpritePackingTagToAtlas)}] No sprite packing tag found, ignoring."); | |
return; | |
} | |
var importedTextures = AssetDatabase.FindAssets("t:Texture2D", new[] { "Assets" }) | |
.Select(AssetDatabase.GUIDToAssetPath) | |
.Select(path => (Path: path, Importer: AssetImporter.GetAtPath(path) as TextureImporter)) | |
.Where(item => item.Importer && item.Importer.spritePackingTag == spritePackingTag); | |
var allObjs = new List<Object>(); | |
foreach ((string path, TextureImporter importer) in importedTextures) | |
{ | |
Object obj = AssetDatabase.LoadAssetAtPath<Texture>(path); | |
allObjs.Add(obj); | |
importer.spritePackingTag = ""; | |
importer.SaveAndReimport(); | |
} | |
var atlas = new SpriteAtlasAsset(); | |
atlas.Add(allObjs.ToArray()); | |
string atlasPath = $"{Path.GetDirectoryName(thisImporter.assetPath)}/{spritePackingTag}.spriteatlasv2"; | |
SpriteAtlasAsset.Save(atlas, atlasPath); | |
AssetDatabase.ImportAsset(atlasPath); | |
EditorGUIUtility.PingObject(atlas); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment