2

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)
Yongyi
  • 327
  • 2
  • 12

1 Answers1

3

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:

enter image description here

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.

Community
  • 1
  • 1
Yongyi
  • 327
  • 2
  • 12
  • Hi, were you able to do something with this client, after you were able to achieve this, and does it work once you build it for Android? any code references you have. – Gkills Jan 07 '19 at 14:48
  • Hi @Gkills, both CreateGameSession and SearchGameSession work on Windows and Mac. I have not tested the code on Android. But since the project setting is .net35 compatible and compiled with Aws Mobile SDK, I assume it would work on Android. – Yongyi Jan 21 '19 at 12:19
  • it is really unsafe to put secret keys into the client, you should instead use some proxy server under your control to authenticate/authorize the unity client users and upon success to communicate the keys to the amazon services – Bob Feb 27 '19 at 01:20
  • Hi @Bob , yes it is not safe to hard code AWS Credential in the client. This answer just illustrates how to integrate GameLift into the client. In my project, I get the Credential from AWS Cognito in runtime after player login. How to integrate Cognito is another story, and there are plenty of documents about it. – Yongyi Feb 28 '19 at 04:20