public void Save(bool mandatory = true, bool optional = true, bool hasBackup = true) { #if USE_GPGS_SAVEGAME if (GooglePlayServiceManager.instance.OnLoadFromCloud) { return; } #endif if (mandatory) { try { bool hasChanged = false; foreach (string key in mMandatory.Keys) { hasChanged |= mMandatory[key].DataChanged; } if (hasChanged) { LoadGameDictionary temp = new LoadGameDictionary(); bool checkValid = false; foreach (string key in mMandatory.Keys) { temp[key] = JsonUtility.ToJson(mMandatory[key].GetData()); checkValid = true; mMandatory[key].DataChanged = false; } if (checkValid) { byte[] data = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(temp)); SaveToFile(MANDATORY_SAVE_NAME, data, hasBackup); } } } catch (System.Exception) { } } if (optional) { try { bool hasChanged = false; foreach (string key in mOptional.Keys) { hasChanged |= mOptional[key].DataChanged; } if (hasChanged) { LoadGameDictionary temp = new LoadGameDictionary(); bool checkValid = false; foreach (string key in mOptional.Keys) { temp[key] = JsonUtility.ToJson(mOptional[key].GetData()); checkValid = true; mOptional[key].DataChanged = false; } if (checkValid) { byte[] data = System.Text.Encoding.UTF8.GetBytes(JsonUtility.ToJson(temp)); SaveToFile(OPTIONAL_SAVE_NAME, data, hasBackup); } } } catch (System.Exception) { } } }
public void Load(bool mandatory = true, bool optional = true, bool notification = true) { LoadGameDictionary loadDictionary = null; if (mandatory) { try { byte[] data = null; if (!LoadFromFile(MANDATORY_SAVE_NAME, ref data, true)) { LoadFromFile("_" + MANDATORY_SAVE_NAME, ref data, true); } loadDictionary = JsonUtility.FromJson <LoadGameDictionary>(data == null ? "{}" : System.Text.Encoding.UTF8.GetString(data, 0, data.Length)); } catch (System.Exception) { loadDictionary = null; } foreach (string key in mMandatory.Keys) { mMandatory[key].SetData(loadDictionary != null && loadDictionary.ContainsKey(key) && loadDictionary[key] != null ? loadDictionary[key] : ""); } } if (optional) { try { byte[] data = null; if (!LoadFromFile(OPTIONAL_SAVE_NAME, ref data, false)) { LoadFromFile("_" + OPTIONAL_SAVE_NAME, ref data, false); } loadDictionary = JsonUtility.FromJson <LoadGameDictionary>(data == null ? "{}" : System.Text.Encoding.UTF8.GetString(data, 0, data.Length)); } catch (System.Exception e) { Debug.LogError(e.Message); loadDictionary = null; } foreach (string key in mOptional.Keys) { mOptional[key].SetData(loadDictionary != null && loadDictionary.ContainsKey(key) && loadDictionary[key] != null ? loadDictionary[key] : ""); } } if (notification) { if (mandatory) { foreach (var item in mMandatory.Values) { item.OnAllDataLoaded(); } } if (optional) { foreach (var item in mOptional.Values) { item.OnAllDataLoaded(); } } } }