Microsoft Azure is the leading cloud solution and a personal favorite when it comes to secure and compliant cloud providers. It’s easy to use, provides a lot of customizable services and has global availability with 50 regions worldwide. So, when it comes to developing a project with global reach, Microsoft Azure is a good and reliable option. Unity3D is the leading game development and Mixed Reality engine, currently touching more than 770 million gamers globally.
If you are an application developer, connecting a powerful engine with a powerful Cloud system is crucial for reaching more customers. However, as of now, there was no Azure support for Unity3D.
Today, this is changing. LightBuzz is presenting the first complete Azure SDK for Unity3D. The SDK allows you to consume remote Azure App Services and even store data locally in a SQLite database. Everything is secured using HTTPS. The framework supports all of the major Unity platforms, including Android, iOS, Windows Standalone, Mac OS, UWP, and HoloLens.
Oh, did I mention that it’s open-source, too?
LightBuzz is a New York-based company that has been developing Mobile and Cloud solutions for Fortune 500 and ambitious startup companies since 2012.
LightBuzz Azure SDK for Unity
The whole project is hosted on GitHub — remember to give it a thumbs-up! Now, I am going to give you an overview of the Azure SDK for Unity.
Azure App Services
The LightBuzz Azure SDK for Unity consumes Azure App Service APIs. Azure App Service Web Apps (or just Web Apps) are cloud services for hosting web applications, REST APIs, and mobile backends. The SDK supports all of the HTTP(S) method requests on every platform.
- GET
- POST
- PUT
- PATCH
- DELETE
HTTPS support
The LightBuzz SDK is built with security in mind. The native Microsoft HttpClient modules do not support HTTPS in Unity. Our team has built the HTTP requests from scratch using the UnityWebRequest class. This way, your data is encrypted and transmitted securely. You do not need to do any special changes to support HTTPS, other than providing the “https://” URL to your Azure Web App.
SQLite
Unlike most of the available SDKs, the LightBuzz Azure SDK for Unity fully supports local database storage. This means you can use the Azure App Services to store data into a local SQLite database. You can sync your local data with the remote server, performing pull and push operations.
As a result, your customers can use your app or game without an active Internet connection! Your app will store the data locally and will sync with the remote server when an active Internet connection is available.
The local database is using the official version of SQLite. SQLite is the most popular and lightweight relational database system for desktop and mobile devices. For UWP, we are using SQLitePCL, which is Microsoft’s recommendation for Windows Store apps.
Supported Platforms
The LightBuzz Azure SDK for Unity supports every major Unity platform:
- Unity Editor
- Android
- iOS
- Windows Desktop (Standalone)
- MacOS (Standalone)
- Universal Windows Platform (UWP) + HoloLens
Requirements
To use the SDK, Unity 2017 LTS or Unity 2018 is recommended.
Scripting Runtime
The SDK is built with the latest C# features, so you need to use the .NET 4.x Scripting Runtime version.
In Unity 2018, the scripting runtime is set to 4.x by default.
In Unity 2017, you need to explicitly select Experimental (.NET 4.6 Equivalent).
Build Settings
Using the SDK, you can apply the proper Unity Build Settings automatically. On the Unity menu bar, select LightBuzz → Apply Build Settings for… and then select the target platform.
We have done all of the heavy-lifting, so the SDK will automatically apply the proper build settings for you!
How to use
In the included samples, we have created a simple demo that implements Microsoft’s ToDo List example. To use the sample, you need to sign up for a free Azure account and create an App Service with a simple ToDo table.
After you clone or download the repository, open the Samples folder and locate the Sample.cs file.
For your convenience, the SDK is using the LightBuzz service URL by default:
private string mobileAppUri = "https://testtodolightbuzz.azurewebsites.net";
You can also specify whether you would like to have local SQLite database support:
private bool supportLocalDatabase = true;
We have implemented an abstract LightBuzzMobileServiceClient class that handles all of the underlying Azure functionality and the data-to-C# object mapping. Simply define your database tables by implementing the 2 abstract methods in LightBuzzMobileServiceClient. An example is included in the Unity Package (SampleMobileClient.cs).
We also provide a generic Data Access Object for you to use, called AppServiceTableDAO. The AppServiceTableDAO supports all of the common CRUD operations, such as Get, Insert, Delete, etc. All you need to do is call the proper C# methods.
Using the code is fairly simple. Let me show you…
Initialization
The first thing you need to do is declare your Azure client and Data Access Object. You can do that in your main Unity MonoBehavior class.
private LightBuzzMobileServiceClient azureClient;
private AppServiceTableDAO<TodoItem> todoTableDAO;
Then, you need to initialize the Azure client, as well as the data access object, specifying whether you want local SQLite support.
private async Task Init()
{
azureClient = new SampleMobileClient(mobileAppUri, supportLocalDatabase);
await azureClient.InitializeLocalStore();
todoTableDAO = new AppServiceTableDAO<TodoItem>(azureClient);
}
Get data
This is how to list all of the ToDo items of the table:
private async Task Get()
{
List<TodoItem> list = await todoTableDAO.FindAll();
foreach (TodoItem item in list)
{
Debug.Log("Text: " + item.Text);
}
}
As you can see, there is no need to type SQL commands — just plain good C#. The Data Access Object is handling that for you!
Insert data
This is a POST request that inserts data:
private async Task Insert()
{
TodoItem item = new TodoItem
{
Text = "Hello World!"
};
await todoTableDAO.Insert(item);
}
Delete data
Finally, this is how to delete an item from the list:
private async Task Delete()
{
List<TodoItem> list = await todoTableDAO.FindAll();
TodoItem item = list.LastOrDefault();
if (item != null)
{
await todoTableDAO.Delete(item);
}
}
Sync local and remote data
In case you are using the local SQLite database for offline functionality, here is how to perform the pull and push requests:
private async Task Sync()
{
await azureClient.Sync();
}
So, do you like the Azure SDK for Unity? Have any questions? Let me know in the comments below! And… enjoy your cloud adventures!
Resources
A series of detailed tutorials and new features will follow.
LightBuzz is a New York-based company that has been developing Mobile and Cloud solutions for Fortune 500 and ambitious startup companies since 2012.
Hi this is Nanaji,
Good to here Azure SDK for Unity. I need some clarification regarding select statement queries. Here you told no need any SQL scripting. So how can store my blob data and retrieve and how to get data with multiple condition using Azure SDK. If it’s possible share any example regrading this.
Thank You so much your hard work.
Hello Nanaji,
Thank you for your comment!
In order to insert an object, you create the object and then just call the Insert method:
TodoItem item = new TodoItem { Text = "Hello World!" };
await todoTableDAO.Insert(item);
To retrieve from the database you always retrieve a list of objects as shown below:
List<TodoItem> list = await todoTableDAO.FindAll();
And if you want to use conditions:
List<TodoItem> list = await todoTableDAO.FindAll(x=>x.Text=="Hello World!" && x.Completed==false);
A post is coming soon with more examples, but in the unity package, you can already find a sample MonoBehavior page with examples for the use of the SDK.
Excuse me,does this Azure SDK for Unity required an Azure account to use?
Hello! The demo application on GitHub is working with a LightBuzz test server. To run the Azure SDK on your own Azure projects, you would need an Azure account, indeed.
I loaded the SDK an tested locally. I created the ASP.NET TODO application from the Microsoft tutorial and tested that both locally and running as a Web Service online connected to an Azure SQL database. I then copied the URI of my Azure Web Service and pasted it into Mobile App Uri property in the Unity Inspector foe the LightBuzz Azure Sample game object.
When I click SYNC nothing is synchronized to my Azure database. When I check the TableCloud property of todoTableDAO (an AppServiceTableDAO object), it is null, so it does not appear that the cloud properties are being initialized.
1. Does the Sample.cs sample code supp0ort synching to the cloud as is or do I have to make modifications for it to do so?
2. If I uncheck Support Local Database then the app hangs when I click Get.
3. Is there (or will there be support) for Azure storage as well as Azure SQL?
Thanks!
Hello Robert,
Thank you for your interest in our project!
To answer your questions:
1. The sample code should work as is. You just need to change the Mobile App Uri to point to your Azure App Service.
2. If you are still facing problems, please open a GitHub issue providing as many details as possible, so we can investigate.
3. Adding support for Azure Storage is in our pipeline. Could you share your use case for Azure SQL?
Stay tuned for updates and let us know if you have any questions.
I see now that the TableCloud property is set when I set Support Local Database to false. So with this set as false the SDK bypasses local storage and talks straight the cloud, correct?
The problem I am having syncing to Azure SQL is in my web service, not your SDK. I see now that I did not implement the CORS To Do service!
Yes, when you set Support local database to false the SDK sends requests directly to your Azure App Service.
Please let us know if you have any more questions.
I Robert,
I have the same problem as you. I implemented my own App Service with a custom database that has the same fields as the sample database with 2 additional fields. I confirmed that my App Service works both in the local and remote web environments on my PC where I can add, update, and delete ToDo items. When I tested it on my mobile device the cloud database doesn’t sync up.
You mentioned that you had to “implement the CORS ToDo service” to fix the problem. Could you describe how you did that? All this is new to me. I watched the “CORS and API Management” video at Microsoft’s Azure site, but the steps appeared to be specific to javascript on a web page, but the web page for the Azure App Service is auto-generated, so I don’t know how to apply the steps in the video.
Any assistance would be appreciated.
Thanks
To enable CORS, simply login to your Azure Portal, select the desired App Service, and specify the desired origins. In the “Allowed origins” field, you can specify the “*” notation to allow any origin to access your service.
Here is a step-by-step tutorial by Microsoft.