コード例 #1
0
        protected virtual void FinishedCreatingFromURL(WWWUtility utility)
        {
            var www = utility.www;

            isCreating = false;
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(gameObject);
#endif

            // handle error
            if (www.error != null)
            {
                if (www.error.Contains("409"))
                {
                    // sheet has already been created, try to load it
                    LoadFromURL();
                }
                else
                {
                    Debug.LogError("[Unity Cloud Data] Error creating CloudDataSheet: " + www.error);
                }
                return;
            }

            // mark as created
            Debug.Log(string.Format("[Unity Cloud Data] Successfully created sheet in Unity Cloud Data at path: {0}", path));
            hasBeenCreatedInCloud = true;

            // refresh it!
            RefreshCache();

#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(gameObject);
#endif
        }
コード例 #2
0
        // Creates the sheet data with the current state of all CloudDataFields
        protected virtual void CreateFromURL()
        {
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                Debug.LogWarning("[Unity Cloud Data] Internet connection not detected; skipping create for '" + this.GetType().Name + "'");
                return;
            }

            // fecth the newEntries
            string rawData = "{" +
                             "\"name\": \"" + path + "\"," +
                             "\"description\": \"created from editor\"," +
                             "\"values\": " + newSheetEntries +
                             "}";

            byte[] byteArray = Encoding.UTF8.GetBytes(rawData);
            var    headers   = new Dictionary <string, string>();

            headers["Content-Type"] = "application/json";
            Debug.Log(string.Format("[Unity Cloud Data] Creating sheet in Unity Cloud Data at path: {0}", path));

            isCreating = true;
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(gameObject);
#endif
            WWWUtility.StartRequest(sheetWriteUrl, byteArray, headers, FinishedCreatingFromURL);
        }
コード例 #3
0
        protected virtual void FinishedCreatingFromURL(WWWUtility utility)
        {
            var request = utility.www;
            var item    = (DownloadItem)utility.metaData;

            HandleResponseForDownloadItem(request, item);
        }
コード例 #4
0
        void FinishedWritingEntry(WWWUtility utility)
        {
            var response = utility.www;

            if (response.error != null)
            {
                Debug.LogWarning(string.Format("[Unity Cloud Data] Error writing key '{0}' to sheet at path '{1}': {2}", m_CurrentWriteEntry.sheetKey, path, response.error));
            }
            else
            {
                Debug.Log(string.Format("[Unity Cloud Data] Successfully wrote key '{0}' to sheet at path '{1}'", m_CurrentWriteEntry.sheetKey, path));
            }

            WriteQueueEntries();
        }
コード例 #5
0
        protected virtual void LoadSheetToken()
        {
            Debug.Log("[Unity Cloud Data] Requesting sheet token from Unity Cloud Data for sheet: '" + path + "'");
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                Debug.LogWarning("[Unity Cloud Data] Internet connection not detected; skipping sheet token request for '" + path + "'");
                return;
            }

            isRefreshing = true;
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(gameObject);
#endif
            WWWUtility.StartRequest(tokenReadUrl, FinishedLoadingSheetToken);
        }
コード例 #6
0
        bool DoRefresh()
        {
            SaveSettings();

            // Find all tweak table prefabs
            List <Component> dataSheets = UnityCloudData.EditorTools.GetAllComponentsInPrefabs <CloudDataSheet>();

            string baseAssetPath = Application.dataPath + "/" + assetFolder;

            if (false == Directory.Exists(baseAssetPath))
            {
                Directory.CreateDirectory(baseAssetPath);
            }

            // Fancy LINQ syntax to build a list of DownloadItem structs
            IEnumerable <DownloadItem> urls =
                from component in dataSheets
                select new DownloadItem
            {
                Url       = ((CloudDataSheet)component).sheetReadUrl,
                AssetName = ((CloudDataSheet)component).path,
                AssetPath = baseAssetPath + "/" + ((CloudDataSheet)component).path.Replace("/", "-") + ".txt"
            };

            bool refreshResult = true;

            // Save each URL to asset
            foreach (DownloadItem item in urls)
            {
                if (m_sync)
                {
                    WWW request = new WWW(item.Url);
                    while (false == request.isDone)
                    {
                        System.Threading.Thread.Sleep(100);
                    }

                    // Make sure we return false if there are any failures
                    refreshResult &= HandleResponseForDownloadItem(request, item);
                }
                else
                {
                    WWWUtility.StartRequest(item.Url, FinishedCreatingFromURL, item);
                }
            }

            return(refreshResult);
        }
コード例 #7
0
        protected virtual void FinishedLoadingSheetToken(WWWUtility utility)
        {
            isRefreshing = false;
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(gameObject);
#endif
            var www = utility.www;

            // handle loading error
            if (www.error != null)
            {
                if (www.error.Contains("404"))
                {
                    hasBeenCreatedInCloud = false;
                    Debug.LogWarning(string.Format("[Unity Cloud Data] sheet '{0}' not found in Unity Cloud Data. Need to create it?", path));
                }
                else
                {
                    Debug.LogError(string.Format("[Unity Cloud Data] Error getting token for sheet '{0}': {1}", path, www.error));
                }
                return;
            }

            string text = www.text;

            var data = MiniJSON.Json.Deserialize(text) as List <object>;
            if (data != null && data.Count > 0)
            {
                var dict = data[0] as Dictionary <string, object>;
                sheetToken = (dict != null) ? dict["token"] as string : null;
            }

            // load data using new sheet token
            if (!String.IsNullOrEmpty(sheetToken))
            {
                base.LoadFromURL();
            }
            else
            {
                Debug.LogError(string.Format("[Unity Cloud Data] Error parsing sheet token for sheet'{0}':  raw data: {1}", path, text));
            }
        }
コード例 #8
0
        void WriteQueueEntries()
        {
            if (m_WriteQueue.Count > 0)
            {
                m_CurrentWriteEntry = m_WriteQueue.Dequeue();

                // Create request headers
                var headers = new Dictionary <string, string>();
                headers.Add("Content-Type", "application/json");
                Debug.Log(string.Format("[Unity Cloud Data] writing key '{0}' for sheet at path '{1}'", m_CurrentWriteEntry.sheetKey, path));
                byte[] body = Encoding.UTF8.GetBytes(m_CurrentWriteEntry.putEntry);
                WWWUtility.StartRequest(m_CurrentWriteEntry.putUrl, body, headers, FinishedWritingEntry);
            }
            else
            {
                Debug.Log(string.Format("[Unity Cloud Data] Done saving sheet at path '{0}'!", path));
                // wait until after the refresh completes to trigger the callback
                onRefreshCacheComplete += PostWriteRefreshFinished;
                RefreshCache();
            }
        }
コード例 #9
0
        // Updates the sheet data with data stored on a remote HTTP server
        protected virtual void LoadFromURL()
        {
            if (string.IsNullOrEmpty(path))
            {
                Debug.LogWarning("[Unity Cloud Data] Not loading sheet - no path set!");
                return;
            }

            Debug.Log("[Unity Cloud Data] Requesting sheet from Unity Cloud Data at path: '" + path + "'");
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                Debug.LogWarning("[Unity Cloud Data] Internet connection not detected; skipping update for '" + path + "'");
                return;
            }

            isRefreshing = true;
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(gameObject);
#endif
            WWWUtility.StartRequest(sheetReadUrl, FinishedLoadingFromURL);
        }
コード例 #10
0
        protected virtual void FinishedLoadingFromURL(WWWUtility utility)
        {
            var www = utility.www;

            isRefreshing = false;
#if UNITY_EDITOR
            UnityEditor.EditorUtility.SetDirty(gameObject);
#endif

            // handle loading error
            if (www.error != null)
            {
                if (www.error.Contains("404"))
                {
                    hasBeenCreatedInCloud = false;
                    Debug.LogWarning(string.Format("[Unity Cloud Data] CloudDataSheet '{0}' not found. Need to create it?", path));
                }
                else
                {
                    Debug.LogError(string.Format("[Unity Cloud Data] Error downloading CloudDataSheet '{0}': {1}", path, www.error));
                }
                return;
            }

            // mark as loaded
            hasBeenCreatedInCloud = true;
            lastRefreshTime       = DateTime.Now;

            // validate and parse
            string text = www.text;
            if (ValidateSheetData(text))
            {
                // force the last refresh time into the JSON (so it will persist)
                string refreshString = "\"__lastRefreshTime\": \"" + lastRefreshTime.ToString("s") + "\"";
                if (text != "{}")
                {
                    refreshString += ",";
                }
                text = text.Insert(1, refreshString);

                // parse the sheet and update the local cache
                Debug.Log("[Unity Cloud Data] Successfully updated '" + path + "' from network");
                ParseSheetData(text);
                localSheetCache        = text;
                isLoaded               = true;
                isRefreshedFromNetwork = true;
            }
            else
            {
                Debug.LogError("[Unity Cloud Data] Error parsing JSON in '" + path + "'. Reverting to local cache.");
            }

            if (onRefreshCacheComplete != null)
            {
                onRefreshCacheComplete(this);
            }

            if (globalRefreshCacheComplete != null)
            {
                globalRefreshCacheComplete(this);
            }

            // now that we've refreshed the data, optionally save all new fields to cloud
#if UNITY_EDITOR
            if (CloudDataManager.instance.autoSaveNewFieldsToCloudOnPlay && Application.isPlaying)
            {
                Save(null);
            }
#endif
        }