ReadyNFT
Search
K
Comment on page

Download Fetched Sprites

In this tutorial, we will download the sprites fetched from the Asset Hub
The ReadyNFTFileHelpers class provides a helper method to facilitate the downloading of sprite images associated with NFTs from the ReadyNFT API. You can create an instance of the ReadyNFTFileHelpers class and utilize the DownloadSpriteImagesAsync method to download the sprite images asynchronously. Additionally, the method supports a progress reporting mechanism through the use of the ReadyNFTDownloadReport class.

Method Signature

public async Task DownloadSpriteImagesAsync(List<ReadyNFTSpriteObject> sprites, IProgress<ReadyNFTDownloadReport> progress = null)

Parameters

  • sprites (List<ReadyNFTSpriteObject>): A list of ReadyNFTSpriteObject instances representing the sprite objects whose images you want to download.
  • progress (IProgress<ReadyNFTDownloadReport>, optional): An instance of IProgress<ReadyNFTDownloadReport> that allows progress reporting during the image download process. This parameter is optional.

Usage Example

using System.Collections.Generic;
using UnityEngine;
public class ReadyNFTManager : MonoBehaviour
{
private const string apiKey = "your-api-key";
private const string gameId = "your-game-id";
void Start()
{
InitializeReadyNFT();
}
public async void InitializeReadyNFT()
{
ReadyNFT readyNFT = new ReadyNFT();
ReadyNFTFileHelpers RNFTHelpers = new ReadyNFTFileHelpers();
readyNFT.Init(apiKey, gameId);
Debug.Log("ReadyNFT initialized!");
Debug.Log("Fetching sprites...");
List<ReadyNFTSpriteObject> sprites = await readyNFT.FetchSpritesAsync();
// save the sprites onto the device
Debug.Log("downloading the sprites...");
await RNFTHelpers.DownloadSpriteImagesAsync(sprites);
}
}

Download Progress Reporting

The ReadyNFTDownloadReport class is used for progress reporting during the image download process. It provides information about the progress, including the sprite ID and the percentage of completion.

Usage Example

using System;
using System.Collections.Generic;
using UnityEngine;
public class ReadyNFTManager : MonoBehaviour
{
private const string apiKey = "your-api-key";
private const string gameId = "your-game-id";
void Start()
{
InitializeReadyNFT();
}
public async void InitializeReadyNFT()
{
ReadyNFT readyNFT = new ReadyNFT();
ReadyNFTFileHelpers RNFTHelpers = new ReadyNFTFileHelpers();
readyNFT.Init(apiKey, gameId);
Debug.Log("ReadyNFT initialized!");
Debug.Log("Fetching sprites...");
List<ReadyNFTSpriteObject> sprites = await readyNFT.FetchSpritesAsync();
// save the sprites onto the device
// create the reporter object
Progress<ReadyNFTDownloadReport> reporter = new Progress<ReadyNFTDownloadReport>(report =>
{
Debug.Log("Files downloaded: " + report.current + "/" + report.total + " (" + report.percent + "%)");
});
Debug.Log("downloading the sprites...");
await RNFTHelpers.DownloadSpriteImagesAsync(sprites, reporter);
}
}
When the DownloadSpriteImagesAsync method is called with a valid progress reporter, the progress reporter will receive periodic updates through the ReportDownloadProgress method, allowing you to track the progress of the image downloads. You can use the progress information to update UI elements, display progress bars, or provide feedback to the user regarding the download status.
By utilizing the ReadyNFTFileHelpers class, creating an instance, and calling the DownloadSpriteImagesAsync method, you can conveniently download sprite images associated with NFTs from the ReadyNFT API. Additionally, by incorporating the progress reporting mechanism using the ReadyNFTDownloadReport class, you can keep track of the download progress and provide visual feedback to enhance the user experience.