/// <summary> /// Gets a new QR Code texture to show to the user to enable them to pair your game/app/device with Allow2. /// Call this to get a new QR Code any time the user changes the name of the device. /// Note this is debounced automatically, so just keep calling it immediately (even if the user is still typing). /// </summary> /// <param name="behaviour">Provide a (any) MonoBehaviour for the sdk to use to call the platform.</param> /// <param name="deviceName">The name the user would like to use to identify this app/game/device.</param> /// <param name="callback">Callback.</param> public static void GetQR(MonoBehaviour behaviour, string deviceName, imageClosure callback) { DateTime now = DateTime.Now; Debug.Log(qrDebounce.CompareTo(now)); if ((qrCall != null) && (qrDebounce.CompareTo(now) > 0)) { Debug.Log("debounce " + qrDebounce.ToShortTimeString() + " < " + now.ToShortTimeString()); IEnumerator oldCall = qrCall; qrCall = null; behaviour.StopCoroutine(oldCall); } qrDebounce = now.AddMilliseconds(QRDebounceDelay); qrCall = _GetQR(deviceName, callback); behaviour.StartCoroutine(qrCall); }
static IEnumerator _GetQR(string deviceName, imageClosure callback) { yield return(new WaitForSeconds(QRDebounceDelay / 1000)); string qrURL = ApiUrl + "/genqr/" + UnityWebRequest.EscapeURL(_deviceToken) + "/" + UnityWebRequest.EscapeURL(uuid) + "/" + UnityWebRequest.EscapeURL(deviceName); UnityWebRequest www = UnityWebRequestTexture.GetTexture(qrURL); yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log("QR LOAD ERROR: " + www.error); Texture errorImage = Resources.Load("Allow2/QRError") as Texture2D; callback(www.error, null); yield break; } Texture2D qrCode = DownloadHandlerTexture.GetContent(www); callback(null, qrCode); }