Skip to main content

Load an Avatar

The Genies Avatar SDK allows developers to load a user's global Avatar or a local Avatar.

Load Avatar

Avatar Definition

An Avatar's hair color, waist size, and clothed wearables are all stored in the Avatar's definition. It's essentially a stored list of what the Avatar looks like that can be saved and loaded from.

Global vs Local

The difference between global and local Avatars is how they are stored in memory. A global Avatar is saved by Genies and can be loaded from any project using the Genies SDK. The local Avatar is saved by the developers and only used in the developer's projects.

Loading a Global Avatar

Loading a global Avatar is useful for interoperability across different projects that use Genies SDK. Having a connected experience across different environments can be desirable for players.

Loading a Local Avatar

Loading a local Avatar is useful when developers want a consistent visual style across their game or to let players select from preset templates. This allows for unique, project-specific experiences while keeping the user’s global Avatar unchanged.

Load Avatar API

Genies Sdk Avatar Library

using Genies.Sdk;

The Genies.Sdk library contains methods to load a user's global Avatar or a default local Avatar.

Managed Avatar Object

ManagedAvatar avatar;

//Animator bound to the Avatar rig.
Animator animator = avatar.Animator;

//Root GameObject holding the Avatar hierarchy.
GameObject root = avatar.Root;

//The MonoBehaviour component attached to the Avatar's root GameObject.
//Provides a bridge between Unity's GameObject system and this ManagedAvatar wrapper.
ManagedAvatarComponent component = avatar.Component;

//Sub-root GameObject where the visual model is parented.
GameObject modelRoot = avatar.ModelRoot;

//Skeleton root Transform.
Transform skeletonRoot = avatar.SkeletonRoot;

//Function to dispose of the Avatar and cleanly remove it
avatar.Dispose();

The ManagedAvatar object is returned when an Avatar is loaded. It contains references to the Avatar's different components such as the root GameObject and Animator.

Load User Avatar Async Function

Genies.Sdk.AvatarSdk.LoadUserAvatarAsync([string], [Transform], [RuntimeAnimatorController]);

This function will load a user's global Avatar object. It has three optional parameters for the root object's name, the root's parent transform, and the animator controller.

Load Default Avatar Async Function

Genies.Sdk.AvatarSdk.LoadDefaultAvatarAsync([string], [Transform], [RuntimeAnimatorController])

This function will load a local default Avatar. It has three optional parameters for the root object's name, the root's parent transform, and the animator controller.

Load From Local Avatar Definition Async Function

Genies.Sdk.AvatarSdk.LoadFromLocalAvatarDefinitionAsync(string);

This function will load a local Avatar using an Avatar definition in a JSON file given a profile ID string. The function returns a ManagedAvatar reference or null if it fails.

Load From Local Game Object Async Function

Genies.Sdk.AvatarSdk.LoadFromLocalGameObjectAsync(string);

This function will load a local Avatar using an Avatar definition in a GameObject given a profile ID string. The function returns a ManagedAvatar reference or null if it fails.

Save Avatar Definition Locally Async

Genies.Sdk.AvatarSdk.SaveAvatarDefinitionLocallyAsync(ManagedAvatar, string);

This function saves a given Avatar's definition to both a local JSON file for persistent data and a scriptable GameObject in Resources. It uses the given string as a profile ID.

note

The Avatar Editor also has functionality to save an Avatar's definition similar to this function.

Example

Load an Avatar

This example will setup two buttons to load a user's Avatar or load a default Avatar.

using Genies.Sdk;
using UnityEngine;
using UnityEngine.UI;

public class LoadAvatarTest : MonoBehaviour
{
[SerializeField] private string avatarName = "MyAvatar";
[SerializeField] private Transform parentTransform = null;
[SerializeField] private RuntimeAnimatorController animatorController = null;
[SerializeField] private Button spawnUserAvatarButton;
[SerializeField] private Button spawnDefaultAvatarButton;

private void Start()
{
spawnUserAvatarButton.onClick.AddListener(SpawnUserAvatar);
spawnDefaultAvatarButton.onClick.AddListener(SpawnDefaultAvatar);
}

private void OnDestroy()
{
spawnUserAvatarButton.onClick.RemoveListener(SpawnUserAvatar);
spawnDefaultAvatarButton.onClick.RemoveListener(SpawnDefaultAvatar);
}

async void SpawnUserAvatar()
{
ManagedAvatar userAvatar = await Genies.Sdk.AvatarSdk.LoadUserAvatarAsync(avatarName, parentTransform, animatorController);
Debug.Log("User Avatar Loaded: " + userAvatar.Root.name);
}

async void SpawnDefaultAvatar()
{
ManagedAvatar defaultAvatar = await Genies.Sdk.AvatarSdk.LoadDefaultAvatarAsync(avatarName, parentTransform, animatorController);
Debug.Log("Default Avatar Loaded: " + defaultAvatar.Root.name);
}
}
note

This example assumes the user is logged in before either button is pressed.