Skip to content

Commit c29ac88

Browse files
author
David Douglas
committed
⚡ Azure Functions for Unity
1 parent 154944b commit c29ac88

File tree

4 files changed

+229
-0
lines changed

4 files changed

+229
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ ExportedObj/
2525

2626
# Unity3D generated meta files
2727
*.pidb.meta
28+
*.meta
2829

2930
# Unity3D Generated File On Crash Reports
3031
sysinfo.txt
3132

3233
# Builds
3334
*.apk
3435
*.unitypackage
36+
37+
# IDEs
38+
/.vscode
39+
omnisharp.json
40+
.editorconfig

AzureFunction.cs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Azure.AppServices;
5+
using RESTClient;
6+
using UnityEngine;
7+
using System.Collections;
8+
using System;
9+
10+
namespace Azure.Functions {
11+
public class AzureFunction {
12+
private AzureFunctionClient client;
13+
14+
private string name; // Azure Function name
15+
private string key; // Azure Function key
16+
private string apiPath; // Azure Functions API path
17+
18+
private const string API = "api";
19+
private const string PARAM_CODE = "code";
20+
21+
public AzureFunction(string name, AzureFunctionClient client, string key = null, string apiPath = API) {
22+
this.client = client;
23+
this.name = name;
24+
this.key = key;
25+
this.apiPath = apiPath;
26+
}
27+
28+
public override string ToString() {
29+
return name;
30+
}
31+
32+
public IEnumerator Get<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
33+
yield return InvokeApi<T>(Method.GET, callback, path, query);
34+
}
35+
36+
public IEnumerator Post<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
37+
yield return InvokeApi<T>(Method.POST, callback, path, query);
38+
}
39+
40+
public IEnumerator Patch<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
41+
yield return InvokeApi<T>(Method.PATCH, callback, path, query);
42+
}
43+
44+
public IEnumerator Put<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
45+
yield return InvokeApi<T>(Method.PUT, callback, path, query);
46+
}
47+
48+
public IEnumerator Delete<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
49+
yield return InvokeApi<T>(Method.DELETE, callback, path, query);
50+
}
51+
52+
public IEnumerator Post<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
53+
yield return InvokeApi<B, T>(body, Method.POST, callback, path, query);
54+
}
55+
56+
public IEnumerator Patch<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
57+
yield return InvokeApi<B, T>(body, Method.PATCH, callback, path, query);
58+
}
59+
60+
public IEnumerator Put<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
61+
yield return InvokeApi<B, T>(body, Method.PUT, callback, path, query);
62+
}
63+
64+
public IEnumerator Delete<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
65+
yield return InvokeApi<B, T>(body, Method.DELETE, callback, path, query);
66+
}
67+
68+
#region Variant methods for parsing JSON arrays
69+
70+
public IEnumerator GetArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
71+
yield return InvokeApiArray<T>(Method.GET, callback, path, query);
72+
}
73+
74+
public IEnumerator PostArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
75+
yield return InvokeApiArray<T>(Method.POST, callback, path, query);
76+
}
77+
78+
public IEnumerator PatchArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
79+
yield return InvokeApiArray<T>(Method.PATCH, callback, path, query);
80+
}
81+
82+
public IEnumerator PutArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
83+
yield return InvokeApiArray<T>(Method.PUT, callback, path, query);
84+
}
85+
86+
public IEnumerator DeleteArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
87+
yield return InvokeApiArray<T>(Method.DELETE, callback, path, query);
88+
}
89+
90+
public IEnumerator PostArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
91+
yield return InvokeApiArray<B, T>(body, Method.POST, callback, path, query);
92+
}
93+
94+
public IEnumerator PatchArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
95+
yield return InvokeApiArray<B, T>(body, Method.PATCH, callback, path, query);
96+
}
97+
98+
public IEnumerator PutArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
99+
yield return InvokeApiArray<B, T>(body, Method.PUT, callback, path, query);
100+
}
101+
102+
public IEnumerator DeleteArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
103+
yield return InvokeApiArray<B, T>(body, Method.DELETE, callback, path, query);
104+
}
105+
106+
#endregion
107+
108+
#region Async methods for parsing JSON objects
109+
110+
private IEnumerator InvokeApi<T>(Method httpMethod = Method.GET, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
111+
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
112+
request.SetQueryParams(query);
113+
if (!string.IsNullOrEmpty(key)) {
114+
request.AddQueryParam(PARAM_CODE, key, true);
115+
}
116+
yield return request.Request.Send();
117+
if (typeof(T) == typeof(string)) {
118+
request.GetText(callback);
119+
} else {
120+
request.ParseJson<T>(callback);
121+
}
122+
}
123+
124+
private IEnumerator InvokeApi<B, T>(B body, Method httpMethod = Method.POST, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
125+
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
126+
request.SetQueryParams(query);
127+
if (!string.IsNullOrEmpty(key)) {
128+
request.AddQueryParam(PARAM_CODE, key, true);
129+
}
130+
if (body != null) {
131+
request.AddBody(body);
132+
}
133+
yield return request.Request.Send();
134+
if (typeof(T) == typeof(string)) {
135+
request.GetText(callback);
136+
} else {
137+
request.ParseJson<T>(callback);
138+
}
139+
}
140+
141+
#endregion
142+
143+
#region Async methods for parsing JSON arrays
144+
145+
private IEnumerator InvokeApiArray<T>(Method httpMethod = Method.GET, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
146+
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
147+
if (!string.IsNullOrEmpty(key)) {
148+
request.AddQueryParam(PARAM_CODE, key, true);
149+
}
150+
yield return request.Request.Send();
151+
request.ParseJsonArray<T>(callback);
152+
}
153+
154+
private IEnumerator InvokeApiArray<B, T>(B body, Method httpMethod = Method.POST, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
155+
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
156+
if (!string.IsNullOrEmpty(key)) {
157+
request.AddQueryParam(PARAM_CODE, key, true);
158+
}
159+
if (body != null) {
160+
request.AddBody(body);
161+
}
162+
yield return request.Request.Send();
163+
request.ParseJsonArray<T>(callback);
164+
}
165+
166+
#endregion
167+
168+
public string ApiUrl(string path = null) {
169+
string customRoute = string.IsNullOrEmpty(path) ? "" : "/" + path;
170+
return string.Format("{0}/{1}/{2}{3}", client.Url, apiPath, name, customRoute);
171+
}
172+
}
173+
}

AzureFunctionClient.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
using Azure.AppServices;
5+
using RESTClient;
6+
7+
namespace Azure.Functions {
8+
public sealed class AzureFunctionClient : ZumoClient {
9+
10+
public AzureFunctionClient(string url) : base(url) {
11+
}
12+
13+
public static AzureFunctionClient Create(string account) {
14+
string url = AppUrl(account);
15+
return new AzureFunctionClient(url);
16+
}
17+
18+
}
19+
}

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Azure Functions for Unity
2+
For Unity developers looking to use Azure Functions in their Unity game / app.
3+
4+
## External dependencies
5+
**First download the required dependencies and extract the contents into your Unity project "Assets" folder.**
6+
* [RESTClient](https://github.com/Unity3dAzure/RESTClient)
7+
* [AppServices](https://github.com/Unity3dAzure/AppServices)
8+
9+
## How to setup Azure Functions with a new Unity project
10+
1. [Download AppServices](https://github.com/Unity3dAzure/AppServices/archive/master.zip) and [REST Client](https://github.com/Unity3dAzure/RESTClient/archive/master.zip) for Unity.
11+
* Copy 'AppServices' and 'RESTClient' into project `Assets` folder.
12+
2. Create an [Azure Function App](https://portal.azure.com)
13+
* Create an HTTP Trigger function.
14+
15+
## Azure Function demos for Unity
16+
Try the [Azure Function Demo](https://github.com/Unity3dAzure/AzureFunctionsDemo) project for Unity 2017 on Mac / Windows. (The demo project has got everything already bundled in and does not require any additional assets to work. Just hook it up with your [Azure Function App](https://portal.azure.com) account and run it right inside the Unity Editor.)
17+
18+
## Minimum Requirements
19+
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.
20+
21+
## Supported platforms
22+
Intended to work on all the platforms [UnityWebRequest](https://docs.unity3d.com/Manual/UnityWebRequest.html) supports including:
23+
* Unity Editor (Mac/PC) and Standalone players
24+
* iOS
25+
* Android
26+
* Windows
27+
28+
## Beta version
29+
This is a work in progress so stuff may change frequently.
30+
31+
Questions or tweet [@deadlyfingers](https://twitter.com/deadlyfingers)

0 commit comments

Comments
 (0)