Create your reality

In this blog, you’ll see how to configure your Ready Player Me Avatar in Unity during runtime. If you don’t know about Ready Player Me or if you want to know how to create your avatar, then you should definitely watch this video before continuing with this blog.

Setting up Unity Project

Let’s begin by creating a new Unity Project and importing the Ready Player Me SDK.

  • Creating a new project with Unity version 2019.4.11f1 and later.

    Unity Project

  • Download the latest version of Ready Player Me SDK.

    Ready Player Me SDK

  • To import it into Unity, drag and drop it into the Project window → uncheck the Newtonsoft Json folder as this would already be there in your Unity project → click on Import.

    Import SDK

Alright now that you have the project set up with Ready Player Me SDK, let’s see how to use the Avatar Configure file.

Creating and Using Avatar Configure File

Avatar Config Files are scriptable objects that you can create and use to load an avatar in the configuration defined by you.

  • To create an avatar configuration file, right-click in the project window CreateScriptable ObjectReady Player MeAvatar Configuration.

    Avatar Configuration

  • Edit the configuration as per your need, for example, you can set the Mesh Lod to Medium, change the Pose to T Pose, include Texture Atlas and adjust the Texture Size Limit as well.

    Edit the Configuration File

  • To use this configuration file created by you, navigate to Ready Player MeSettings, this will open the Ready Player Me Settings window. Now, all you have to do is drag and drop the Avatar Configuration file into the Avatar Config parameter.

    Assign Avatar Config

To test this,

  • In the Project window, search for RuntimeExample scene or you can find the same by navigating to PluginsReady Player MeExamplesRuntime Example. Then, open the scene.

    Runtime Example

  • Next, copy your avatar link from the Ready Player Me hub.

    Ready Player Me Hub

  • Then, go back to your Unity project. Select the Runtime Example GameObject and paste the link into the Avatar Url parameter.

    Avatar Url

  • Now when you press the play button, the avatar will load with the same configuration as you had defined in the Avatar Configuration file

    Unity Project

With that, you have learnt to load an avatar using the avatar config scriptable object. But what if you want to load an avatar at runtime? That’s exactly what you’ll learn in the next section.

Loading Avatar at Runtime

There are two ways of loading Avatar at runtime. You can either create an Avatar Config file and use it to load an avatar or directly define it in the script. In this section, you’ll see how to load the avatar using the latter method.

Scripting

First, create a new C# script and name it as RuntimeAvatarLoader. Next, copy the following code into your script.

using ReadyPlayerMe;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RuntimeAvatarLoader : MonoBehaviour
{
    [SerializeField] private string avatarUrl;
    
    private AvatarConfig avatarConfig;
    private AvatarLoader avatarLoader;
    
		void Start()
    {
        avatarLoader = new AvatarLoader();
        avatarConfig = ScriptableObject.CreateInstance<AvatarConfig>();
        avatarConfig.MeshLod = MeshLod.Low;
        avatarConfig.Pose = ReadyPlayerMe.Pose.TPose;
        avatarConfig.TextureAtlas = TextureAtlas.Low;
        avatarLoader.AvatarConfig = avatarConfig;
    }

    public void LoadAvatar()
    {
        avatarLoader.LoadAvatar(avatarUrl);
    }
}

The script has a SerializeField where we need to provide the URL of the Avatar. On start, the script will create a new Avatar Config file and set it up as per the configuration written in the script. Only when the function LoadAvatar() is called, the script uses the URL to load the avatar into the scene.

Scene Setup

Open the SampleScene, create an empty GameObject and name it AvatarLoader → add the RuntimeAvatarLoader script to it.

Runtime avatar.png

Now go to the ReadyPlayerMe dashboard, copy the Avatar link and paste it into the Avatar URL field of the RuntimeAvatarLoader component.

Ready Player Me Avatar Link

Then, add a UI button by right clicking on the Hierarchy window and select UIButton TextMeshPro. Also, replace the button text with “Load Avatar”.

Adding UI Button

Finally, select the Button GameObject → navigate to the Button component → under the On Click event, click on the Plus sign ➕ → drag and drop the LoadAvatar GameObject. From the drop-down select RuntimeLoadAvatarLoadAvatar ().

LoadAvatar event call

Testing

To test this, all you need to do is press the Play button and then press the LoadAvatarUI button in the Game window.

Testing

With that, you were able to load the avatar at run time with a predetermined configuration.

Conclusion

Now that you know how to change the parameters of an avatar either by using the scriptable objects or via a script, you can create a dynamic experience for the users. Ready Player Me has also provided an example scene where the LOD of the avatar changes based on the distance of the camera from that avatar. This is really helpful when you have to optimize your game/experience. There are many more use cases for using the API during runtime, feel free to explore them.

Thank you

Thanks for reading this blog post. 🧡 If you are interested in creating your own AR and VR apps, you can learn more about it here on immersive insiders. Also, if you have any questions, don't hesitate to reach out! We're always happy to help.

You’ve successfully subscribed to immersive insiders
Welcome back! You’ve successfully signed in.
Great! You’ve successfully signed up.
Your link has expired
Success! Check your email for magic link to sign-in.