Hello, I am still very new to working with Unity and have been learning as I go but here goes:
I am working on the process of taking a 3D Animated character and making a sprite sheet texture in batch of all its animations, this is working fine enough for now.
I wrote a script to set the proper settings on the texture and to slice it into the appropriate sprites, this mostly works.
The issue I have is that when a sprite sheet has “empty” space (ex. 6x6 texture of 100x100x sprites, but only 28 sprites leaving 8 “empty” spaces), these empty sprites are being generated:
If I simply re-slice it with the Sprite editor it respects the empty space as I would:
During my research I have found several ways to attempt slicing, but I don’t see this happening with others people using the scripts. I even went as far as replacing most of mine with snippets from others to get to some common ground. I spent a while researching for a way to validate each sprite before finalize the metadata with no luck? Is there a way to use the Sprite Editor to perform the slice operation in a script? or Perhaps a better way to approach this problem?
static void Slicer()
{
string[] textureGuids = AssetDatabase.FindAssets("t:texture2D", new[] {"Assets/Textures"});
int SliceSize = 100;
foreach (string guid in textureGuids)
{
var path = AssetDatabase.GUIDToAssetPath(guid);
TextureImporter importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer == null)
{
Debug.Log("Null Importer: " + path);
}
else
{
if (importer.isReadable != true || importer.filterMode != FilterMode.Point || importer.spriteImportMode != SpriteImportMode.Multiple || importer.textureType != TextureImporterType.Sprite)
{
importer.isReadable = true;
importer.filterMode = FilterMode.Point;
importer.spriteImportMode = SpriteImportMode.Multiple;
importer.textureType = TextureImporterType.Sprite;
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
var textureSettings = new TextureImporterSettings();
importer.ReadTextureSettings(textureSettings);
textureSettings.spriteMeshType = SpriteMeshType.Tight;
textureSettings.spriteExtrude = 0;
importer.SetTextureSettings(textureSettings);
}
List<SpriteMetaData> smdList = new List<SpriteMetaData>();
Texture2D curTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D));
for (int i = 0; i < curTexture.width; i += SliceSize)
{
for (int j = curTexture.height; j > 0; j -= SliceSize)
{
SpriteMetaData smd = new SpriteMetaData();
smd.pivot = new Vector2(0.5f, 0.5f);
smd.alignment = 9;
smd.name = (curTexture.height - j) / SliceSize + ", " + i / SliceSize;
smd.rect = new Rect(i, j - SliceSize, SliceSize, SliceSize);
smdList.Add(smd);
}
}
importer.spritesheet = smdList.ToArray();
AssetDatabase.ForceReserializeAssets(new List<string>() { path });
AssetDatabase.ImportAsset(path, ImportAssetOptions.ForceUpdate);
AssetDatabase.SaveAssets();
}
}
}

