Caching Assets
Load times for avatars and wearables is a really important aspect for the user experience in games. The Genies Avatar SDK has methods to alleviate the load times for assets by pre-caching them.
Order of Operations
Pre-caching assets requires that the user is logged in which can be done anonymously.
Here is the recommended instructions for pre-caching assets:
- User logs in with Genies account or anonymously.
- Assets are pre-cached for given Avatar definition, user Avatar, or default Avatar.
- The Avatar and wearables are then loaded much quicker than without pre-caching.
It may also be good to clear the cache if the user's inventory may have changed such as a new wearable was gifted.
Caching Folder
You can find the cache folder using your computer's persistent path:
- Windows:
C:\Users\[pc-name]\AppData\LocalLow\Genies\[app-name]\DefaultInventoryCache - Mac:
Library -> Application Support -> Genies -> [app-name] -> DefaultInventoryCache
Caching Assets API
Force Fetch Boolean
Most the API methods that would fetch the cached assets have an optional boolean argument named forceFetch. If true, this will force the method to fetch the assets manually instead of using the cached assets.
An example is the GetDefaultWearablesByCategoryAsync method.
Clear Default Wearables Cache Function
AvatarSdk.ClearDefaultWearablesCache();
Clears the disk cache for default wearables only (does not affect user wearables). Also clears the in-memory cache so subsequent calls to GetDefaultWearablesByCategoryAsync re-fetch from the server.
Clear User Wearables Cache Function
AvatarSdk.ClearUserWearablesCache();
Clears the disk cache for user wearables only. Also clears the in-memory cache so subsequent calls to GetUserWearablesByCategoryAsync re-fetch from the server.
Precache Avatar Assets By Definition Async Function
AvatarSdk.PrecacheAvatarAssetsByDefinitionAsync(string);
Pre-caches assets required for an avatar based on a JSON definition without loading it. This downloads and caches all assets needed for the avatar, improving subsequent load times. User must be logged in. Returns a bool if the precache was successful.
Precache Default Avatar Assets Async Function
AvatarSdk.PrecacheDefaultAvatarAssetsAsync([System.Threading.CancellationToken cancellationToken]);
Pre-caches assets required for a default avatar without loading it. This downloads and caches all assets needed for the avatar, improving subsequent load times. Returns a bool if the precache was successful.
Precache User Avatar Assets Async Function
AvatarSdk.PrecacheUserAvatarAssetsAsync([System.Threading.CancellationToken cancellationToken]);
Pre-caches assets required for the authenticated user's avatar without loading it. This downloads and caches all assets needed for the avatar, improving subsequent load times. User must be logged in. Returns a bool if the precache was successful.
Examples
Precache Assets
This example will log in the user anonymously or with instant login. Then it will pre-cache the assets based on the login method.
using UnityEngine;
using Genies.Sdk;
public class PrecacheTest : MonoBehaviour
{
[SerializeField] private bool usingAnonLogin = false;
//A JSON avatar definition that is optional for anonymous login users
[SerializeField] private string avatarDefinition;
async void Start()
{
AvatarSdk.ClearDefaultWearablesCache();
AvatarSdk.ClearUserWearablesCache();
if (usingAnonLogin)
{
var (succeeded, reason) = await AvatarSdk.StartLoginAnonymousAsync();
if(succeeded)
{
Debug.Log("Anonymous login successful");
if(avatarDefinition != null)
{
var precacheSuccess = await AvatarSdk.PrecacheAvatarAssetsByDefinitionAsync(avatarDefinition);
if(precacheSuccess)
{
Debug.Log("Avatar definition assets precache successful");
ManagedAvatar avatar = await AvatarSdk.LoadAvatarByDefinitionAsync(avatarDefinition);
await AvatarSdk.OpenAvatarEditorAsync(avatar);
}
else
{
Debug.Log("Avatar definition assets precache failed");
}
}
else
{
var precacheSuccess = await AvatarSdk.PrecacheDefaultAvatarAssetsAsync();
if(precacheSuccess)
{
Debug.Log("Default avatar assets precache successful");
ManagedAvatar avatar = await AvatarSdk.LoadDefaultAvatarAsync();
await AvatarSdk.OpenAvatarEditorAsync(avatar);
}
else
{
Debug.Log("Default avatar assets precache failed");
}
}
}
else
{
Debug.Log("Anonymous login failed: " + reason);
}
}
else
{
var (succeeded, username) = await AvatarSdk.TryInstantLoginAsync();
if(succeeded)
{
Debug.Log("Instant login successful: " + username);
var precacheSuccess = await AvatarSdk.PrecacheUserAvatarAssetsAsync();
if(precacheSuccess)
{
Debug.Log("User avatar assets precache successful");
ManagedAvatar avatar = await AvatarSdk.LoadUserAvatarAsync();
await AvatarSdk.OpenAvatarEditorAsync(avatar);
}
else
{
Debug.Log("User avatar assets precache failed");
}
}
else
{
Debug.Log("Instant login failed");
}
}
}
}