The example provided at the path above demonstrates two different methods for obtaining the current persona in your Unity project using the EmergenceSDK. We will go through the example code and explain the purpose of each section.
For this code to execute properly you must first have logged in using the Emergence overlay.
First, we need to import the necessary namespaces:
Next, we define a new class called GettingCurrentPersona, which inherits from MonoBehaviour. This allows us to attach this script to a GameObject in Unity.
We declare a private variable personaService of type IPersonaService, which will be used to access the persona-related services provided by EmergenceSDK.
In the Awake method, we initialize the personaService variable by getting the IPersonaService instance from the EmergenceServices class.
The Start method is called at the beginning of the script's execution. Here, we call the GetCurrentPersonaAsync method to obtain the current persona.
privatevoidStart(){GetCurrentPersonaAsync();}
The GetCurrentPersonaAsync method is an asynchronous method that waits for the personaService to return the current persona. Once the persona is received, it calls the GetPersonaSuccess method. If an error occurs, it will call the ErrorLogger.LogError method to log the error.
The GetCachedPersona method gets the cached persona from the personaService instance. If a cached persona exists, which it will after using the async method, it logs the cached persona to the console.
You should see your persona logged to the console twice.
Full Code:
usingEmergenceSDK.Runtime.Internal.Services;usingEmergenceSDK.Runtime.Internal.Types;usingUnityEngine;namespaceEmergenceSDK.Samples.CoreSamples{publicclassGettingCurrentPersona:MonoBehaviour {privateIPersonaService personaService;privatevoidAwake() { // Initialize the personaService variable by getting the IPersonaService instance from the EmergenceServices class
personaService =EmergenceServiceProvider.GetService<IPersonaService>(); }privatevoidStart() {GetCurrentPersonaAsync(); }privateasyncvoidGetCurrentPersonaAsync() { // Waits for the personaService to return the current persona and then calls the GetPersonaSuccess methodawaitpersonaService.GetCurrentPersona(GetPersonaSuccess,ErrorLogger.LogError); } // This method is called when the personaService successfully returns the current personaprivatevoidGetPersonaSuccess(Persona currentpersona) { // Logs the current persona to the consoleDebug.Log($"Found persona: {currentpersona}"); // Calls the GetCachedPersona method to get the cached personaGetCachedPersona(); } // This method gets the cached persona from the personaService instanceprivatevoidGetCachedPersona() { // Checks if there is a cached persona and logs it to the console if there isif (personaService.GetCachedPersona(outPersona persona)) {Debug.Log($"Found cached persona: {persona}"); } } }}