I’m trying to finish up an AssetPostProcessor that will automatically slice and create the sprites from a spritesheet based on parameters in the filename.
I have everything working so that the spritesheet does get sliced appropriately and those sprites display correctly in the sprite editor. However, the asset itself does not appear fully imported as it does not have the dropdown arrow with the sprite list in it yet. In order to get that, I have to right click on the asset and select “Reimport”. This results in the expected behavior, but I get some exceptions related to physics outlines…
- How can I make sure the texture and sprites are fully processed without having to reimport a second time?
- Does anyone know why I might get these exceptions for physics outlines when generating sprite slices from an editor script?
Processor Code
private void OnPostprocessTexture(Texture2D texture)
{
AssetDetails details = GetTextureDetails();
if (details.isSpritesheet && details.hasDimens)
{
TextureImporter importer = (TextureImporter) assetImporter;
bool success = GetImageSize(assetPath, out int imageWidth, out int imageHeight);
if (success)
{
List<SpriteMetaData> metaDatas = new List<SpriteMetaData>();
int spriteWidth = imageWidth / details.dimens.cols;
int spriteHeight = imageHeight / details.dimens.rows;
string filename = Path.GetFileNameWithoutExtension(assetPath);
int spriteCount = 0;
for (int row = 0; row < details.dimens.rows; ++row)
{
for (int col = 0; col < details.dimens.cols; ++col)
{
SpriteMetaData metaData = new SpriteMetaData()
{
rect = new Rect(col * spriteWidth, row * spriteHeight, spriteWidth, spriteHeight),
name = $"{filename}_{spriteCount}"
};
metaDatas.Add(metaData);
++spriteCount;
}
}
importer.spritesheet = metaDatas.ToArray();
}
}
}
Exceptions