Skip to main content

NPCs

info

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

In this example we will import multiple avatars.

First, open the Avatar NPC Example scene. You can find it in:
NPC Avatars → Scenes

The NPC Loader​

Inspector of the NPC Loader 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
NPC CountHow many npcs to draw in scene
Spawn RadiusHow far can NPCs spawn from the origin of the scene
NPC AnimatorThe animator controller of 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 avatars move and fly around freely!

Untitled


Code walkthrough​

Open NPCLoader.cs

// We make use of async await when interacting with the API async 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: ct.Token, organization: Organization ); // First we need to login // The ServerSession object we created will take care of keeping a record of our token access for future operations var logged = await session.Login(Username, Password); //In case the login fails if (!logged) return; // We retrieve the last 4 avatars of the user account var avatars = await session.GetAvatars(4, 1); List<Task> npcSpawnTasks = new List<Task>(); for (int i = 0; i < npcCount; i++) { // Spawn randomly one of the avatars in the list int randomAvatarIndex = Random.Range(0, avatars.Items.Length); AvatarMetadata avatar = avatars.Items[randomAvatarIndex]; npcSpawnTasks.Add(SpawnNPC(avatar)); } session.LogHandler.Info("Fetching and spawning avatars..."); await Task.WhenAll(npcSpawnTasks); session.LogHandler.Info("Successfully loaded all the NPCs"); } private async Task SpawnNPC(AvatarMetadata avatar) { try { // Use the AvatarImporter helper to Instantiate and convert avatars GameObject npcGameObject = await AvatarImporter.ImportAvatarAsHumanoid( avatar, npcAnimator, ct.Token ); npcGameObject.transform.position = GetRandomPosition(); npcGameObject.transform.rotation = Quaternion.Euler(0, Random.Range(0.0f, 360.0f), 0); npcGameObject.AddComponent<NPCAvatar>(); } catch (System.Exception) { session.LogHandler.AvatarWarning("Avatar import failed"); } }
// We make use of async await when interacting with the API async 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: ct.Token, organization: Organization ); // First we need to login // The ServerSession object we created will take care of keeping a record of our token access for future operations var logged = await session.Login(Username, Password); //In case the login fails if (!logged) return; // We retrieve the last 4 avatars of the user account var avatars = await session.GetAvatars(4, 1); List<Task> npcSpawnTasks = new List<Task>(); for (int i = 0; i < npcCount; i++) { // Spawn randomly one of the avatars in the list int randomAvatarIndex = Random.Range(0, avatars.Items.Length); AvatarMetadata avatar = avatars.Items[randomAvatarIndex]; npcSpawnTasks.Add(SpawnNPC(avatar)); } session.LogHandler.Info("Fetching and spawning avatars..."); await Task.WhenAll(npcSpawnTasks); session.LogHandler.Info("Successfully loaded all the NPCs"); } private async Task SpawnNPC(AvatarMetadata avatar) { try { // Use the AvatarImporter helper to Instantiate and convert avatars GameObject npcGameObject = await AvatarImporter.ImportAvatarAsHumanoid( avatar, npcAnimator, ct.Token ); npcGameObject.transform.position = GetRandomPosition(); npcGameObject.transform.rotation = Quaternion.Euler(0, Random.Range(0.0f, 360.0f), 0); npcGameObject.AddComponent<NPCAvatar>(); } catch (System.Exception) { session.LogHandler.AvatarWarning("Avatar import failed"); } }