コード例 #1
0
        static IEnumerator FetchPrivacyUrlCoroutine(Action <string> success, Action <string> failure = null)
        {
            string postJson = DataPrivacyUtils.UserPostDataToJson(GetUserData());

            byte[] bytes = Encoding.UTF8.GetBytes(postJson);

            WWW www = new WWW(kTokenUrl, bytes, new Dictionary <string, string>()
            {
                { "Content-Type", "application/json" },
#if !UNITY_5_3 && !UNITY_WEBGL
                { "User-Agent", GetUserAgent() }
#endif
            });

            //request.chunkedTransfer = false; // needed for WWW in 2017.3?
            Debug.Log("Requesting data privacy URL from " + www.url);
            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";
                }
                if (failure != null)
                {
                    failure(error);
                    yield break;
                }
            }
            else
            {
                TokenData tokenData;
                try
                {
                    tokenData = DataPrivacyUtils.ParseTokenData(www.text);
                }
                catch (Exception e)
                {
                    failure(e.ToString());
                    yield break;
                }

                success(tokenData.url);
            }
        }
コード例 #2
0
        static UserPostData GetUserData()
        {
            var postData = new UserPostData
            {
                appid        = DataPrivacyUtils.GetApplicationId(),
                userid       = DataPrivacyUtils.GetUserId(),
                sessionid    = DataPrivacyUtils.GetSessionId(),
                platform     = Application.platform.ToString(),
                platformid   = (UInt32)Application.platform,
                sdk_ver      = Application.unityVersion,
                debug_device = Debug.isDebugBuild,
                deviceid     = DataPrivacyUtils.GetDeviceId(),
                plugin_ver   = kVersionString
            };

            return(postData);
        }
コード例 #3
0
        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);
            }
        }