Share via


Azure Storage SDK for Unity

Note

This is a Sandbox project. The content in this article is unsupported, and therefore may be out of date or not in a working state. That said, we still think it was awesome enough to include here. Enjoy!

Azure Storage for Gaming

Azure Storage provides different types of storage that could be used in a variety of gaming scenarios. For example, you could build a system using Blob Storage to save a user's game state for later play. Table Storage could be used as a quick and easy database to store simple values.

Important

This is an experimental Unity SDK for Azure Storage. As such, please note that this SDK is not supported and is not provided by the Azure Storage team. If you run into problems, please let us know using the GitHub Issues page for the SDK.

Get the source Try it now

Requirements

Compatibility

This has been tested with the following Unity exporters. Others wil likely work, however we haven't tested every possible platform and combination. Please let us know if you've had success!

  • Windows/Mac Standalone
  • iOS
  • Android
  • UWP (IL2CPP)
  • Unity Editor

Import the SDK

To import the SDK into your own project, make sure you have downloaded the latest .unitypackage from GitHub. Then, do the following:

  1. Open Unity and select Edit > Project Settings > Player to open the PlayerSettings panel.

  2. Select .NET 4.x Equivalent from the Scripting Runtime Version dropdown and .NET Standard 2.0 from the Api Compatibility Level dropdown in the Configuration section.

    Scripting Configuration dialog

  3. Add the .unitypackage you downloaded in the first step to Unity by using the Assets > Import Package > Custom Package menu option.

  4. In the Import Unity Package box that pops up, you can select which things you'd like to import. By default everything will be selected. If you don't care to use the sample, you can uncheck that box.

  5. Click the Import button to add the items to your project.

With the package added, you can now use the normal API in your scripts as you would in any other application. Please take a look at the corresponding sample (also included in each .unitypackage) which demonstrates how to use each of the services to perform simple tasks.

Please refer to the Azure Storage Docs for even more samples and tutorials on using the API.

Unity 2018.1 and SSL support

Due to a limitation in Unity 2018.1 (fixed in 2018.2), HTTPS requests using the standard .NET networking stack (i.e. not using UnityWebRequest) will fail. To workaround this, you will need to modify the DefaultEndpointsProtocol entry in your connection string to use http instead of https. This means your data will not be encrypted to and from the server. We highly recommend upgrading your project to Unity 2018.2 if possible. Here's an example of a connection string using http:

`DefaultEndpointsProtocol=http;AccountName=yourazureaccount;AccountKey=abcdef12345;EndpointSuffix=core.windows.net`

Try the Sample

To use the sample, you will need to have an Azure Storage account setup along with a valid connection string. You can learn more about that here.

To use the sample, do the following:

  1. Download the Unity SDKs repo from GitHub (or import it from the .unitypackage and continue to step 4).

  2. Unzip to a location on your hard drive.

  3. Open Unity 2018.1 (or greater) and point it to the Storage directory inside the unzipped package.

  4. In the Project window, double-click the AzureSample scene inside the AzureSamples\Storage directory to open the main scene for the sample.

  5. In this scene, select the StorageObject item in the Hierarchy window.

  6. With StorageObject selected, you'll notice that there are blank Connection String entries in the Inspector window. Fill in the these entries with your valid connection string as shown on the Azure portal, but remember to change the endpoint to use http as described above. You can change the names of the other items if you wish, but the defaults should work as-is. You can find your connection strings in the Azure Portal as shown.

    Azure Storage Keys in Azure Portal

  7. Run the project from within the editor by clicking the Play button. Alternatively, you can export to the platform of your choosing and run there.

  8. At this point, you can click the button for any of the four storage types and watch the output window. If things are setup and working, you will see the sample test a standard workflow.

The code for the sample is broken out into four separate scripts, one for each storage type. Take a look at each to learn more about how it works.

Cosmos DB Table API

Cosmos DB is Microsoft's globally distributed, multi-model database. One of the data models available for Cosmos DB is Table API. Applications written for Azure Table storage can migrate to Azure Cosmos DB by using the Table API with no code changes and take advantage of premium capabilities. If you have a Cosmos DB database, you can use the Table Storage client SDK we provide to access it from your Unity game. Check the instructions here on how to find out your connection string.

Next Steps