public static void FetchOptOutStatus(Action <bool> optOutAction = null) { // Load from player prefs var localOptOutStatus = LoadFromPlayerPrefs(); SetOptOutStatus(localOptOutStatus); var userData = GetUserData(); if (string.IsNullOrEmpty(userData.appid)) { Debug.LogError("Could not find AppID for the project. Create a new Unity Project ID or link to an existing ID in the Services window."); } if (string.IsNullOrEmpty(userData.userid)) { Debug.LogError("Could not find UserID!"); } if (string.IsNullOrEmpty(userData.deviceid)) { Debug.LogError("Could not find DeviceID!"); } var query = string.Format(kOptOutUrl + "?appid={0}&userid={1}&deviceid={2}", userData.appid, userData.userid, userData.deviceid); var baseUri = new Uri(kBaseUrl); var uri = new Uri(baseUri, query); var www = UnityWebRequest.Get(uri.ToString()); #if !UNITY_WEBGL www.SetRequestHeader("User-Agent", GetUserAgent()); #endif var async = www.SendWebRequest(); async.completed += (AsyncOperation async2) => { var json = www.downloadHandler.text; if (!String.IsNullOrEmpty(www.error) || String.IsNullOrEmpty(json)) { var error = getErrorString(www); Debug.LogWarning(String.Format("Failed to load data opt-opt status from {0}: {1}", www.url, error)); if (optOutAction != null) { optOutAction(localOptOutStatus.optOut); } return; } OptOutStatus optOutStatus; try { OptOutResponse response = JsonUtility.FromJson <OptOutResponse>(json); optOutStatus = response.status; } catch (Exception e) { Debug.LogWarning(String.Format("Failed to load data opt-opt status from {0}: {1}", www.url, e.ToString())); if (optOutAction != null) { optOutAction(localOptOutStatus.optOut); } return; } SetOptOutStatus(optOutStatus); SaveToPlayerPrefs(optOutStatus); if (optOutAction != null) { optOutAction(optOutStatus.optOut); } }; }
static IEnumerator FetchOptOutStatusCoroutine(Action <bool> optOutAction = null) { // Load from player prefs var localOptOutStatus = LoadFromPlayerPrefs(); DataPrivacyUtils.SetOptOutStatus(localOptOutStatus); var userData = GetUserData(); if (string.IsNullOrEmpty(userData.appid)) { Debug.LogError("Could not find AppID for the project! Make sure you have set your Cloud Project ID in Unity Analytics!"); } if (string.IsNullOrEmpty(userData.userid)) { Debug.LogError("Could not find UserID! Make sure that you have enabled Unity Analytics for this project"); } if (string.IsNullOrEmpty(userData.deviceid)) { Debug.LogError("Could not find DeviceID!"); } var query = string.Format(kOptOutUrl + "?appid={0}&userid={1}&deviceid={2}", userData.appid, userData.userid, userData.deviceid); var baseUri = new Uri(kBaseUrl); var uri = new Uri(baseUri, query); WWW www = new WWW(uri.ToString(), null, new Dictionary <string, string>() { #if !UNITY_5_3 && !UNITY_WEBGL { "User-Agent", GetUserAgent() } #endif }); yield return(www); if (!String.IsNullOrEmpty(www.error) || String.IsNullOrEmpty(www.text)) { var error = www.error; if (String.IsNullOrEmpty(error)) { // 5.5 sometimes fails to parse an error response, and the only clue will be // in www.responseHeadersString, which isn't accessible. error = "Empty response"; } Debug.LogWarning(String.Format("Failed to load data opt-opt status from {0}: {1}", www.url, error)); if (optOutAction != null) { optOutAction(localOptOutStatus.optOut); } yield break; } OptOutStatus optOutStatus; try { OptOutResponse response = DataPrivacyUtils.ParseOptOutResponse(www.text); optOutStatus = response.status; } catch (Exception e) { Debug.LogWarning(String.Format("Failed to load data opt-opt status from {0}: {1}", www.url, e.ToString())); if (optOutAction != null) { optOutAction(localOptOutStatus.optOut); } yield break; } DataPrivacyUtils.SetOptOutStatus(optOutStatus); SaveToPlayerPrefs(optOutStatus); Debug.Log("Opt-out preferences successfully retrieved, applied and saved:\n" + DataPrivacyUtils.OptOutStatusToJson(optOutStatus)); if (optOutAction != null) { optOutAction(optOutStatus.optOut); } }