From 3ca512632815ebe20e236db14d04f144cdc3c705 Mon Sep 17 00:00:00 2001 From: Suvitruf Date: Wed, 16 Nov 2016 11:15:26 +0900 Subject: [PATCH 1/2] Fixed error "AddComponent with MonoBehaviour is not allowed. Create a class that derives from MonoBehaviour and add it instead." --- source/Assets/Scripts/InfinarioSDK/FakeObject.cs | 6 ++++++ source/Assets/Scripts/InfinarioSDK/Sender.cs | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 source/Assets/Scripts/InfinarioSDK/FakeObject.cs diff --git a/source/Assets/Scripts/InfinarioSDK/FakeObject.cs b/source/Assets/Scripts/InfinarioSDK/FakeObject.cs new file mode 100644 index 0000000..527499b --- /dev/null +++ b/source/Assets/Scripts/InfinarioSDK/FakeObject.cs @@ -0,0 +1,6 @@ +using UnityEngine; + +namespace Infinario.Sender { + public class FakeObject : MonoBehaviour{ + } +} diff --git a/source/Assets/Scripts/InfinarioSDK/Sender.cs b/source/Assets/Scripts/InfinarioSDK/Sender.cs index da26433..2ce1112 100644 --- a/source/Assets/Scripts/InfinarioSDK/Sender.cs +++ b/source/Assets/Scripts/InfinarioSDK/Sender.cs @@ -22,7 +22,7 @@ protected void StartCoroutine(IEnumerator coroutine) { var go = new GameObject("Infinario Coroutines"); UnityEngine.Object.DontDestroyOnLoad(go); - _coroutineObject = go.AddComponent(); + _coroutineObject = go.AddComponent(); } _coroutineObject.StartCoroutine (coroutine); } From 77a71a99f2e06cfc5f04ecee996278df4debf8b4 Mon Sep 17 00:00:00 2001 From: Suvitruf Date: Sat, 10 Dec 2016 19:21:19 +0900 Subject: [PATCH 2/2] Better to use UnityWebRequest for Unity > 5.4 --- source/Assets/Scripts/InfinarioSDK/Sender.cs | 39 ++++++++++++++------ 1 file changed, 28 insertions(+), 11 deletions(-) diff --git a/source/Assets/Scripts/InfinarioSDK/Sender.cs b/source/Assets/Scripts/InfinarioSDK/Sender.cs index 2ce1112..ad438bb 100644 --- a/source/Assets/Scripts/InfinarioSDK/Sender.cs +++ b/source/Assets/Scripts/InfinarioSDK/Sender.cs @@ -4,7 +4,9 @@ using Infinario.Storage; using Infinario.MiniJSON; using System.Text; - +#if UNITY_5_4_OR_NEWER +using UnityEngine.Networking; +#endif namespace Infinario.Sender { class Sender @@ -54,21 +56,36 @@ private IEnumerator APISendLoop(string target, PersistentBulkCommandQueue comman var httpBody = Json.Serialize(new Dictionary {{"commands", commands}}); byte[] httpBodyBytes = Encoding.UTF8.GetBytes(httpBody); Dictionary httpHeaders = new Dictionary{ {"Content-type", "application/json"} }; - - // 2. Send the bulk API request - WWW req = new WWW(httpTarget, httpBodyBytes, httpHeaders); //TODO: we could add a timeout functionality - yield return req; - - // 3A: Check response for errors - if (!string.IsNullOrEmpty(req.error)) + + // 2. Send the bulk API request +#if UNITY_5_4_OR_NEWER + UnityWebRequest req = new UnityWebRequest(httpTarget, "POST"); //TODO: we could add a timeout functionality + + foreach (var header in httpHeaders) + req.SetRequestHeader(header.Key, header.Value); + + req.uploadHandler = new UploadHandlerRaw(httpBodyBytes); + req.downloadHandler = new DownloadHandlerBuffer(); + + yield return req.Send(); +#else + WWW req = new WWW(httpTarget, httpBodyBytes, httpHeaders); //TODO: we could add a timeout functionality + yield return req; +#endif + // 3A: Check response for errors + if (!string.IsNullOrEmpty(req.error)) { consecutiveFailedRequests++; } else { - // 3B. Parse the API response - var responseBody = req.text; - Dictionary apiResponse = (Dictionary) Json.Deserialize(responseBody); + // 3B. Parse the API response +#if UNITY_5_4_OR_NEWER + var responseBody = req.downloadHandler.text; +#else + var responseBody = req.text; +#endif + Dictionary apiResponse = (Dictionary) Json.Deserialize(responseBody); bool success = (bool) apiResponse["success"]; if(success) {