Documentation

Environments API

Find how to use the Environments API and Environment Selector in the Unity Editor and at build and runtime to set the active Unity Gaming Services environment.
Read time 2 minutesLast updated 6 days ago

Environments are logical partitions for Unity Gaming Services that contain data associated with your project.
The Core package's
EnvironmentsApi
singleton provides access to the environment state.
namespace Unity.Services.Core.Environments.Editor{ public class EnvironmentsApi { public static IEnvironmentsApi Instance { get; } }}

Select an environment using the Environment Selector

The Environment Selector sets the current environment for all supported services.
Project Settings Services page showing the Environment Selector with the active environment name.

The global Environment Selector in the Unity Editor Project Settings.

To select an environment in the Unity Editor:
  1. Select Edit > Project Settings > Services > Environments.
  2. In the Editor Environment field, select the environment to use.
You can manage environments, such as adding or deleting environments, in the Unity Dashboard.

Access the active environment

You can access the active environment as the name or GUID. The
PropertyChanged
event informs you when the environment changes.
namespace Unity.Services.Core.Environments.Editor{ public class EnvironmentsApi { public string ActiveEnvironmentName { get; set; } public System.Nullable<System.Guid> ActiveEnvironmentId { get; } public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged; }}

Manage environments using the API

The
EnvironmentsApi
allows you to refresh the environments list, get all environments, set the active environment, and validate that the active environment still exists remotely.
namespace Unity.Services.Core.Editor.Environments{ public class EnvironmentsApi { public System.Collections.Generic.IReadOnlyCollection<EnvironmentInfo> Environments { get; } public System.Threading.Tasks.Task RefreshAsync(); public void SetActiveEnvironment(EnvironmentInfo environment); public System.Threading.Tasks.Task<ValidationResult> ValidateEnvironmentAsync(); }}

Understand build and runtime behavior

At build and runtime, the chosen environment uses a priority fallback mechanism to choose the correct environment. The order of precedence is
InitializationOptions
, then the Environment Selector value, then the default environment (
"production"
).
For an example that uses
InitializationOptions
, refer to the Services Core SDK Initialization example.

Add the EnvironmentView component

The
EnvironmentView
component displays the currently active environment and allows quick access to the global Environment Selector.
Environment View control showing the active environment name.

The EnvironmentView control on an Editor toolbar.

The following example shows how to add this component into a toolbar with UXML:
<ui:UXML xmlns:uie="UnityEditor.UIElements" xmlns:coreUi="Unity.Services.Core.Editor.Environments.UI" editor-extension-mode="True"> <uie:Toolbar> <coreUi:EnvironmentView /> </uie:Toolbar></ui:UXML>