

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 将 Amazon GameLift Servers 集成到 Unity 项目中
<a name="integration-engines-unity-using"></a>

本主题可帮助您在 Unity 游戏服务器项目中设置 Amazon GameLift Servers C\$1 服务器开发工具包。如果您不确定托管 Amazon GameLift Servers 服务是否支持您所使用的操作系统，请参阅[对于游戏服务器](gamelift-supported.md#gamelift-supported-servers)。

设置完适用于 Unity 的Amazon GameLift Servers插件后，您可以试用 [Amazon GameLift ServersUnity 示例](https://github.com/aws-samples/amazon-gamelift-unity) GitHub。

## 设置适用于 Unity 的 C\$1 Server 开发工具包
<a name="integration-engines-unity-setup"></a>

按照以下步骤以构建适用于 C\$1 的 Amazon GameLift Servers 服务器 SDK 并将其添加到您的 Unity 游戏服务器项目中。

**为 Unity 设置适用于 Amazon GameLift Servers 的服务器 SDK**

1. **下载 [Amazon GameLift Servers 服务器 SDK](https://www.amazonaws.cn/gamelift/getting-started)。**要验证是否支持您的游戏系统要求，请参阅 [获取 Amazon GameLift Servers 开发工具](gamelift-supported.md)。服务器软件开发工具包 压缩文件包含 C\$1 服务器软件开发工具包 和源文件，因此您可以根据需要为项目构建软件开发工具包。

1. **构建 C\$1 软件开发工具包库。**在 IDE 中，加载要使用的解决方案文件。使用该 IDEs功能恢复项目的 NuGet 文件。有关最低要求和附加构建选项，请参阅 C\$1 服务器软件开发工具包的 `README.md` 文件。生成解决方案以生成 C\$1软件开发工具包 库。

1. **检查配置设置。**在 Unity 编辑器中，打开您的游戏项目。在 Unity Editor 中，请转到 **File**、**Build Settings**、**Player Settings**。在 **Other Settings (其他设置)**、**Configuration (配置)** 下，检查以下设置：
   + 脚本运行时版本：设置为要使用的 .NET 解决方案。

1. **将 Amazon GameLift Servers 库添加到您的 Unity 项目中。**在 Unity Editor 中，将构建所生成的库导入项目的 `Assets/Plugins` 目录中。有关您正在使用的开发工具包版本的完整库列表，请参阅 `README.md` 文件。

## 添加 Amazon GameLift Servers 服务器代码
<a name="integration-engines-unity-code"></a>

有关添加 Amazon GameLift Servers 功能的更多信息，请参阅以下主题：
+ [借助服务器 SDK 将 Amazon GameLift Servers 添加到游戏服务器](gamelift-sdk-server-api.md)
+ 

以下代码示例使用 `MonoBehavior`，演示使用 Amazon GameLift Servers 执行的简单游戏服务器初始化。

```
using UnityEngine;
using Aws.GameLift.Server;
using System.Collections.Generic;

public class GameLiftServerExampleBehavior : MonoBehaviour
{
    //This is an example of a simple integration with GameLift server SDK that makes game server 
    //processes go active on Amazon GameLift
    public void Start()
    {
        //Set the port that your game service is listening on for incoming player connections (hard-coded here for simplicity)
        var listeningPort = 7777;

        //InitSDK establishes a local connection with the Amazon GameLift agent to enable 
        //further communication.
        var initSDKOutcome = GameLiftServerAPI.InitSDK();
        if (initSDKOutcome.Success)
        {
            ProcessParameters processParameters = new ProcessParameters(
                (gameSession) => {
                    //Respond to new game session activation request. GameLift sends activation request 
                    //to the game server along with a game session object containing game properties 
                    //and other settings. Once the game server is ready to receive player connections, 
                    //invoke GameLiftServerAPI.ActivateGameSession()
                    GameLiftServerAPI.ActivateGameSession();
                },
                () => {
                    //OnProcessTerminate callback. GameLift invokes this callback before shutting down 
                    //an instance hosting this game server. It gives this game server a chance to save
                    //its state, communicate with services, etc., before being shut down. 
                    //In this case, we simply tell GameLift we are indeed going to shut down.
                    GameLiftServerAPI.ProcessEnding();
                }, 
                () => {
                    //This is the HealthCheck callback.
                    //GameLift invokes this callback every 60 seconds or so.
                    //Here, a game server might want to check the health of dependencies and such.
                    //Simply return true if healthy, false otherwise.
                    //The game server has 60 seconds to respond with its health status. 
                    //GameLift will default to 'false' if the game server doesn't respond in time.
                    //In this case, we're always healthy!
                    return true;
                },
                //Here, the game server tells GameLift what port it is listening on for incoming player 
                //connections. In this example, the port is hardcoded for simplicity. Active game
                //that are on the same instance must have unique ports.
                listeningPort, 
                new LogParameters(new List<string>()
                {
                    //Here, the game server tells GameLift what set of files to upload when the game session ends.
                    //GameLift uploads everything specified here for the developers to fetch later.
                    "/local/game/logs/myserver.log"
                }));

            //Calling ProcessReady tells GameLift this game server is ready to receive incoming game sessions!
            var processReadyOutcome = GameLiftServerAPI.ProcessReady(processParameters);
            if (processReadyOutcome.Success)
            {
                print("ProcessReady success.");
            }
            else
            {
                print("ProcessReady failure : " + processReadyOutcome.Error.ToString());
            }
        }
        else
        {
            print("InitSDK failure : " + initSDKOutcome.Error.ToString());
        }
    }

    void OnApplicationQuit()
    {
        //Make sure to call GameLiftServerAPI.ProcessEnding() when the application quits. 
        //This resets the local connection with GameLift's agent.
        GameLiftServerAPI.ProcessEnding();
    }
}
```