/// <summary> /// 读取内存中的存档值 /// </summary> /// <typeparam name="T1">赋值操作对象类型。</typeparam> /// <typeparam name="T2">你希望保存组件对象类型 /// 请务必保证该类型中的泛型,是你希望保存组件对象类型</typeparam> /// <param name="gameObject">gameObject</param> /// <param name="sceneName">场景名,不填为当前场景</param> /// <returns></returns> public static bool LoadComponent <T1, T2>(this GameObject gameObject, string sceneName = "") where T1 : ISave <T2> where T2 : UnityEngine.Component { if (gameObject.CheckEmpty()) { return(false); } T2 component = gameObject.GetComponent <T2>(); if (component.CheckEmpty()) { return(false); } SaveObject saveObject = GameSaveUtility.GetSaveObjectData(gameObject.name, sceneName); SetValue setValue = GameSaveUtility.GetComponentSetValue <T1, T2>(gameObject, saveObject); if (null == setValue) { return(false); } bool result = gameSaveSystem.Load <T1, T2>(component, setValue); return(result); }
public void PlayButton() { GameSaveUtility.gameTitle = titleInput.text; GameSaveUtility.NewSaveName(); GameSaveUtility.Save(); GameSaveUtility.Load(); SceneManager.LoadScene("Tactics"); }
public void SaveComponent <T1, T2>(string name, string value, string sceneName) where T1 : ISave <T2> { if (string.IsNullOrEmpty(name)) { return; } string ISaveName = typeof(T1).Name; string componentName = typeof(T2).Name; SceneData sceneData = GameSaveUtility.GetSceneData(sceneName); if (null == sceneData) { GameSaveUtility.AddSceneData(sceneName); } SaveObject saveObject = GameSaveUtility.GetSaveObjectData(name, sceneName); if (null == saveObject) { saveObject = GameSaveUtility.AddSaveObjectData(name, sceneName); } if (saveObject.SetValues.CheckEmpty()) { saveObject.SetValues = new Dictionary <string, Dictionary <string, SetValue> >(); } Dictionary <string, SetValue> setValues; if (saveObject.SetValues.ContainsKey(componentName)) { setValues = saveObject.SetValues[componentName]; } else { setValues = new Dictionary <string, SetValue>(); saveObject.SetValues.Add(componentName, setValues); } SetValue setValue = new SetValue { FunOpera = ISaveName, Value = value }; if (setValues.ContainsKey(ISaveName)) { setValues[ISaveName] = setValue; } else { setValues.Add(ISaveName, setValue); } }
/// <summary> /// Posts the player name, player university team to the corresponding account using account ID /// </summary> /// <returns></returns> IEnumerator UploadPlayerRoutine() { WWWForm form = new WWWForm(); form.AddField("player_name", GameSaveUtility.GetPlayerName()); form.AddField("player_team", GameSaveUtility.GetUniversity()); form.AddField("player_ID", GameSaveUtility.GetID()); using (UnityWebRequest www = UnityWebRequest.Post(path + "addplayer.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); #endif if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("userAdded")) { if (www.GetResponseHeaders()["userAdded"] == "true") { Debug.Log("User added succesful"); } else { Debug.Log("User added failed"); } } else if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("userUpdated")) { if (www.GetResponseHeaders()["userUpdated"] == "true") { Debug.Log("User updated succesful"); } else { Debug.Log("User updated failed"); } } } } }
/// <summary> /// Posts the level objective name, chapter, mode, difficulty, score and star to the PlayerLevels table where it matches the player ID /// Called at the end of every level - updated simulataneously with the local storage of the level scores /// </summary> /// <returns></returns> IEnumerator StoreLevelDataRoutine(string objective, int score, int star) { WWWForm form = new WWWForm(); form.AddField("player_ID", GameSaveUtility.GetID()); form.AddField("level_name", objective); form.AddField("level_chapter", ObjectiveUtility.CurrentChapter.ToString()); form.AddField("level_mode", ObjectiveUtility.CurrentGameMode.ToString()); form.AddField("level_difficulty", ObjectiveUtility.CurrentDifficulty.ToString()); form.AddField("level_score", score); form.AddField("level_star", star); using (UnityWebRequest www = UnityWebRequest.Post(path + "storelevels.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); Debug.Log(www.downloadHandler.text); #endif if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("levelUpdated")) { if (www.GetResponseHeaders()["levelUpdated"] == "true") { Debug.Log("Level score updating succesfull"); // Print Body #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } else { Debug.Log("Score saving failed"); } } } } }
/// <summary> /// Updates the score the individual level by POSTing the player ID, level objective, level chapter, level difficulty, level mode to identify corect row /// POSTs and score to store. /// </summary> /// <returns></returns> IEnumerator UpdateLevelDataRoutine(bool star) { WWWForm form = new WWWForm(); form.AddField("player_id", GameSaveUtility.GetID()); form.AddField("level_objective", ObjectiveUtility.CurrentObjective); form.AddField("level_chapter", ObjectiveUtility.CurrentChapter.ToString()); form.AddField("level_difficulty", ObjectiveUtility.CurrentDifficulty.ToString()); form.AddField("level_mode", ObjectiveUtility.CurrentGameMode.ToString()); form.AddField("level_score", ObjectiveUtility.Score); if (star) { form.AddField("level_star", ObjectiveUtility.EarnedStars); // if a star is earned, POST the star earned } using (UnityWebRequest www = UnityWebRequest.Post(path + "storelevels.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); #endif if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("storeLevel")) { if (www.GetResponseHeaders()["storeLevel"] == "true") { Debug.Log("Level score successfully updated"); } else { Debug.Log("Level score failed up upate"); } } } } }
/// <summary> /// Posts the earned stars to the player university using the account ID /// </summary> /// <returns></returns> IEnumerator UpdateLeaderboardRoutine() { WWWForm form = new WWWForm(); form.AddField("player_id", GameSaveUtility.GetID()); form.AddField("player_team", GameSaveUtility.GetUniversity()); form.AddField("earnedStars", ObjectiveUtility.EarnedStars); // php will add the earned stars to the total stars of the player using (UnityWebRequest www = UnityWebRequest.Post(path + "updateleaderboard.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); #endif if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("leaderboardUpdated")) { if (www.GetResponseHeaders()["leaderboardUpdated"] == "true") { Debug.Log("Team score updating succesfully"); // Print Body #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } else { Debug.Log("Score saving failed"); } } } } }
public void SaveGobal <T1, T2>(string componentName, string value) where T1 : ISave <T2> { if (string.IsNullOrEmpty(componentName)) { return; } string ISaveName = typeof(T1).Name; GobalData gobalData = GameSaveUtility.GetGobalObjectData(componentName); if (null == gobalData) { gobalData = GameSaveUtility.AddGobalObjectData(componentName); } if (gobalData.SetValues.CheckEmpty()) { gobalData.SetValues = new Dictionary <string, Dictionary <string, SetValue> >(); } Dictionary <string, SetValue> setValues; if (gobalData.SetValues.ContainsKey(componentName)) { setValues = gobalData.SetValues[componentName]; } else { setValues = new Dictionary <string, SetValue>(); gobalData.SetValues.Add(componentName, setValues); } SetValue setValue = new SetValue { FunOpera = ISaveName, Value = value }; if (setValues.ContainsKey(ISaveName)) { setValues[ISaveName] = setValue; } else { setValues.Add(ISaveName, setValue); } }
private void Update() { if (Input.GetKeyDown(KeyCode.A)) { transform.SaveComponent <SavePos, Transform>(); gobalTest.SaveGobal <GobalTestDebug, GobalTest>(); GameSaveUtility.SaveGame(); } if (Input.GetKeyDown(KeyCode.S)) { gameObject.LoadComponent <SavePos, Transform>(); gobalTest.LoadGobal <GobalTestDebug, GobalTest>(); } if (Input.GetKeyDown(KeyCode.D)) { gobalTest.test = "567"; } }
/// <summary> /// 读取内存中的全局数据 /// </summary> /// <typeparam name="T1">赋值操作对象类型。</typeparam> /// <typeparam name="T2">你希望保存组件对象类型 /// 请务必保证该类型中的泛型,是你希望保存组件对象类型</typeparam> /// <param name="component">全局对象</param> /// <returns></returns> public static bool LoadGobal <T1, T2>(this T2 component) where T1 : ISave <T2> { if (component.CheckEmpty()) { return(false); } string componentName = typeof(T2).Name; GobalData gobalData = GameSaveUtility.GetGobalObjectData(componentName); SetValue setValue = GameSaveUtility.GetGobalSetValue <T1, T2>(gobalData); if (null == setValue) { return(false); } bool result = gameSaveSystem.Load <T1, T2>(component, setValue); return(result); }
public void SetPlayerInfo() { if (progress1 > 0) { GameSaveUtility.SetChapProgress(1); } if (progress2 > 0) { GameSaveUtility.SetChapProgress(2); } if (progress3 > 0) { GameSaveUtility.SetChapProgress(3); } GameSaveUtility.SavePlayerName(playerName); GameSaveUtility.SaveUniversity(playerTeam); GameSaveUtility.AddTotalBalance(playerMoney); GameSaveUtility.SetTotalStar(playerStars); PlayerPrefs.Save(); }
/// <summary> /// Tests the connection. /// </summary> /// <returns>The connection.</returns> /// <param name="action">Action.</param> IEnumerator TestConnection() { WWWForm form = new WWWForm(); using (UnityWebRequest www = UnityWebRequest.Post(path + "connectioncheck.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); Debug.Log("No connection. No progress will be saved."); if (GameSaveUtility.OfflineMode != 2) { GameSaveUtility.SetConnectionMode(1); } } else { Debug.Log("Connection established."); GameSaveUtility.SetConnectionMode(0); } } }
public void SaveGame() { Debug.Log("Game Saved"); GameSaveUtility.gameSaveData = GetSaveData(); GameSaveUtility.Save(); }
/// <summary> /// Posts the player's money, team, first time boolean and name to the PlayerInfo database where it matches the player ID /// </summary> /// <param name="logout">Determines if the coroutine is being called alongside a logout action. If true, delete all player prefs</param> /// <returns></returns> IEnumerator StorePlayerDataRoutine(bool logout) { WWWForm form = new WWWForm(); form.AddField("player_id", GameSaveUtility.GetID()); form.AddField("player_money", GameSaveUtility.GetTotalBalance()); if (GameSaveUtility.GetChapProgress(1)) { form.AddField("progress1", 1); } if (GameSaveUtility.GetChapProgress(2)) { form.AddField("progress2", 1); } if (GameSaveUtility.GetChapProgress(3)) { form.AddField("progress3", 1); } using (UnityWebRequest www = UnityWebRequest.Post(path + "updateplayerinfo.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form upload complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); #endif if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("userUpdated")) { if (www.GetResponseHeaders()["userUpdated"] == "true") { Debug.Log("Player data saved succesfully"); if (logout) { Debug.Log("LOGOUT"); GameSaveUtility.Reset(); // if updating the player is called during a logout action, delete all player pref local storage canvasManager.OpenStart(); // makes sure the page when logging back in starts with the start menu and not settings } // Print Body #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } else { Debug.Log("Player data saving failed"); Debug.Log(www.downloadHandler.text); } } } } }
/// <summary> /// Using the Signup Canvas input fields to post account name, account password and email to PHP. Returns the account ID to store in GameSaveUtility /// </summary> /// <returns></returns> IEnumerator LoginUserRoutione() { WWWForm form = new WWWForm(); form.AddField("account_name", loginCanvas.transform.GetChild(0).GetComponent <InputField>().text); form.AddField("account_pass", loginCanvas.transform.GetChild(1).GetComponent <InputField>().text); string[] dbText; bool successfulLogin = false; using (UnityWebRequest www = UnityWebRequest.Post(path + "verifylogin.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form load complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); #endif if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("LoginSucceed")) { if (www.GetResponseHeaders()["LoginSucceed"] == "true") { Debug.Log("Login succesful"); dbText = www.downloadHandler.text.Split('\n'); // splits all echoed text from PHP status = true; GameSaveUtility.SaveID(int.Parse(dbText[1])); // know that the ID is echoed on the 2nd line of the db text body" successfulLogin = true; // boolean trigger to retrieve all player data } else { loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login"; string bodyError = "Login failed: "; if (www.GetResponseHeaders().ContainsKey("PasswordFail")) { bodyError += "Inccorect password.\n"; } if (www.GetResponseHeaders().ContainsKey("AccountFail")) { bodyError += "Inccorect account name.\n"; } loginPopup.transform.GetChild(1).GetComponent <Text>().text = bodyError; loginPopup.SetActive(true); #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } } else { // Failed loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login"; loginPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server."; loginPopup.SetActive(true); #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } } } // if successful login, retrieve all player data and store to PlayerPrefs if (successfulLogin) { form = new WWWForm(); int playerID = GameSaveUtility.GetID(); form.AddField("player_ID", playerID); form.AddField("request", "getlevels"); using (UnityWebRequest www = UnityWebRequest.Post(path + "getplayerdata.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form load complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); #endif if (www.GetResponseHeaders().Count > 0) { Debug.Log(www.downloadHandler.text); dbText = www.downloadHandler.text.Split('\n'); MenuData menuData = JsonUtility.FromJson <MenuData>(dbText[0]); menuData.StoreLevels(); // store level information back into local PlayerPrefs string parsePlayer = dbText[1].Replace("[", "").Replace("]", ""); // format the player text outside of the array to be recognized as a PlayerData object JSON PlayerData playerData = JsonUtility.FromJson <PlayerData>(parsePlayer); playerData.SetPlayerInfo(); // store player information back into the local PlayerPrefs } else { // Failed loginPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Login"; loginPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server."; loginPopup.SetActive(true); #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } } } if (successfulLogin) { canvasManager.SwitchCanvas("menus"); } } }
/// <summary> /// Using the Signup Canvas input fields to post account name, account password and email to PHP. Returns the account ID to store in GameSaveUtility /// </summary> /// <returns></returns> IEnumerator CreateNewUserRoutine() { string[] dbText; WWWForm form = new WWWForm(); form.AddField("account_name", signupCanvas.transform.GetChild(0).GetComponent <InputField>().text); form.AddField("account_pass", signupCanvas.transform.GetChild(1).GetComponent <InputField>().text); form.AddField("confirm_pass", signupCanvas.transform.GetChild(2).GetComponent <InputField>().text); form.AddField("email", signupCanvas.transform.GetChild(3).GetComponent <InputField>().text); using (UnityWebRequest www = UnityWebRequest.Post(path + "createaccount.php", form)) { yield return(www.SendWebRequest()); if (www.isNetworkError || www.isHttpError) { Debug.Log(www.error); } else { Debug.Log("Form load complete!"); StringBuilder sb = new StringBuilder(); foreach (System.Collections.Generic.KeyValuePair <string, string> dict in www.GetResponseHeaders()) { sb.Append(dict.Key).Append(": \t[").Append(dict.Value).Append("]\n"); } // Print Headers #if (UNITY_EDITOR) Debug.Log(sb.ToString()); #endif if (www.GetResponseHeaders().Count > 0 && www.GetResponseHeaders().ContainsKey("UserCreation")) { if (www.GetResponseHeaders()["UserCreation"] == "true") { Debug.Log("New account successful."); dbText = www.downloadHandler.text.Split('\n'); status = true; GameSaveUtility.SaveID(int.Parse(dbText[2])); // know that the ID is echoed on the 3rd line of the db text body canvasManager.SwitchCanvas("input"); } else { signupPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Create Account"; string bodyError = "Create account failed: "; if (www.GetResponseHeaders().ContainsKey("PasswordMatch")) { bodyError += "Passwords do not match.\n"; } if (www.GetResponseHeaders().ContainsKey("AccountFail")) { bodyError += "This account name has been taken. Please choose another.\n"; } if (www.GetResponseHeaders().ContainsKey("EmailFail")) { bodyError += "There is already an account under this email.\n"; } if (www.GetResponseHeaders().ContainsKey("EmailFormat")) { bodyError += "This is not a valid email.\n"; } signupPopup.transform.GetChild(1).GetComponent <Text>().text = bodyError; signupPopup.SetActive(true); #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } } else { // Failed signupPopup.transform.GetChild(0).GetComponent <Text>().text = "ERROR: Create Account"; signupPopup.transform.GetChild(1).GetComponent <Text>().text = "Unable to connect to server."; signupPopup.SetActive(true); #if (UNITY_EDITOR) Debug.Log(www.downloadHandler.text); #endif } } } }