Load an Avatar
The Genies Avatar SDK allows developers to load a user's global Avatar or a local 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.Avatar;
The GeniesSdkAvatar
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;
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
GeniesSdkAvatar.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
GeniesSdkAvatar.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.
Dispose Avatar Function
ManagedAvatar.Dispose();
This function disposes Avatar resources to cleanly remove it. Optionally, the caller should also destroy the GameObject hosting this Avatar if needed.
Example
Load an Avatar
This example will setup two buttons to load a user's Avatar or load a default Avatar.
using Genies.Sdk.Avatar;
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 GeniesSdkAvatar.LoadUserAvatarAsync(avatarName, parentTransform, animatorController);
Debug.Log("User Avatar Loaded: " + userAvatar.Root.name);
}
async void SpawnDefaultAvatar()
{
ManagedAvatar defaultAvatar = await GeniesSdkAvatar.LoadDefaultAvatarAsync(avatarName, parentTransform, animatorController);
Debug.Log("Default Avatar Loaded: " + defaultAvatar.Root.name);
}
}
This example assumes the user is logged in before either button is pressed.