Azure App Services for Unity3d
For game developers looking to use Azure App Services (previously Mobile Services) in their Unity project. This App Services REST API library for Unity is quite similar in structure to the Azure Mobile Services SDK so if you've used that before you should feel relatively at home.
Prerequisites
Requires Unity v5.3 or greater as UnityWebRequest and JsonUtility features are used. Unity will be extending platform support for UnityWebRequest so keep Unity up to date if you need to support these additional platforms.
How to setup App Services with a new Unity project
- Download AppServices
- Copy 'AppServices' into project
Assets
folder.
- Copy 'AppServices' into project
- Create an Azure App Service Mobile App
- Create a Table (using Easy Tables) for app data.
Azure App Services Demos for Unity 5
Try the Azure App Services Demos project for Unity v5.4.x on Mac / Windows. (The demo project has got everything already bundled in and does not require any additional assets to work. Just wire it up with your Azure App Service and run it right inside the Unity Editor.) For detailed instructions read my developer blog on how to setup Azure App Services and Unity demo project.
Supported Features
MobileServiceClient
API | Description |
---|---|
Login | Client-directed login using access token. |
InvokeApi | Invoke custom API (Easy API) using GET, POST, PUT, PATCH or DELETE |
MobileServiceTable
API | Description |
---|---|
Insert | Create a new item. |
Read | Get a list of items. |
Update | Update an item’s data using id property. |
Delete | Delete an item using id property. |
Query | Get a list of results using a custom query. |
Lookup | Get an item’s data using id property. |
MobileServiceClient Interface
Login(MobileServiceAuthenticationProvider provider, string token, Action<IRestResponse<MobileServiceUser>> callback = null);
InvokeApi<T>(string apiName, Action<IRestResponse<T>> callback = null) where T : new();
InvokeApi<T>(string apiName, Method httpMethod, Action<IRestResponse<T>> callback = null) where T : new();
InvokeApi<T>(string apiName, Method httpMethod, T body, Action<IRestResponse<T>> callback = null) where T : new();
MobileServiceTable Interface
Insert<T>(T item, Action<IRestResponse<T>> callback = null) where T : new();
Read<T>(Action<IRestResponse<T[]>> callback = null) where T : new();
Update<T>(T item, Action<IRestResponse<T>> callback = null) where T : new();
Delete<T>(string id, Action<IRestResponse<T>> callback = null) where T : new();
Query<T>(CustomQuery query, Action<IRestResponse<T[]>> callback = null) where T : new();
Query<T>(CustomQuery query, Action<IRestResponse<T>> callback = null) where T : INestedResults, new();
Lookup<T>(string id, Action<IRestResponse<T>> callback = null) where T : new();
Sample usage
using UnityEngine;
using System;
using System.Net;
using System.Collections.Generic;
using Unity3dAzure.AppServices;
private MobileServiceClient _client;
private MobileServiceTable<TodoItem> _table;
void Start () {
_client = new MobileServiceClient(appUrl); // <- add your app url here.
_table = _client.GetTable<TodoItem>("TodoItem");
}
private void ReadItems() {
StartCoroutine( _table.Read<TodoItem>(OnReadItemsCompleted) );
}
private void OnReadItemsCompleted(IRestResponse<TodoItem[]> response) {
if (!response.IsError) {
Debug.Log ("OnReadCompleted: " + response.Url + " data: " + response.Content);
TodoItem[] items = response.Data;
Debug.Log ( "Todo items count: " + items.Count);
} else {
Debug.LogWarning ("Read Error Status:" + response.StatusCode + " Url: " + response.Url);
}
}
Known issues
- There is an issue with PATCH on Android using UnityWebRequest with Azure App Services. Android won't recognize the "PATCH" http method currently required to update an item in App Services. One workaround is to enable the
X-HTTP-Method-Override
header. Here's the quick fix for App Services running node backend:- Install the "method-override" package.
npm install method-override --save
- In 'app.js' file insert:
var methodOverride = require('method-override'); // after the line "var app = express();" add app.use(methodOverride('X-HTTP-Method-Override'));
- Install the "method-override" package.
This will enable PATCH requests to be sent on Android.
Supported platforms
Will work on all the platforms UnityWebRequest supports including:
- Unity Editor and Standalone players
- iOS
- Android
- Windows
Questions or tweet #Azure #GameDev @deadlyfingers