Home

Awesome

Azure App Services for Unity

For Unity developers looking to use Azure App Services (previously Mobile Services) in their Unity game / app.

What's new

Please be aware this latest update of the library brings a number of changes to the namespace, some class names and method calls have been updated as Mobile Services is replaced by App Services. There is also a dependency on a shared RESTClient for Unity so that multiple Unity libraries for Azure can be supported without adding duplicate scripts.

External dependencies

First download the shared REST Client library for Unity and extract the contents into your Unity project "Assets" folder.

Requirements

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

  1. Download AppServices and REST Client for Unity.
    • Copy 'AppServices' and 'RESTClient' into project Assets folder.
  2. 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

AppServiceClient API

APIDescription
LoginWithFacebookClient-directed login using user access token.
LoginWithTwitterClient-directed login using access token and access token secret.
LoginWithGoogleClient-directed login using access token and id token.
LoginWithMicrosoftAccountClient-directed login using access token.
LoginWithAADClient-directed login using access token.
LogoutLogout current user
InvokeApiInvoke custom API (Easy API) using GET, POST, PUT, PATCH or DELETE

AppServiceTable API

APIDescription
InsertCreate a new item.
ReadGet a list of items.
UpdateUpdate an item’s data using id property.
DeleteDelete an item using id property.
QueryGet a list of results using a custom query.
LookupGet an item’s data using id property.

AppServiceClient Interface

LoginWithFacebook(string accessToken, Action<IRestResponse<AuthenticatedUser>> callback = null);
LoginWithTwitter(string accessToken, string accessTokenSecret,	Action<IRestResponse<AuthenticatedUser>> callback = null);
LoginWithGoogle(string accessToken, string idToken,Action<IRestResponse<AuthenticatedUser>> callback = null);
LoginWithMicrosoftAccount(string accessToken,Action<IRestResponse<AuthenticatedUser>> callback = null);
LoginWithAAD(string accessToken, Action<IRestResponse<AuthenticatedUser>> callback = null);
Logout(Action<IRestResponse<string>> callback = null);

AppServiceClient (Easy APIs) Interface

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<B,T> (string apiName, Method httpMethod, B body, Action<IRestResponse<T>> callback = null) where T : new();

AppServiceTable (Easy Tables) 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>(TableQuery query, Action<IRestResponse<T[]>> callback = null) where T : new();
Query<T>(TableQuery 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 Azure.AppServices;
[System.Serializable]
public class TodoItem : DataModel {
  public string text;
  public bool complete;
}
using System.Collections
using System.Collections.Generic;
using UnityEngine;
using System;
using RESTClient;
using Azure.AppServices;
private AppServiceClient _client;
private AppServiceTable<TodoItem> _table;
void Start () {
  _client = AppServiceClient.Create("unityapp"); // <- add your App Service account here!
  _table = _client.GetTable<TodoItem>("TodoItem");
  ReadItems();
}
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.Length);
  } else {
    Debug.LogWarning ("Read Error Status:" + response.StatusCode + " Url: " + response.Url);
  }
}

Known issues

This will enable PATCH requests to be sent on Android.

Supported platforms

Intended to work on all the platforms UnityWebRequest supports including:

Questions or tweet @deadlyfingers