【问题标题】:How to integrate GameLift with Unity3d as a game client如何将 GameLift 与 Unity3d 集成为游戏客户端
【发布时间】:2018-12-02 09:01:28
【问题描述】:

我正在尝试使用 Unity3d 游戏作为 GameList 客户端。

根据GameLift forum,亚马逊似乎不建议直接使用游戏客户端作为GameLift客户端。

但我想试一试,因为我不想要一个单独的游戏服务。

  1. 第一步是从GitHub下载AWS SDK源代码并构建.net35版本的dll;

  2. 将 AWSSDK.Core.dll 和 AWSSDK.GameLift.dll 放入 /Assets/Plugins;

  3. 从 MonoBehaviour 创建一个新的派生类来创建 AmazonGameLiftClient,下面是我的代码:

public class MyGameLiftClient : MonoBehaviour
{
    private void Awake()
    {    
        AmazonGameLiftConfig gameLiftConfig = 
                new AmazonGameLiftConfig {RegionEndpoint = RegionEndpoint.USWest1};
        AmazonGameLiftClient client = new AmazonGameLiftClient(
                "AwsAccessKeyId",
                "AwsSecrectAcessKey",
                gameLiftConfig);
    }
}
  1. 这里遇到了第一个问题:Failed to create the GameLiftClient

  2. 解决上述问题后,我尝试使用 AmazonGameLiftClient 列出车队:

AmazonGameLiftConfig gameLiftConfig = new AmazonGameLiftConfig {RegionEndpoint = RegionEndpoint.USWest1};
AmazonGameLiftClient client = new AmazonGameLiftClient(
            "awsAccessKeyId",
            "awsAccessSecretKey",
            gameLiftConfig);
ListFleetsRequest listFleetsRequest = new ListFleetsRequest();
ListFleetsResponse fleets = client.ListFleets(listFleetsRequest);

但我得到以下异常:

NotSupportedException: https://gamelift.us-west-1.amazonaws.com/
System.Net.WebRequest.GetCreator (System.String prefix)
System.Net.WebRequest.Create (System.Uri requestUri)
Amazon.Runtime.Internal.HttpRequest..ctor (System.Uri requestUri)
Amazon.Runtime.Internal.HttpWebRequestFactory.CreateHttpRequest (System.Uri requestUri)
Amazon.Runtime.Internal.HttpHandler`1[System.IO.Stream].CreateWebRequest (IRequestContext requestContext)
Amazon.Runtime.Internal.HttpHandler`1[System.IO.Stream].InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.Unmarshaller.InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.ErrorHandler.InvokeSync (IExecutionContext executionContext)
  1. 我在我的 aws.config 中添加了更多配置来修复它,下面是我的整个 aws.config:
<configuration>
  <configSections>
    <section name="aws" type="Amazon.AWSSection, AWSSDK.Core"/>
    <section name="system.diagnostics" type="System.Diagnostics.DiagnosticsConfigurationHandler" />
    <sectionGroup name="system.net" type="System.Net.Configuration.NetSectionGroup, System">
       <section name="authenticationModules" type="System.Net.Configuration.AuthenticationModulesSection, System" />
       <section name="connectionManagement" type="System.Net.Configuration.ConnectionManagementSection, System" />
       <sectionGroup name="mailSettings" type="System.Net.Configuration.MailSettingsSectionGroup, System">
          <section name="smtp" type="System.Net.Configuration.SmtpSection, System" />
       </sectionGroup>
       <section name="requestCaching" type="System.Net.Configuration.RequestCachingSection, System" />
       <section name="settings" type="System.Net.Configuration.SettingsSection, System" />
       <section name="webRequestModules" type="System.Net.Configuration.WebRequestModulesSection, System" />
     </sectionGroup>
  </configSections>
  <aws>
    <logging logTo="Log4Net"/>
    <csmConfig csmEnabled="false"/>
  </aws>
  <system.diagnostics>
     <trace autoflush="true" />
  </system.diagnostics>
  <system.net>  
    <authenticationModules>  
      <add type="System.Net.DigestClient" />  
      <add type="System.Net.NegotiateClient" />  
      <add type="System.Net.KerberosClient" />  
      <add type="System.Net.NtlmClient" />  
      <add type="System.Net.BasicClient" />  
    </authenticationModules>  
    <connectionManagement>  
      <add address="*" maxconnection="2" />  
    </connectionManagement>  
    <webRequestModules>  
      <add prefix="http"  
           type="System.Net.HttpRequestCreator"  
      />  
      <add prefix="https"  
           type="System.Net.HttpRequestCreator"  
      />  
      <add prefix="file"  
           type="System.Net.FileWebRequestCreator"  
      />
    </webRequestModules>  
  </system.net>  
</configuration>
  1. 现在我得到另一个例外:
MissingMethodException: Method not found: 'System.Net.ServicePoint.SetTcpKeepAlive'.
Amazon.Runtime.Internal.HttpHandler`1[System.IO.Stream].CreateWebRequest (IRequestContext requestContext)
Amazon.Runtime.Internal.HttpHandler`1[System.IO.Stream].InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.Unmarshaller.InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.PipelineHandler.InvokeSync (IExecutionContext executionContext)
Amazon.Runtime.Internal.ErrorHandler.InvokeSync (IExecutionContext executionContext)

有人知道这个例外吗?

我的环境:

  • 操作系统:Mac OS X 10.14.1
  • Unity3d:2018.2.12f1
  • AWS 开发工具包核心:3.3.29.10(.net35)
  • AWS 开发工具包 GameLift:3.3.12.29(.net35)

【问题讨论】:

    标签: c# .net amazon-web-services unity3d amazon-gamelift


    【解决方案1】:

    最终,我找到了在 Unity3d 中使用 GameLiftClient 的方法。

    先决条件:

    • Windows 10
    • JetBrain Rider,Visual Studio 也应该可以工作
    • 将“UnityEngine.dll”放入 C:\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll
    • 在 Unity3d 项目的项目设置中将“脚本运行时版本”设置为“.net35 等效”。

    第 1 步:Github 下载 AWS 开发工具包源并将其解压缩到您喜欢的任何地方。

    下载与您使用的 GameLift 服务器 SDK 兼容的版本会更安全。

    第 2 步:JetBrain Rider 中打开sdk/AWSSDK.Unity.sln。 Visual Studio 也应该可以工作,但我没有与该解决方案兼容的正确 VS 版本。

    第 3 步: 在 Rider 的解决方案面板中,在“服务”下创建一个新的解决方案文件夹,将其命名为“GameLift”。 右键单击“GameLift”文件夹并选择“添加现有项目”。在弹出窗口中,浏览并选择“sdk\src\Services\GameLift\AWSSDK.GameLift.Net35.csproj”。

    现在解决方案应该如下所示:

    第 4 步: 右键单击“AWSSDK.GameLift.Net35.csproj”并选择“编辑 AWSSDK.GameLift.Net35.csproj” 在 Rider 的编辑器面板中,将&lt;ProjectReference Include="..\..\Core\AWSSDK.Core.Net35.csproj"/&gt; 更改为

    <ProjectReference Include="..\..\Core\AWSSDK.Core.Unity.csproj">
      <Project>{5A8B25C1-3D58-4BB6-BF7D-77AD818D9EAD}</Project>
      <Name>AWSSDK.Core.Unity</Name>
    </ProjectReference>
    

    Above ProjectReferece 是从默认情况下包含在解决方案中的任何其他项目设置复制而来的。 不要忘记保存文件。

    第 5 步: 右键单击“AWSSDK.GameLift.Net35.csproj”并选择“Build Selected Projects”。

    第 6 步: 转到“sdk\src\Services\GameLift\bin\Debug\net35”或“sdk\src\Services\GameLift\bin\Release\net35”,将除“UnityEngnine.dll”之外的所有dll复制到您的Unity3d项目中。我将它们放在“资产/AWSSDK”下。

    第 7 步: 使用以下内容创建“Assets/AWSSDK/src/Core/Resources/awsconfig.xml”:

    <?xml version="1.0" encoding="utf-8"?>
    <aws
        region="us-west-1"
        correctForClockSkew="true">
    </aws>
    

    第 8 步: 现在它应该可以使用下面的 sn-p 创建 GameLiftClient:

    Awake()
    {
        UnityInitializer.AttachToGameObject(gameObject);
    
        AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;
    
        AmazonGameLiftConfig gameLiftConfig = new AmazonGameLiftConfig
        {
                RegionEndpoint = RegionEndpoint.USWest1
        };
    
        m_Client = new AmazonGameLiftClient(
                    "awsAccessKeyId",
                    "awsSecretAccessKey",
                    gameLiftConfig);
    }
    

    不要忘记将“awsAccessKey”替换为真实的。 此外,将 AWS 凭证硬编码到客户端中也不安全。因此,请仅将此代码 sn-p 用于测试目的。出于生产目的,AWS Cognito 可用于在运行时分发 AWS 凭证。

    全部完成。

    【讨论】:

    • 您好,在您能够实现这一目标之后,您是否能够对这个客户端做一些事情,并且一旦您为 Android 构建它就可以工作吗?您拥有的任何代码参考。
    • 嗨@Gkills,CreateGameSession 和SearchGameSession 都可以在Windows 和Mac 上运行。我还没有在 Android 上测试过代码。但由于项目设置与 .net35 兼容并使用 Aws Mobile SDK 编译,我认为它可以在 Android 上运行。
    • 将密钥放入客户端确实不安全,您应该使用您控制下的一些代理服务器来验证/授权统一客户端用户,并在成功后将密钥传递给亚马逊服务
    • 嗨@Bob,是的,在客户端硬编码 AWS 凭证是不安全的。这个答案只是说明了如何将 GameLift 集成到客户端中。在我的项目中,我在玩家登录后在运行时从 AWS Cognito 获取凭证。如何集成 Cognito 是另一回事,关于它的文档很多。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    相关资源
    最近更新 更多