Unity Editor Service Authorizer
The UnityEditorServiceAuthorizer class is an implementation of the IServiceAuthorizer interface, and you can use it only in the context of Unity Editor scripting.
The class inherits from the UnityEditor.ScriptableSingleton<T> class to ensure seamless integration into the Unity Editor lifecycle. This integration provides support for domain reloads and serialization of internal property states, such as AuthenticationState and any acquired Unity Services token.
Use the UnityEditorServiceAuthorizer class for these purposes:
- Trigger the
IAuthenticationStateProvider.AuthenticationStateChangedevent using the active user session in the Unity Editor. - Fetch the Unity organizations and Unity projects that are associated with the current user, along with their assigned roles and permissions in these entities. To do this, use the methods that this class exposes from the
IOrganizationRepositoryandIUserInfoProviderinterfaces.
Learn to use the UnityEditorServiceAuthorizer.instance static property as an IOrganizationRepository interface and retrieve organizations, projects, members, and Role-based access control (RBAC) information available for the current logged-in user.
You can inject UnityEditorServiceAuthorizer.instance as an IServiceAuthorizer in a Common.ServiceHttpClient class instance to provide the bearer authorization header in HTTP requests to Unity Cloud service endpoints.
UnityEditorServiceAuthorizer usage
The example below shows how you can use the UnityEditorServiceAuthorizer class in a UnityEditor.EditorWindow derived class to fetch the name of the logged-in user:
internal class MyUnityEditorServiceAuthorizerExample : EditorWindow
{
AuthenticationState m_AuthenticationState;
IUserInfoProvider m_UserInfoProvider => UnityEditorServiceAuthorizer.instance;
IUserInfo m_UserInfo;
// Uncomment next line to Add menu item to the Window menu
// [MenuItem("Window/Unity Editor Service Authorizer Example")]
static void Init()
{
MyUnityEditorServiceAuthorizerExample window = (MyUnityEditorServiceAuthorizerExample)GetWindow(typeof(MyUnityEditorServiceAuthorizerExample));
window.titleContent.text = "Unity Editor Authenticator Example";
window.Show();
}
void OnEnable()
{
UnityEditorServiceAuthorizer.instance.AuthenticationStateChanged += OnAuthenticationStateChanged;
OnAuthenticationStateChanged(UnityEditorServiceAuthorizer.instance.AuthenticationState);
}
async void OnAuthenticationStateChanged(AuthenticationState authenticationState)
{
switch (authenticationState)
{
case AuthenticationState.LoggedIn:
await GetUserInfoAsync();
break;
case AuthenticationState.AwaitingInitialization:
case AuthenticationState.AwaitingLogin:
case AuthenticationState.AwaitingLogout:
case AuthenticationState.LoggedOut:
break;
}
Repaint();
}
async Task GetUserInfoAsync()
{
m_UserInfo = await m_UserInfoProvider.GetUserInfoAsync();
}
void OnGUI()
{
if (UnityEditorServiceAuthorizer.instance.AuthenticationState.Equals(AuthenticationState.LoggedIn))
{
GUILayout.Label(m_UserInfo != null ? $"You are Logged in as {m_UserInfo.Name}." : "You are Logged in.");
}
else
{
GUILayout.Label(
UnityEditorServiceAuthorizer.instance.AuthenticationState.Equals(AuthenticationState.AwaitingInitialization)
? $"Please wait, initializing..."
: $"You are Logged out.");
}
}
}