ImageAsset¶
Uniasset.Image.ImageAsset is the high-level image wrapper for loading, cropping, resizing, cloning, and converting image data into Unity Texture2D objects.
Definition¶
Constructor¶
Creates an empty ImageAsset instance.
Read-only properties¶
| Property | Type | Description |
|---|---|---|
Width |
int |
Current image width |
Height |
int |
Current image height |
ChannelCount |
int |
Number of channels in the current image |
PixelType |
PixelType |
Current pixel layout |
Common ChannelCount values:
3: RGB4: RGBA
Loading¶
Load¶
Loads an image from a file path.
| Parameter | Type | Default | Description |
|---|---|---|---|
path |
string |
— | Image file path |
expectedWidth |
int |
0 |
Requested decoded width; 0 keeps the original width |
expectedHeight |
int |
0 |
Requested decoded height; 0 keeps the original height |
Negative expectedWidth or expectedHeight values throw ArgumentOutOfRangeException.
Load(Span<byte>)¶
Loads from encoded image bytes in memory.
| Parameter | Type | Default | Description |
|---|---|---|---|
data |
Span<byte> |
— | Encoded image bytes |
expectedWidth |
int |
0 |
Requested decoded width |
expectedHeight |
int |
0 |
Requested decoded height |
LoadAsync¶
Asynchronously loads an image from a file path.
LoadAsync(byte[])¶
Asynchronously loads from byte[].
LoadIO¶
Loads from IUniassetStream.
LoadIO(Stream)¶
Loads from System.IO.Stream. Internally, Uniasset wraps it with StreamWrapper.
LoadIOAsync¶
public Task LoadIOAsync(IUniassetStream stream, int expectedWidth = 0, int expectedHeight = 0)
public Task LoadIOAsync(Stream stream, int expectedWidth = 0, int expectedHeight = 0)
Asynchronously loads from a stream.
Lifetime¶
Unload¶
Unloads the currently decoded image while keeping the ImageAsset instance reusable.
Dispose¶
Releases the underlying native resources. The instance should not be used afterward.
Image processing¶
Crop¶
Crops the image in place.
| Parameter | Type | Description |
|---|---|---|
x |
int |
Left boundary |
y |
int |
Top boundary |
width |
int |
Crop width |
height |
int |
Crop height |
CropAsync¶
Asynchronously crops the image.
CropMultiple¶
Batch-crops the image and returns multiple new ImageAsset instances.
| Parameter | Type | Description |
|---|---|---|
optionsArray |
CropOptions[] |
Crop rectangle array |
The returned array preserves the same order as the input.
var crops = new[]
{
new CropOptions(0, 0, 50, 50),
new CropOptions(50, 0, 50, 50),
};
ImageAsset[] results = image.CropMultiple(crops);
CropMultipleAsync¶
Asynchronously batch-crops the image.
Resize¶
Resizes the image in place.
| Parameter | Type | Default | Description |
|---|---|---|---|
width |
int |
— | Target width |
height |
int |
— | Target height |
filter |
ResizeFilter |
Nearest |
Resize filter algorithm |
ResizeAsync¶
Asynchronously resizes the image.
Conversion¶
ToTexture2D¶
public Texture2D ToTexture2D(
bool mipmap = false,
bool linear = true,
bool noLongerReadable = true
)
Converts the current image into a Unity Texture2D.
Only images with ChannelCount equal to 3 or 4 are supported; otherwise NotSupportedException is thrown.
| Parameter | Type | Default | Description |
|---|---|---|---|
mipmap |
bool |
false |
Whether to generate mipmaps |
linear |
bool |
true |
Whether to use linear color space |
noLongerReadable |
bool |
true |
Whether to mark the texture unreadable after upload |
ToTexture2DAsync¶
public Task<Texture2D> ToTexture2DAsync(
bool mipmap = false,
bool linear = true,
bool noLongerReadable = true
)
Asynchronously converts the image to Texture2D.