Image Loading Guide¶
This guide covers how to use Uniasset.Image.ImageAsset to load, process, and convert image assets.
Supported formats¶
Uniasset currently supports:
| Format | Extensions | Notes |
|---|---|---|
| JPEG | .jpg, .jpeg |
Lossy compression, good for photos |
| WebP | .webp |
Supports lossy and lossless compression |
| PNG | .png |
Lossless compression with alpha |
| BMP | .bmp |
Broad compatibility |
| TGA | .tga |
Common in game pipelines |
| PSD | .psd |
Photoshop source files |
Basic loading¶
Load from file¶
using Uniasset.Image;
using var image = new ImageAsset();
image.Load("Assets/Images/photo.png");
Debug.Log($"Image size: {image.Width} x {image.Height}");
Debug.Log($"Channels: {image.ChannelCount}");
Load from a byte array¶
Load from a stream¶
using Uniasset.Image;
using var image = new ImageAsset();
using var stream = File.OpenRead("photo.png");
image.LoadIO(stream);
Load asynchronously¶
Note
Asynchronous loading currently exists for LoadAsync(string) and LoadAsync(byte[]). LoadIO(...) has no async overload in this guide's scope.
Decode directly to a target size¶
You can pass expectedWidth and expectedHeight during decode to reduce later memory and processing overhead:
If you do not need resize-on-decode, pass 0:
Warning
expectedWidth and expectedHeight must not be negative, or ArgumentOutOfRangeException will be thrown.
Image processing¶
Crop¶
Crop asynchronously¶
Batch crop¶
Useful for extracting multiple sprites from an atlas:
var crops = new CropOptions[]
{
new CropOptions(0, 0, 32, 32),
new CropOptions(32, 0, 32, 32),
new CropOptions(64, 0, 32, 32),
new CropOptions(96, 0, 32, 32),
};
ImageAsset[] frames = image.CropMultiple(crops);
Batch crop asynchronously¶
Resize¶
Resize asynchronously¶
Convert to Texture2D¶
Synchronous conversion¶
Asynchronous conversion¶
Parameters¶
| Parameter | Description |
|---|---|
mipmap |
Whether to generate mipmaps |
linear |
Whether to create the texture in linear color space |
noLongerReadable |
Whether to make the texture unreadable after upload |
Note
ToTexture2D() and ToTexture2DAsync() only support image data with 3 or 4 channels.
Clone¶
This returns a new ImageAsset instance that can be processed independently.
Lifetime management¶
Use using¶
using var image = new ImageAsset();
image.Load("photo.png");
Texture2D texture = image.ToTexture2D();
Unload loaded data¶
Unload() releases the current image content but keeps the ImageAsset instance reusable.
Common use cases¶
Load a UI image¶
public class ImageLoader : MonoBehaviour
{
[SerializeField] private RawImage _rawImage;
private ImageAsset _imageAsset;
private async void Start()
{
_imageAsset = new ImageAsset();
await _imageAsset.LoadAsync("Assets/UI/background.png");
_rawImage.texture = await _imageAsset.ToTexture2DAsync();
}
private void OnDestroy()
{
_imageAsset?.Dispose();
}
}
Load and square-crop an avatar¶
public async Task<Texture2D> LoadAvatar(string path, int size)
{
using var image = new ImageAsset();
await image.LoadAsync(path);
int minDim = Math.Min(image.Width, image.Height);
int x = (image.Width - minDim) / 2;
int y = (image.Height - minDim) / 2;
image.Crop(x, y, minDim, minDim);
image.Resize(size, size);
return await image.ToTexture2DAsync();
}
Slice a sprite sheet¶
public async Task<Texture2D[]> LoadSpriteSheet(string path, int spriteWidth, int spriteHeight)
{
using var sheet = new ImageAsset();
await sheet.LoadAsync(path);
int cols = sheet.Width / spriteWidth;
int rows = sheet.Height / spriteHeight;
var crops = new CropOptions[cols * rows];
for (int y = 0; y < rows; y++)
{
for (int x = 0; x < cols; x++)
{
crops[y * cols + x] = new CropOptions(
x * spriteWidth,
y * spriteHeight,
spriteWidth,
spriteHeight
);
}
}
ImageAsset[] sprites = sheet.CropMultiple(crops);
try
{
var textures = new Texture2D[sprites.Length];
for (int i = 0; i < sprites.Length; i++)
{
textures[i] = sprites[i].ToTexture2D();
}
return textures;
}
finally
{
foreach (var sprite in sprites)
{
sprite.Dispose();
}
}
}