I’m loading images from outside unity using Texture2D extension method, LoadImage, everything is working fine, but now I need to load just a portion of some pngs. example: we have a sprite sheet with 16x64 but I want to get just the first image in a 16x16 texture. Any help would be welcome
I tried to change the png size in stream array reader manually, but it does not work.
I also tried to manually add the IEND into the end of the stream, but it does not work too.
Commented code was my last two tries
private static Sprite GetSpriteFromEntry (ZipArchiveEntry zipArchiveEntry)
{
using (var ms = new MemoryStream())
{
// var buffer = new byte[512];
// var count = zipArchiveEntry.Open().Read(buffer, 0, 512);
// buffer[23] = 16;
// ms.Write(buffer, 0, count);
zipArchiveEntry
.Open()
.CopyTo(ms);
// ms.Seek(243, SeekOrigin.Begin);
// ms.Write(endBuffer, 0, 12);
var tex = new Texture2D(2, 2);
tex.LoadImage(ms.ToArray());
return Sprite.Create(tex, new Rect(0f, 0f, tex.width, tex.height), new Vector2(.5f, .5f), 16f);
}
}