Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 44 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# Azure App Services for Unity3d
For game developers looking to use Azure App Services (previously Mobile Services) in their Unity project.
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](https://docs.unity3d.com/Manual/UnityWebRequest.html) and [JsonUtility](https://docs.unity3d.com/ScriptReference/JsonUtility.html) 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 UnityRestClient](https://github.com/ProjectStratus/UnityRestClient/archive/master.zip)
* Copy JsonFx 'plugins' and the UnityRestClient 'Source' into project `Assets` folder
2. [Download AppServices](https://github.com/Unity3dAzure/AppServices/archive/master.zip)
1. [Download AppServices](https://github.com/Unity3dAzure/AppServices/archive/master.zip)
* Copy 'AppServices' into project `Assets` folder.
3. Create an Azure App Service [Mobile App](https://portal.azure.com)
2. Create an Azure App Service [Mobile App](https://portal.azure.com)
* Create a Table (using Easy Tables) for app data.

## Azure App Services Demos for Unity 5
Try the [Azure App Services Demos](https://github.com/Unity3dAzure/AppServicesDemo) project for Unity 5 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](https://portal.azure.com) and run it right inside the Unity Editor.)
Try the [Azure App Services Demos](https://github.com/Unity3dAzure/AppServicesDemo) 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](https://portal.azure.com) 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](http://www.deadlyfingers.net/azure/azure-app-services-for-unity3d/).

## Supported Features
Expand All @@ -30,27 +31,27 @@ 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
void Login(MobileServiceAuthenticationProvider provider, string token, Action<IRestResponse<MobileServiceUser>> callback = null);
void InvokeApi<T>(string apiName, Action<IRestResponse<T>> callback = null) where T : new();
### [MobileServiceClient Interface](https://github.com/Unity3dAzure/AppServices/blob/master/client/IAzureMobileServiceClient.cs)
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
void Insert<T>(T item, Action<IRestResponse<T>> callback = null) where T : new();
void Read<T>(Action<IRestResponse<List<T>>> callback = null) where T : new();
void Update<T>(T item, Action<IRestResponse<T>> callback = null) where T : new();
void Delete<T>(string id, Action<IRestResponse<T>> callback = null) where T : new();
void Query<T>(CustomQuery query, Action<IRestResponse<List<T>>> callback = null) where T : new();
void Query<T>(CustomQuery query, Action<IRestResponse<T>> callback = null) where T : INestedResults, new();
void Lookup<T>(string id, Action<IRestResponse<T>> callback = null) where T : new();
### [MobileServiceTable Interface](https://github.com/Unity3dAzure/AppServices/blob/master/table/IAzureMobileServiceTable.cs)
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 RestSharp;
using Pathfinding.Serialization.JsonFx;
using Unity3dAzure.AppServices;

```
Expand All @@ -68,25 +69,38 @@ void Start () {
```
```
private void ReadItems() {
_table.Read<TodoItem>(OnReadItemsCompleted);
StartCoroutine( _table.Read<TodoItem>(OnReadItemsCompleted) );
}

private void OnReadItemsCompleted(IRestResponse<List<TodoItem>> response) {
if ( response.StatusCode == HttpStatusCode.OK) {
Debug.Log("OnReadItemsCompleted data: " + response.Content);
List<TodoItem> items = response.Data;
Debug.Log( "Todo items count: " + items.Count);
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 {
ResponseError err = JsonReader.Deserialize<ResponseError>(response.Content);
Debug.Log("Error " + err.code.ToString() + " " + err.error + " Uri: " + response.ResponseUri);
Debug.LogWarning ("Read Error Status:" + response.StatusCode + " Url: " + response.Url);
}
}
```

## Dependencies
The REST service implements [UnityRestClient](https://github.com/ProjectStratus/UnityRestClient) which uses [JsonFx](https://bitbucket.org/TowerOfBricks/jsonfx-for-unity3d-git/) to parse JSON data.
## Known issues
* There is an issue with [PATCH on Android using UnityWebRequest with Azure App Services](http://answers.unity3d.com/questions/1230067/trying-to-use-patch-on-a-unitywebrequest-on-androi.html). 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:
1. Install the "method-override" package.
```
npm install method-override --save
```
2. In 'app.js' file insert:
```
var methodOverride = require('method-override');
// after the line "var app = express();" add
app.use(methodOverride('X-HTTP-Method-Override'));
```

This will enable PATCH requests to be sent on Android.

## Supports
## Supported platforms
Will work on all the platforms [UnityWebRequest](https://docs.unity3d.com/Manual/UnityWebRequest.html) supports including:
* Unity Editor and Standalone players
* iOS
* Android
* Windows
Expand Down
16 changes: 8 additions & 8 deletions authentication/MobileServiceAuthenticationProvider.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace Unity3dAzure.AppServices
{
public enum MobileServiceAuthenticationProvider
{
MicrosoftAccount = 0,
Google = 1,
Twitter = 2,
Facebook = 3,
WindowsAzureActiveDirectory = 4
}
public enum MobileServiceAuthenticationProvider
{
MicrosoftAccount = 0,
Google = 1,
Twitter = 2,
Facebook = 3,
WindowsAzureActiveDirectory = 4
}
}
51 changes: 24 additions & 27 deletions client/IAzureMobileServiceClient.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,36 @@
using System.Collections;
using System.Collections.Generic;
using System;
using RestSharp;
using System;
using System.Collections;

namespace Unity3dAzure.AppServices
{
[CLSCompliant(false)]
public interface IAzureMobileServiceClient
{
MobileServiceTable<E> GetTable<E>(string tableName) where E : class;

/// <summary>
/// Client-directed login (sdk) for single sign on
/// https://msdn.microsoft.com/en-us/library/azure/jj710106.aspx
/// </summary>
void Login(MobileServiceAuthenticationProvider provider, string token, Action<IRestResponse<MobileServiceUser>> callback = null);

/// <summary>
/// TODO: Service-directed login (webview)
/// </summary>
//void Login(MobileServiceAuthenticationProvider provider);

/// <summary>
/// Invokes a custom API using GET HTTP method
/// </summary>
void InvokeApi<T>(string apiName, Action<IRestResponse<T>> callback = null) where T : new();
public interface IAzureMobileServiceClient
{
MobileServiceTable<E> GetTable<E> (string tableName) where E : class;

/// <summary>
/// Client-directed login (sdk) for single sign on
/// https://msdn.microsoft.com/en-us/library/azure/jj710106.aspx
/// </summary>
IEnumerator Login (MobileServiceAuthenticationProvider provider, string token, Action<IRestResponse<MobileServiceUser>> callback = null);

/// <summary>
/// TODO: Service-directed login (webview)
/// </summary>
//void Login(MobileServiceAuthenticationProvider provider);

/// <summary>
/// Invokes a custom API using GET HTTP method
/// </summary>
IEnumerator InvokeApi<T> (string apiName, Action<IRestResponse<T>> callback = null) where T : new();

/// <summary>
/// Invokes a custom API for HTTP Methods: GET, POST, PUT, PATCH, DELETE
/// </summary>
void InvokeApi<T>(string apiName, Method httpMethod, Action<IRestResponse<T>> callback = null) where T : new();
IEnumerator InvokeApi<T> (string apiName, Method httpMethod, Action<IRestResponse<T>> callback = null) where T : new();

/// <summary>
/// Invokes a custom API with body object
/// </summary>
void InvokeApi<T> (string apiName, Method httpMethod, T body, Action<IRestResponse<T>> callback = null) where T : new();
}
IEnumerator InvokeApi<T> (string apiName, Method httpMethod, T body, Action<IRestResponse<T>> callback = null) where T : new();
}
}
Loading