How to integrate GameLift with Unity3d as a game client

柔情痞子 提交于 2020-01-03 05:04:31

问题


I am trying to use a Unity3d game as GameList client.

According to the GameLift forum, it seems Amazon does not recommend to use a game client as a GameLift client directly.

But I want to give it a try because I do not want one more separate game service.

  1. The first step is downloading AWS SDK source code from GitHub and building the .net35 version dll;

  2. Put the AWSSDK.Core.dll and AWSSDK.GameLift.dll into /Assets/Plugins;

  3. Create a new derived class from MonoBehaviour to create the AmazonGameLiftClient, below is my code:

public class MyGameLiftClient : MonoBehaviour
{
    private void Awake()
    {    
        AmazonGameLiftConfig gameLiftConfig = 
                new AmazonGameLiftConfig {RegionEndpoint = RegionEndpoint.USWest1};
        AmazonGameLiftClient client = new AmazonGameLiftClient(
                "AwsAccessKeyId",
                "AwsSecrectAcessKey",
                gameLiftConfig);
    }
}
  1. Here I encountered the first problem: Failed to create the GameLiftClient

  2. After fixing the above problem, I tried to use the AmazonGameLiftClient to list the fleets:

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

But I get below exception:

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. I added some more configuration into my aws.config to fix it, below is my whole 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. Now I get another exception:
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)

Does anyone have an idea about this exception?

My Environment:

  • OS: Mac OS X 10.14.1
  • Unity3d: 2018.2.12f1
  • AWS SDK Core:3.3.29.10(.net35)
  • AWS SDK GameLift: 3.3.12.29(.net35)

回答1:


Eventually, I find a way to use GameLiftClient in Unity3d.

Prerequisite:

  • Windows 10
  • JetBrain Rider, Visual Studio should also work
  • Put "UnityEngine.dll" to C:\Program Files\Unity\Editor\Data\Managed\UnityEngine.dll
  • Set "Scripting Runtime version" to ".net35 Equivalent" in Project Setting of your Unity3d project.

Step 1: Download AWS SDK Source for from Github and unzip it to anywhere you like.

It is safer to download the version which is compatible with the GameLift Server SDK you use.

Step 2: Open sdk/AWSSDK.Unity.sln in JetBrain Rider. Visual Studio should also work, but I do not own the right version of VS which is compatible with the solution.

Step 3: In solution panel of Rider, create a new solution folder under "Services," name it "GameLift". Right click on the "GameLift" folder and choose "Add Existing Project,". In the popup windows, browse and choose "sdk\src\Services\GameLift\AWSSDK.GameLift.Net35.csproj".

Now the solution should look like:

Step 4: Right click the "AWSSDK.GameLift.Net35.csproj" and choose "Edit AWSSDK.GameLift.Net35.csproj" In the editor panel of Rider, change <ProjectReference Include="..\..\Core\AWSSDK.Core.Net35.csproj"/> to

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

Above ProjectReferece is copied from any other Project setting which is included in the solution by default. Do not forget to save the file.

Step 5: Right-click the "AWSSDK.GameLift.Net35.csproj" and choose "Build Selected Projects".

Step 6: Go to "sdk\src\Services\GameLift\bin\Debug\net35" or "sdk\src\Services\GameLift\bin\Release\net35", copy all dll except the "UnityEngnine.dll" into your Unity3d project. I put them under 'Assets/AWSSDK'.

Step 7: Create 'Assets/AWSSDK/src/Core/Resources/awsconfig.xml' with below content:

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

Step 8: Now it should be able to create GameLiftClient using below snippet:

Awake()
{
    UnityInitializer.AttachToGameObject(gameObject);

    AWSConfigs.HttpClient = AWSConfigs.HttpClientOption.UnityWebRequest;

    AmazonGameLiftConfig gameLiftConfig = new AmazonGameLiftConfig
    {
            RegionEndpoint = RegionEndpoint.USWest1
    };

    m_Client = new AmazonGameLiftClient(
                "awsAccessKeyId",
                "awsSecretAccessKey",
                gameLiftConfig);
}

Do not forget to replace the "awsAccessKey" to the real one. Also, it is not safe to hard code AWS Credentials into the client. So please only use this code snippet for test purpose. For production purpose, AWS Cognito can be used to distribute AWS Credentials at runtime.

All done.



来源:https://stackoverflow.com/questions/53578853/how-to-integrate-gamelift-with-unity3d-as-a-game-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!