Loading images to UI component using ImageSwift
Overview
In ImageSwift, loading images into Unity GameObjects and sprites is a straightforward process. This section provides guidance on using the library to dynamically load and display images within your Unity project.
ImageSwift.Load(string: url).Into(targetComponent).Start();
Learn more about these functions down.
Load()
The Load
function is the starting point for fetching an image in ImageSwift. It is a static method that returns an instance of the ImageSwift
class, allowing you to chain additional methods for customization.
Syntax:
ImageSwift.Load(string url);
Parameter
url
(string): The URL of the image to be loaded.
Example:
ImageSwift.Load("https://example.com/image.jpg");
This initiates the process of loading the image from the specified URL.
Into()
The Into function is used to specify the target where the loaded image should be applied. It supports various Unity components, allowing you to display images in different contexts. Into()
has to be chained with Load()
method to work.
Example:
ImageSwift.Load(string url).Into(GameObject gameObject).Start();
ImageSwift.Load(string url).Into(SpriteRenderer spriteRenderer).Start();
ImageSwift.Load(string url).Into(RawImage rawImage).Start();
ImageSwift.Load(string url).Into(Image uiImage).Start();
ImageSwift.Load(string url).Into(Sprite sprite).Start();
To perform some customized operation on the texture, you can also pass Action
callback as a parameter.
Example:
ImageSwift.Load(string url).Into((downloadedTexture)=> {
Debug.Log("Do something with downloaded Texture.")
}).Start();
Start()
Start()
method will initiates the excution of all the method you chained. All the method chained with be run sequentially.
Example:
ImageSwift.Load(string url).Into(Image image).Start();
ImageSwift provides callbacks for certain events learn about them more here.