# Unity 环境

> Use logical partitions to organize your service data for testing, staging, and production workflows.

环境是指 Unity Game Services（Unity 游戏服务）的逻辑分区，其中包含与项目关联的数据。相关示例包括使用 [Cloud Code](/cloud-code.md) 的游戏代码，或使用 [Remote Config](/remote-config.md) 的游戏配置。

* 环境是孤立的。这意味着，如果在一个环境中更改数据，其他环境中的数据不会受到影响。
* 在项目级别使用环境。
* 可以将环境视为数据的命名空间或标签。
* 创建环境并不会调配资源，而是支持将数据绑定到特定的工作空间。

## 支持的服务##supported-services

目前有以下服务支持环境：

* [分析](/analytics.md)
* [Cloud Code](/cloud-code.md)
* [Cloud Content Delivery](/ccd.md)
* [Cloud Save](/cloud-save.md)
* [Economy](/economy.md)
* [Lobby](/lobby.md)
* [Relay](/relay.md)
* [Remote Config](/remote-config.md)

Unity Gaming Services（Unity 游戏服务）将继续发布更多的服务来支持环境。

## 管理环境##managing-environments

要从 [Unity Dashboard](https://cloud.unity.com/) 访问项目的环境，请执行以下操作：

1. 从主导航菜单中选择 **Projects（项目）**。
2. 选择要将环境应用到的项目。
3. 选择 **Environments（环境）** 选项卡。

所有项目都是从生产环境开始。最多可以创建 25 个环境。要创建新环境，请单击 **Add Environment（添加环境）**，为新环境命名，然后选择 **Add（添加）**。

## 切换服务环境##switching-environments-for-a-service

要在 Unity Dashboard 中切换服务环境，请执行以下操作：

1. 从主导航菜单中选择 **Projects（项目）**。
2. 选择要切换环境的项目。
3. 选择 **Environments（环境）** 选项卡。
4. 选择要使用的环境。

## 访问 Unity 项目内的环境##accessing-environments-within-unity-projects

使用 Services Core 初始化选项，在您希望玩家体验的开发环境中初始化您的 Unity Gaming Services（Unity 游戏服务）。如果未指定，Unity Gaming Services（Unity 游戏服务）将在默认的“production（生产）”环境中初始化。

> **Note:**
>
> **注意**：每个支持环境的服务都依赖附带的 Services Core SDK。如需了解更多信息，请参阅有关 [Services Core API](./services-core-api.md) 的文档。

为此，请包含 `Unity.Services.Core` 和 `Unity.Services.Core.Environments` 命名空间，然后使用配置为传入环境名称的 `options` 参数调用 `UnityServices.InitializeAsync()` 方法。例如：

*使用“dev”环境初始化 Unity Gaming Services（Unity 游戏服务）。*

```plaintext
using Unity.Services.Authentication;
using Unity.Services.Core;
using Unity.Services.Core.Environments;
using UnityEngine;

class InitWithEnvironment : MonoBehaviour {
   async void Awake()
   {
       var options = new InitializationOptions();

       options.SetEnvironmentName("dev");
       await UnityServices.InitializeAsync(options);
       await AuthenticationService.Instance.SignInAnonymouslyAsync();
   }
}
```

如果未指定选项，则使用 Environment Selector（环境选择器）值。如果没有 Environment Selector（环境选择器）选项，则默认使用“production（生产）”。

如需了解更多信息，请参阅[环境选择器](./environments-api.md#environment-selector)。

> **Warning:**
>
> **重要**：必须包含 `Unity.Services.Core.Environments` 命名空间才能访问 `SetEnvironmentName` 方法。
