private bool LoadSaveGameData(GameSaveData saveData) { if (saveData == null) { return(false); } // create scene json files and save them in the temp directory for (int i = 0; i < saveData.scenes.Length; i++) { string path = Application.temporaryCachePath + "/HeroScenes/" + saveData.scenes[i].sceneName + ".json"; string jsonSceneText = JsonUtility.ToJson(saveData.scenes[i]); File.WriteAllText(path, jsonSceneText); } // load the global variables HeroList globals = HeroKitDatabase.globals; HeroKitCommonScene.AddVariables(globals.ints.items, saveData.globalInts); HeroKitCommonScene.AddVariables(globals.floats.items, saveData.globalFloats); HeroKitCommonScene.AddVariables(globals.bools.items, saveData.globalBools); HeroKitCommonScene.AddVariables(globals.strings.items, saveData.globalStrings); HeroKitDatabase.globals = globals; // load the scene that was last opened Vector3 defaultCoords = new Vector3(-999999, -999999, -999999); HeroKitCommonScene.LoadScene(saveData.lastScene, false, false, defaultCoords, defaultCoords); return(true); }
// Execute the action public int Execute(HeroKitObject hko) { // get values heroKitObject = hko; string saveGameName = StringFieldValue.GetValueA(heroKitObject, 0, true); GameSaveData savedGame = HeroKitCommonRuntime.GetSaveGame(saveGameName); string totalGameplay = ""; bool runThis = (savedGame != null); // save the time as a string if (runThis) { // get the time format to use string timeFormat = StringFieldValue.GetValueA(heroKitObject, 1); // get the start & current date TimeSpan timeSpan = new TimeSpan(savedGame.playtimeDays, savedGame.playtimeHours, savedGame.playtimeMinutes, savedGame.playtimeSeconds); // create the string totalGameplay = String.Format(timeFormat, (int)timeSpan.TotalDays, (int)timeSpan.TotalHours, (int)timeSpan.TotalMinutes, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds); // save the time StringFieldValue.SetValueB(heroKitObject, 2, totalGameplay); } // debug message if (heroKitObject.debugHeroObject) { string debugMessage = "Total Gameplay: " + totalGameplay + "\n" + "Save Game Name: " + saveGameName; Debug.Log(HeroKitCommonRuntime.GetActionDebugInfo(heroKitObject, debugMessage)); } return(-99); }
// Execute the action public int Execute(HeroKitObject hko) { heroKitObject = hko; // save the current scene SaveScene saveScene = new SaveScene(); saveScene.SaveSceneData(heroKitObject, false); // save scene objects saveScene.SaveSceneData(heroKitObject, true); // save persistent objects // get the path where you want to put the save game file on the player's device string saveGameName = StringFieldValue.GetValueA(heroKitObject, 0, true); string path = Application.persistentDataPath + "/HeroSaves/" + saveGameName + ".json"; // if there is an existing save file, get the seconds played GameSaveData existingGame = HeroKitCommonRuntime.GetSaveGame(saveGameName); // convert game data to json data string jsonText = GetSaveGameData(existingGame); // save the json data to player's device File.WriteAllText(path, jsonText); // debug message if (heroKitObject.debugHeroObject) { string debugMessage = "Save Game Name: " + saveGameName + "\n" + "Full Path: " + path; Debug.Log(HeroKitCommonRuntime.GetActionDebugInfo(heroKitObject, debugMessage)); } return(-99); }
// Execute the action public int Execute(HeroKitObject hko) { heroKitObject = hko; // get the path where you want to load the save game file string saveDataName = StringFieldValue.GetValueA(heroKitObject, 0, true); // get the saved game GameSaveData savedGame = HeroKitCommonRuntime.GetSaveGame(saveDataName); // load the game LoadSaveGameData(savedGame); // debug message if (heroKitObject.debugHeroObject) { string debugMessage = "Game to Load: " + saveDataName; Debug.Log(HeroKitCommonRuntime.GetActionDebugInfo(heroKitObject, debugMessage)); } return(-99); }
// Execute the action public int Execute(HeroKitObject hko) { // get values heroKitObject = hko; string saveGameName = StringFieldValue.GetValueA(heroKitObject, 0, true); GameSaveData savedGame = HeroKitCommonRuntime.GetSaveGame(saveGameName); bool runThis = (savedGame != null); // save the name of the scene if (runThis) { StringFieldValue.SetValueB(heroKitObject, 1, savedGame.lastScene); } // debug message if (heroKitObject.debugHeroObject) { string lastScene = (savedGame != null) ? savedGame.lastScene : ""; string debugMessage = "Scene: " + lastScene; Debug.Log(HeroKitCommonRuntime.GetActionDebugInfo(heroKitObject, debugMessage)); } return(-99); }
private string GetSaveGameData(GameSaveData existingGame) { // if save file existed, get date we need to transfer to new save file bool newGame = (existingGame == null); DateTime startDate = new DateTime(); DateTime endDate = new DateTime(); TimeSpan gameplay = new TimeSpan(); if (newGame) { TimeSpan timeElapsed = TimeSpan.FromSeconds(Time.realtimeSinceStartup); startDate = DateTime.Now.Subtract(timeElapsed); endDate = DateTime.Now; gameplay = TimeSpan.FromSeconds(Time.realtimeSinceStartup); } else { startDate = new DateTime(existingGame.startYear, existingGame.startMonth, existingGame.startDay, existingGame.startHour, existingGame.startMinute, existingGame.startSecond); endDate = DateTime.Now; TimeSpan oldTime = new TimeSpan(existingGame.playtimeDays, existingGame.playtimeHours, existingGame.playtimeMinutes, existingGame.playtimeSeconds); TimeSpan newTime = TimeSpan.FromSeconds(Time.realtimeSinceStartup); gameplay = oldTime + newTime; } // get the json files DirectoryInfo directoryInfo = new DirectoryInfo(Application.temporaryCachePath + "/HeroScenes/"); FileInfo[] fileInfo = directoryInfo.GetFiles("*.json"); // set up the game save data class GameSaveData saveData = new GameSaveData(); saveData.scenes = new SceneSaveData[fileInfo.Length]; // build the game save data for (int i = 0; i < fileInfo.Length; i++) { //Debug.Log(fileInfo[i].Name); string jsonSceneText = GetCachedScene(fileInfo[i].Name); saveData.scenes[i] = JsonUtility.FromJson <SceneSaveData>(jsonSceneText); } // save the current scene name saveData.lastScene = UnityEngine.SceneManagement.SceneManager.GetActiveScene().name; saveData.lastSceneID = UnityEngine.SceneManagement.SceneManager.GetActiveScene().buildIndex; // add start date saveData.startYear = startDate.Year; saveData.startMonth = startDate.Month; saveData.startDay = startDate.Day; saveData.startHour = startDate.Hour; saveData.startMinute = startDate.Minute; saveData.startSecond = startDate.Second; // add current date saveData.endYear = endDate.Year; saveData.endMonth = endDate.Month; saveData.endDay = endDate.Day; saveData.endHour = endDate.Hour; saveData.endMinute = endDate.Minute; saveData.endSecond = endDate.Second; // add gameplay saveData.playtimeDays = (int)gameplay.TotalDays; saveData.playtimeHours = (int)gameplay.TotalHours; saveData.playtimeMinutes = (int)gameplay.TotalMinutes; saveData.playtimeSeconds = gameplay.Seconds; // variables saveData.globalInts = HeroKitDatabase.GetGlobals().ints.Save(); saveData.globalFloats = HeroKitDatabase.GetGlobals().floats.Save(); saveData.globalBools = HeroKitDatabase.GetGlobals().bools.Save(); saveData.globalStrings = HeroKitDatabase.GetGlobals().strings.Save(); // convert the game save data class to json string jsonGameText = JsonUtility.ToJson(saveData); // return the json file return(jsonGameText); }