Пример #1
0
        /// <summary>
        /// Load game to savepoint (checkpoint reached in scene). This does not load to scene, use 'Continue' for that.
        /// </summary>
        /// <param name="loadSavePointState">Only set true if loading using 'continue' button</param>
        public void LoadGame(bool loadSavePointState)
        {
            if (loadSavePointState)
            {
                // SavePoint State load:
                SavePoint[] tempSavePoints = GameObject.FindObjectsOfType <SavePoint>();
                string      loadString     = PlayerPrefs.GetString("savePointStateSave");

                if (tempSavePoints != null)
                {
                    string[] dataString = loadString.Split(new[] { SAVE_SEPERATOR }, System.StringSplitOptions.None);

                    for (int i = 1; i < dataString.Length; i++) // Must start at 1
                    {
                        SavePointContainer tempSavePointContainer =
                            JsonUtility.FromJson <SavePointContainer>(dataString[i]);

                        for (int j = 0; j < tempSavePoints.Length; j++)
                        {
                            if (tempSavePointContainer.thisID == tempSavePoints[j].thisID)
                            {
                                tempSavePoints[j].savePointUsed = tempSavePointContainer.savePointUsed;
                            }
                        }
                    }
                }

                LoadGame();
            }
            else
            {
                LoadGame();
            }
        }
Пример #2
0
        public void SaveGame()
        {
            string saveString = "";

            //// Object saving: ////
            Saveable[] saveables = Resources.FindObjectsOfTypeAll <Saveable>();

            foreach (var saveable in saveables)
            {
                saveable.ConstructContainer();

                saveable.GetContainer().Name = saveable.gameObject.name;

                if (saveable.savePosition)
                {
                    saveable.GetContainer().Position = saveable.transform.position;
                }

                if (saveable.saveRotation)
                {
                    saveable.GetContainer().Rotation = saveable.transform.rotation;
                }

                saveable.GetContainer().IsActive = saveable.gameObject.activeSelf;

                saveString += saveable.ThisContainerToString() + SAVE_SEPERATOR;
            }

            PlayerPrefs.SetString("objectSave", saveString);


            //// SavePoint State: ////
            SavePoint[] tempSavePoints = GameObject.FindObjectsOfType <SavePoint>();
            saveString = "";

            if (tempSavePoints != null)
            {
                List <SavePointContainer> tempSavePointContainerList = new List <SavePointContainer>();

                for (int i = 0; i < tempSavePoints.Length; i++)
                {
                    SavePointContainer tempSavePointContainer = new SavePointContainer();

                    tempSavePointContainer.savePointUsed = tempSavePoints[i].savePointUsed;
                    tempSavePointContainer.thisID        = tempSavePoints[i].thisID;

                    tempSavePointContainerList.Add(tempSavePointContainer);

                    saveString += SAVE_SEPERATOR + JsonUtility.ToJson(tempSavePointContainerList[i]);
                }

                PlayerPrefs.SetString("savePointStateSave", saveString);
            }

            //// Scene save: ////
            PlayerPrefs.SetInt("currentScene", SceneManager.GetActiveScene().buildIndex);

            PlayerPrefs.SetInt("previousGame", 1);

            if (Application.isEditor)
            {
                Debug.Log("Saved the game!");
            }
        }