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​
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 |
NPC Count | How many npcs to draw in scene |
Spawn Radius | How far can NPCs spawn from the origin of the scene |
NPC Animator | The 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!
Code walkthrough​
Open NPCLoader.cs
csharp
// We make use of async await when interacting with the APIasync 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 operationsvar logged = await session.Login(Username, Password);//In case the login failsif (!logged)return;// We retrieve the last 4 avatars of the user accountvar 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 listint 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 avatarsGameObject 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");}}
csharp
// We make use of async await when interacting with the APIasync 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 operationsvar logged = await session.Login(Username, Password);//In case the login failsif (!logged)return;// We retrieve the last 4 avatars of the user accountvar 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 listint 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 avatarsGameObject 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");}}