Exemplo n.º 1
0
        /// <summary>
        /// Save data to a text file or to the clipboard in the WebPlayer
        /// </summary>
        public static string SaveByFilePath(string p_filePath, byte[] p_data, byte[] p_meta)
        {
            // YOU SHOULD ZIP THE LEVEL DATA! YOU CAN SAVE UP TO 95% DATA VOLUME, AVARAGE SEEMS TO BE AROUND 75% REDUCTION
            string dataAsString     = System.Convert.ToBase64String(p_data) + "#" + System.Convert.ToBase64String(p_meta);
            string levelSizeMessage = "Level size: '" + ((float)(p_data.Length + p_meta.Length) / 1048576f).ToString("0.00") + "' MB\n";

            UtilityPlatformIO.SaveToFile(p_filePath, dataAsString);
            return("Level saved to '" + p_filePath + "'.\n" +
                   levelSizeMessage +
                   "The load button will load from this file.");
        }
Exemplo n.º 2
0
        public IEnumerator GetLevelListAsync(System.Action <LevelFile[]> p_onLoaded)
        {
            // show loading popup
            if (uMyGUI_PopupManager.Instance != null)
            {
                uMyGUI_PopupManager.Instance.ShowPopup(LE_FileSelectionHelpers.POPUP_LOADING);
            }

            // load file with the level file names
            WWW www = new WWW(UtilityPlatformIO.FixFilePath(Path.Combine(Application.persistentDataPath, LEVEL_FILE_NAMES_PATH)));

            yield return(www);

            string savedLevelFileNamesText;

            if (string.IsNullOrEmpty(www.error))
            {
                savedLevelFileNamesText = www.text;
            }
            else
            {
                savedLevelFileNamesText = "";
            }

            // hide loading popup
            if (uMyGUI_PopupManager.Instance != null)
            {
                uMyGUI_PopupManager.Instance.HidePopup(LE_FileSelectionHelpers.POPUP_LOADING);
            }

            // callback with the loaded level names
            string[] savedLevelFileNames = savedLevelFileNamesText.Split(new string[] { "\r\n", "\n" }, System.StringSplitOptions.RemoveEmptyEntries);
            if (p_onLoaded != null)
            {
                LevelFile[] levelFiles = new LevelFile[savedLevelFileNames.Length];
                for (int i = 0; i < levelFiles.Length; i++)
                {
                    levelFiles[i] = new LevelFile()
                    {
                        Name     = savedLevelFileNames[i],
                        PathData = Path.Combine(Application.persistentDataPath, savedLevelFileNames[i] + ".txt"),
                        PathIcon = Path.Combine(Application.persistentDataPath, savedLevelFileNames[i] + ".png")
                    };
                }
                p_onLoaded(levelFiles);
            }
        }
Exemplo n.º 3
0
 public IEnumerator Delete(LevelFile p_levelFile, System.Action <bool> p_onResult)
 {
     return(GetLevelListAsync((LevelFile[] p_levels) =>
     {
         // show confirm popup
         ((uMyGUI_PopupText)uMyGUI_PopupManager.Instance.ShowPopup(LE_FileSelectionHelpers.POPUP_TEXT)).SetText("Delete Level", "Do you really want to delete '" + p_levelFile.Name + "'?")
         .ShowButton("no", () =>
         {
             if (p_onResult != null)
             {
                 p_onResult(false);
             }
         })
         .ShowButton("yes", () =>
         {
             // remove level file name from the list
             List <string> updatedLevelNames = new List <string>();
             for (int i = 0; i < p_levels.Length; i++)
             {
                 if (p_levels[i].Name != p_levelFile.Name)
                 {
                     updatedLevelNames.Add(p_levels[i].Name);
                 }
             }
             string levelFileNamesText = "";
             for (int i = 0; i < updatedLevelNames.Count; i++)
             {
                 levelFileNamesText += updatedLevelNames[i] + "\n";
             }
             string levelListFilePath = Path.Combine(Application.persistentDataPath, LEVEL_FILE_NAMES_PATH);
             UtilityPlatformIO.SaveToFile(levelListFilePath, levelFileNamesText);
             // delete level files
             if (!string.IsNullOrEmpty(p_levelFile.PathData))
             {
                 File.Delete(p_levelFile.PathData);
             }
             if (!string.IsNullOrEmpty(p_levelFile.PathIcon))
             {
                 File.Delete(p_levelFile.PathIcon);
             }
             if (p_onResult != null)
             {
                 p_onResult(true);
             }
         });
     }));
 }
Exemplo n.º 4
0
        /// <summary>
        /// Load data from a text file
        /// </summary>
        public static IEnumerator LoadRoutineByFilePath(string p_filePath, System.Action <byte[][]> p_onLoaded)
        {
            // load level data with the WWW class (WWW is supported on all platforms for disk IO)
            WWW www = new WWW(UtilityPlatformIO.FixFilePath(p_filePath));

            yield return(www);

            string savedLevel;

            if (string.IsNullOrEmpty(www.error))
            {
                savedLevel = www.text;
            }
            else
            {
                Debug.LogError("ExampleGame_LoadSave: LoadRoutine: could load file '" + www.url + "'! Error:\n" + www.error);
                savedLevel = null;
            }

            LoadFromStr(savedLevel, p_onLoaded);
        }
Exemplo n.º 5
0
        public void SaveFile(string p_levelName, byte[] p_levelData, byte[] p_levelMeta, int p_removedDuplicatesCount, LevelFile[] p_levelFiles, System.Action <string> p_onSuccess, System.Action p_onFail)
        {
            // check if this is a new level file name
            bool isExistingLevel = false;

            string[] levelNames = LevelFile.GetLevelNames(p_levelFiles);
            for (int i = 0; i < levelNames.Length; i++)
            {
                if (levelNames[i] == p_levelName)
                {
                    isExistingLevel = true;
                    break;
                }
            }

            string levelDataFilePath = Path.Combine(Application.persistentDataPath, p_levelName + ".txt");
            string levelIconFilePath = Path.Combine(Application.persistentDataPath, p_levelName + ".png");

            if (!isExistingLevel)
            {
                // add new level file name to the list
                string[] updatedLevelNames = new string[levelNames.Length + 1];
                System.Array.Copy(levelNames, updatedLevelNames, levelNames.Length);
                updatedLevelNames[updatedLevelNames.Length - 1] = p_levelName;
                System.Array.Sort(updatedLevelNames);
                string levelFileNamesText = "";
                for (int i = 0; i < updatedLevelNames.Length; i++)
                {
                    levelFileNamesText += updatedLevelNames[i] + "\n";
                }
                string levelListFilePath = Path.Combine(Application.persistentDataPath, LEVEL_FILE_NAMES_PATH);
                UtilityPlatformIO.SaveToFile(levelListFilePath, levelFileNamesText);
                // save level
                LE_FileSelectionHelpers.SaveLevel(levelDataFilePath, levelIconFilePath, p_levelData, p_levelMeta, p_removedDuplicatesCount);
                if (p_onSuccess != null)
                {
                    p_onSuccess(levelDataFilePath);
                }
            }
            else
            {
                // show confirm popup
                ((uMyGUI_PopupText)uMyGUI_PopupManager.Instance.ShowPopup(LE_FileSelectionHelpers.POPUP_TEXT)).SetText("Save Level", "The level '" + p_levelName + "' already exists, do you want to overwrite it?")
                .ShowButton("no", () =>
                {
                    // go back
                    if (p_onFail != null)
                    {
                        p_onFail();
                    }
                })
                .ShowButton("yes", () =>
                {
                    // save level
                    LE_FileSelectionHelpers.SaveLevel(levelDataFilePath, levelIconFilePath, p_levelData, p_levelMeta, p_removedDuplicatesCount);
                    if (p_onSuccess != null)
                    {
                        p_onSuccess(levelDataFilePath);
                    }
                });
            }
        }
Exemplo n.º 6
0
        private IEnumerator LoadLevelIcon(string p_iconPath)
        {
            if (m_iconImage != null)
            {
                float startTime = Time.realtimeSinceStartup;
                // wait for icon to be fully loaded
                WWW iconWWW = null;
                WaitForEndOfFrame frameEnd = new WaitForEndOfFrame();
                while (iconWWW == null || !iconWWW.isDone)
                {
                    // start loading icon (after a little delay (wait for the popup animation to finish))
                    if (iconWWW == null && Time.realtimeSinceStartup - startTime >= 0.65f && s_streamCount < MAX_STREAMS)
                    {
                        iconWWW = new WWW(UtilityPlatformIO.FixFilePath(p_iconPath));
                        s_streamCount++;
                        m_isIconLoading = true;
                    }
                    // rotate loading indicator
                    if (m_iconImage != null)
                    {
                        m_iconImage.transform.eulerAngles = 140f * Vector3.back * Time.realtimeSinceStartup;
                    }
                    if (m_iconOverlay != null)
                    {
                        m_iconOverlay.transform.rotation = Quaternion.identity;
                    }
                    yield return(frameEnd);
                }

                m_isIconLoading = false;
                s_streamCount--;

                // show icon
                if (string.IsNullOrEmpty(iconWWW.error) && iconWWW.texture != null)
                {
                    if (m_iconImage != null)
                    {
                        // reset icon rotation
                        m_iconImage.transform.rotation = Quaternion.identity;
                        if (m_iconOverlay != null)
                        {
                            m_iconOverlay.transform.rotation = Quaternion.identity;
                        }
                        // set texture
                        if (iconWWW.texture.width > iconWWW.texture.height)
                        {
                            float aspectChangeFactor   = (float)iconWWW.texture.width / (float)iconWWW.texture.height;
                            float aspectCorrectedWidth = m_iconImage.rectTransform.rect.width;
                            aspectCorrectedWidth                    *= aspectChangeFactor;
                            m_iconImage.rectTransform.pivot          = new Vector2(0f, m_iconImage.rectTransform.pivot.y);
                            m_iconImage.rectTransform.localPosition -= Vector3.right * m_iconImage.rectTransform.rect.width * 0.5f;
                            m_iconImage.rectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, aspectCorrectedWidth);
                        }
                        m_iconImage.texture = iconWWW.texture;
                    }
                    m_isIconLoaded = true;
                }
                else
                {
                    // hide icon on error (not found)
                    if (m_iconImage != null)
                    {
                        m_iconImage.enabled = false;
                    }
                }
            }
            else
            {
                yield return(null);
            }
        }