Skip to main content

API Essentials

info

You can import this example in the Package Manager window
Managing Samples

In this example we will import an avatar using the sdk API and make it playable.

First, open the Avatar API Example scene. You can find it in:
Essential Samples → Scenes

The Avatar Importer​

Inspector of the Avatar Importer prefab

The Avatar Importer prefab allows to import and spawn a playable avatar by using our API.

VariableDescription
UsernameUnion Avatars account user
PasswordUnion Avatars account password
OrganizationYour organization ID
Player AnimatorThe animator controller of the avatar
Attach CameraIf true, the scene camera will follow the avatar
Don't have an organization id?

Please request access here: Union Avatars Subscription

If you fill the variables and press Play, you should be able to see your last created avatar and move around freely!

Untitled


Code walkthrough​

Open AvatarLoaderAPI.cs. If you scroll down you will find a method called BuildAvatarFromAPI. This method contains all the logic used to get our avatar.

// We make use of async await when interacting with the API private async void BuildAvatarFromAPI() { // Initialize a Union Avatars session // We will use this object as our main interface to perform operations // Ex: Login, Downloading/Creating Avatars... ServerSession session = new ServerSession( logToUnity: true, ct: cancellationTokenSource.Token, organization: Organization // Pass the organization ID here ); // First we need to login // The ServerSession object we created will take care of // keeping a record of our token access for future operations bool logged = await session.Login(Username, Password); // In case the login fails if (!logged) return; // We retrieve the last 10 avatars of the user // The Avatars are returned inside a container class called Paginated // Avatars are stored in an array called Items inside the paginated class // AvatarMetadata is a class containing all of the avatar's data Paginated<AvatarMetadata> avatars = await session.GetAvatars(10, 1); //Now we import the first avatar of the list session.LogHandler.Info("Importing avatar..."); try { // Use the AvatarImporter helper to Instantiate and convert avatars GameObject newAvatar = await AvatarImporter.ImportAvatarAsHumanoid( avatars.Items[0], // Get the first avatar item PlayerAnimator, cancellationTokenSource.Token ); session.LogHandler.Info("Avatar Loaded!"); newAvatar.AddComponent<PlayerMovement>(); if (AttachCamera) cameraFollowComponent.SetupTarget(newAvatar.transform); } catch (System.Exception) { session.LogHandler.AvatarWarning("Avatar import failed"); } }
// We make use of async await when interacting with the API private async void BuildAvatarFromAPI() { // Initialize a Union Avatars session // We will use this object as our main interface to perform operations // Ex: Login, Downloading/Creating Avatars... ServerSession session = new ServerSession( logToUnity: true, ct: cancellationTokenSource.Token, organization: Organization // Pass the organization ID here ); // First we need to login // The ServerSession object we created will take care of // keeping a record of our token access for future operations bool logged = await session.Login(Username, Password); // In case the login fails if (!logged) return; // We retrieve the last 10 avatars of the user // The Avatars are returned inside a container class called Paginated // Avatars are stored in an array called Items inside the paginated class // AvatarMetadata is a class containing all of the avatar's data Paginated<AvatarMetadata> avatars = await session.GetAvatars(10, 1); //Now we import the first avatar of the list session.LogHandler.Info("Importing avatar..."); try { // Use the AvatarImporter helper to Instantiate and convert avatars GameObject newAvatar = await AvatarImporter.ImportAvatarAsHumanoid( avatars.Items[0], // Get the first avatar item PlayerAnimator, cancellationTokenSource.Token ); session.LogHandler.Info("Avatar Loaded!"); newAvatar.AddComponent<PlayerMovement>(); if (AttachCamera) cameraFollowComponent.SetupTarget(newAvatar.transform); } catch (System.Exception) { session.LogHandler.AvatarWarning("Avatar import failed"); } }