-
-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
Description
I was trying to create an application that saves images in the QOI format at runtime.
Unfortunately, I ran into an issue where EncodeToQOI works correctly only in the Unity Editor, but fails in Runtime builds.
Unity throws an error stating that it cannot access the importer from runtime code, which makes the current implementation unusable in a built application.
Because of this limitation, I implemented my own runtime-compatible extension method for Texture2D.EncodeToQOI, which directly uses the encoder and works correctly in Runtime. This might be useful for others or as a reference for adding official runtime support.
Runtime-compatible implementation
using UnityEngine;
using Qoi.Csharp;
using Channels = Qoi.Csharp.Channels;
using ColorSpace = Qoi.Csharp.ColorSpace;
public static class QOIRuntime
{
static bool HasAlpha(Color32[] pixels)
{
for (int i = 0; i < pixels.Length; i++)
{
if (pixels[i].a < 255)
return true;
}
return false;
}
public static byte[] EncodeToQOI(this Texture2D tex)
{
Color32[] pixels = tex.GetPixels32();
bool hasAlpha = HasAlpha(pixels);
Channels channels = hasAlpha ? Channels.Rgba : Channels.Rgb;
byte[] data = hasAlpha ? tex.GetByteArray32() : tex.GetByteArray24();
return Encoder.Encode(data, tex.width, tex.height, channels, ColorSpace.SRgb);
}
}Suggestion
It might be helpful to:
- clearly document that
EncodeToQOIis Editor-only,
or - add official Runtime support that does not rely on the Unity importer
Metadata
Metadata
Assignees
Labels
No labels