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​
The Avatar Importer prefab allows to import and spawn a playable avatar by using our API.
Variable | Description |
---|---|
Username | Union Avatars account user |
Password | Union Avatars account password |
Organization | Your organization ID |
Player Animator | The animator controller of the avatar |
Attach Camera | If 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!
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.
csharp
// We make use of async await when interacting with the APIprivate 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 operationsbool logged = await session.Login(Username, Password);// In case the login failsif (!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 dataPaginated<AvatarMetadata> avatars = await session.GetAvatars(10, 1);//Now we import the first avatar of the listsession.LogHandler.Info("Importing avatar...");try{// Use the AvatarImporter helper to Instantiate and convert avatarsGameObject newAvatar = await AvatarImporter.ImportAvatarAsHumanoid(avatars.Items[0], // Get the first avatar itemPlayerAnimator,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");}}
csharp
// We make use of async await when interacting with the APIprivate 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 operationsbool logged = await session.Login(Username, Password);// In case the login failsif (!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 dataPaginated<AvatarMetadata> avatars = await session.GetAvatars(10, 1);//Now we import the first avatar of the listsession.LogHandler.Info("Importing avatar...");try{// Use the AvatarImporter helper to Instantiate and convert avatarsGameObject newAvatar = await AvatarImporter.ImportAvatarAsHumanoid(avatars.Items[0], // Get the first avatar itemPlayerAnimator,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");}}