Asset Bundle #2- App Dev Series 20
Quick guide of downloading your asset bundle from AWS S3 server to Unity.
In the last tutorial, we have created an asset bundle of the horse model. To make an asset bundle can really do what it should be- become an online resource that would be downloadable- we need to upload it to AWS server, which is what we had done before to create an insurance app.
Here are steps that we would finish in this tutorial:
- Upload assetbundle to S3 bucket
- Locate assetbundle from bucket through script
- Download and import assetbundle to game scene
As for the first step of uploading the object to S3 bucket, you should create a new bucket to continue with this part. You can check my tutorial of how to create a S3 bucket on AWS.
Upload to S3
After created an assetbundle object and a new S3 bucket, we can simply click “Upload” , then add the file of assetbundle to upload it.
Back to your bucket, you should see the file in the list.
Locate file from script
Back to Unity, we need to create an AWS Manager to handle the communication with S3 bucket.
Create an empty object with a AWSManager script attached to it, then make this script a Singleton to make it easier to call.
Now we need to adjust AWSManager to make it available to receive data from S3 bucket. First we would match this script as a S3 client to bucket. This would contain the part of using Amazon Cognito identity, which you can check my tutorial of using it.
You would need to get the credentials of AWS to follow this part.
Select your identity pool and select “Sample code” on the left menu.
Then take part of the credentials example code to AWSManager script to create the S3 client.
This would create the credential of your script to make it match the setting of your bucket.
Then we need to make sure our credential would work to make a connection to S3 bucket. We can locate it in Awake().
We have access the AWS server as a client and locate our bucket name. Let’s run our app with these code. If everything works perfectly, it should get the name of our uploaded assetbundle.
Download from bucket
When click the assetbundle in S3 bucket, there is a link called object url. This is the address of this object. We can use this url to download it to Project.
Create a coroutine with this url and receive it as a gameobject. Within this coroutine, access the url with UnityWebRequestAssetBundle to get the object.
Then we can check if we actually get the horse model.
Now we can continue to get the assetbundle as gameobject and instantiate it to scene by getting the name of matching “horse”.
Run it again, and you should see the horse was imported to your game scene after its downloading.