This article was initially published on my Medium Page.
Days ago, I published an article about creating a simple signup flow with Unity3D and AWS Cognito. Some days later, Yan Cui published an excellent article about a passwordless integration with Vue, Amplify, and AWS Cognito. I talked with him later, and he asked me the following:
Out of curiosity, what client did you use for Unity, the C# AWS SDK?
I did not use the AWS SDK for .NET; since integrating Unity and Cognito is relatively simple, I used “Vanilla” Unity. At this point, I started to doubt my own words. Is it easier to use Vanilla Unity over the AWS SDK? Let’s see.
Dealing With the AWS SDK for .NET
Do you remember my article about building a monitoring system? I used there the AWS SDK to integrate Unity and S3. Please refer to it for a detailed explanation of the NuGet mechanism.
For the current integration, we need the AWS core and the AWS Cognito packages. Since the AWS SDK functions use asynchronous tasks, we also need the AsyncInterfaces package.
We download the three packages, unzip them, and place the DLL files in the Plugins folder of our project:
The Code
First of all, we create the Cognito client:
Notes:
- We use the AmazonCognitoIdentityProviderClient constructor to create a new Cognito client.
- Since we connect as an anonymous user (without credentials, IAM user, or other), we use the class AnonymousAWSCredentials.
- We specify the region where the Cognito User Pool has been created (In my case, Ohio).
Then, we create the signup function with the following code:
Notes:
- The class Task is a pure .NET mechanism to manage asynchronous processes. You can call it in Unity with the await operator within an async function.
- We add an email attribute to perform an email confirmation.
- We perform the signup thanks to the SignUpRequest and SignUpAsync classes as specified in the documentation.
And here is the confirm function:
Notes:
- We send the code we received by email.
- We perform the signup confirmation thanks to the ConfirmSignUpRequest and ConfirmSignUpAsync classes, as specified in the documentation.
Conclusion
Working with the AWS SDK in Unity is complex: you have to install the SDK through the NuGet website, work with .NET asynchronous classes, create a client, deal with the credentials, and check the AWS documentation to implement the functions.
However, working with the AWS SDK is a good practice; it may be a bit tedious for this particular case, but it is a game changer for complex processes like a file upload to S3. Furthermore, almost all the AWS SDK implementations have the same structure (Task -> Request -> Function), making it much easier after successfully performing your first implementation.
Thanks for reading this article. I hope you liked it!