Creating an AWS Manager in Unity: Downloading the AWS SDK
Now that we have an asset bundle uploaded to S3, we’ll want to download it from within Unity. To do that, we’ll need to go to this page and download the SDK.
After downloading, unzip and look for the S3 package. We can ignore everything else. Import the s3 package into unity, I’ll do it by dragging the file into the Assets window.
Setting up the AWS Manager
We’ll create a script called AWSManager as well as a similarly named Empty that we’ll attach the script to and we’ll make it a singleton.
From this barebones object, we’ll add the necessary amazon libraries:
Next, we’ll use Amazons command UnityInitializer to attach the library to the gameObject assuring it will never be destroyed.
Next, we’ll initialize Amazons HTTP client.
We’ll also need to define the Region, we’ll also be using this variable later, again, your region may be different:
Before I continue to the next step, I’m going to create a new script called Keys.cs which we’ll include in my .gitIgnore file. I’m doing this because the next step uses sensitive key information that must not be hardcoded and accidentally published on github for security reasons.
And then create the .gitIgnore entry:
This will assure our security keys won’t be harvested by bots on github. Be sure to push .gitIgnore first just to be safe and for extra safety, always remove the keys file before pushing as well.
Continuing on…We’ll now set up our Client, which is almost the same as creating a singleton except if the client is null, we’ll create one with our sensitive credentials and this is where our Keys file comes in, if you need help with setting up the S3 accounts, please refer to my earlier article where I go into detail on how to do it:
A new AmazonS3Client is now created with our hidden key with my appropriate region end point. Check your amazonAWS account for yours, it may be different. Also, note the final parameter is _S3Region which we defined earlier. If all goes well, you’ll get a message you’re connected to the AWSManager!
Next, we’re going to list the files in our bucket looking for the horse file we uploaded yesterday, the bucket name is “sdaranatomy” so we’ll need to specify that.
Next we’ll use the S3Client Async method to pull data from AWS, we’ll use a variable called responseObject to put the data into and using a lambda, we’ll set up the code to pull the info:
Inside the Async request, we’ll check if the response object has an exception, if it doesn’t, that’s good! we can continue to look into the response object via S3Objects, we’ll pass another lambda expression that uses obj for a variable, and we’ll go through each item inside of responseObject, and we’ll Debug.Log every name.
If all goes well, our horse object will appear!
And next, we’re ready to download!