Skip to main content

User Interface

info

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

In this example we will use the SDK UI to create and load avatars.

First, open the Avatar UI Example scene. You can find it in:
User Interface Scenes

Requirements

Add a new layer "Avatar”

First add a new layer called Avatar. To do this go to the Inspector -> Layer -> Add Layer

0_add_new_layer

Then add the name “Avatar” to an empty field

00_add_new_layer

You don't need to care about the layer number, just make sure "Avatar" is written correctly


Using the UI

Inspector of the Avatar UI prefab

VariableDescription
UI PrefabOnly change this is you have a custom version of the interface
Open Interface KeyThe interface will open when this key is pressed
Load UI On StartIf true, the UI will appear when the game starts
Customize the UI

If you want to customize the UI we recommend you to clone our prefab. You can find it in:
User Interface/Prefabs/GUI/Modules
You should make a duplicate of the whole folder since the UI is split in mutliple sub-modules

Now you can create and load your custom avatars inside your game!

Avatar UI

Code walkthrough

Open AvatarLoaderUI.cs

private void Start() { // Initialize a Union Avatars session // We will use this object as our main interface to perform operations // Ex: Login, Downloading Avatars,... session = new ServerSession( logToUnity: true, ct: cancellationToken.Token, organization: Organization // Pass the organization ID here ); if(LoadUIOnStart) LoadUI(); } private void LoadUI() { if (isUILoaded) return; isUILoaded = true; // Create an instance of the UI prefab. The instance contains a AvatarUIManager // We can use this manager to setup and interact with the interface easily AvatarUIManager unionUI = Instantiate(uiPrefab).GetComponent<AvatarUIManager>(); //First we initialize/setup the UI with our session unionUI.SetupUI(session); // The onAvatarSelected event will trigger once an avatar is selected or created // It will return its Avatar Metadata unionUI.onAvatarSelected += BuildAvatar; unionUI.onClose += () => isUILoaded = false; } private async void BuildAvatar(AvatarMetadata avatar) { if (avatar == null) throw new System.ArgumentNullException("avatar"); session.LogHandler.Info($"Importing avatar: {avatar.Name} ..."); try { // Use the AvatarImporter helper to Instantiate and convert avatars GameObject newAvatar = await AvatarImporter.ImportAvatarAsHumanoid(avatar, null, cancellationToken.Token); session.LogHandler.Info("Avatar Loaded!"); SetupAvatarObject(newAvatar); } catch (System.Exception) { session.LogHandler.AvatarWarning("Avatar import failed"); } }
private void Start() { // Initialize a Union Avatars session // We will use this object as our main interface to perform operations // Ex: Login, Downloading Avatars,... session = new ServerSession( logToUnity: true, ct: cancellationToken.Token, organization: Organization // Pass the organization ID here ); if(LoadUIOnStart) LoadUI(); } private void LoadUI() { if (isUILoaded) return; isUILoaded = true; // Create an instance of the UI prefab. The instance contains a AvatarUIManager // We can use this manager to setup and interact with the interface easily AvatarUIManager unionUI = Instantiate(uiPrefab).GetComponent<AvatarUIManager>(); //First we initialize/setup the UI with our session unionUI.SetupUI(session); // The onAvatarSelected event will trigger once an avatar is selected or created // It will return its Avatar Metadata unionUI.onAvatarSelected += BuildAvatar; unionUI.onClose += () => isUILoaded = false; } private async void BuildAvatar(AvatarMetadata avatar) { if (avatar == null) throw new System.ArgumentNullException("avatar"); session.LogHandler.Info($"Importing avatar: {avatar.Name} ..."); try { // Use the AvatarImporter helper to Instantiate and convert avatars GameObject newAvatar = await AvatarImporter.ImportAvatarAsHumanoid(avatar, null, cancellationToken.Token); session.LogHandler.Info("Avatar Loaded!"); SetupAvatarObject(newAvatar); } catch (System.Exception) { session.LogHandler.AvatarWarning("Avatar import failed"); } }