Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///Dieton 2023
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class PlayerJsonSaveLoad: MonoBehaviour
- {
- //---- playerData ----//
- [SerializeField]
- public SaveLoadData gameData;
- public string playerName;
- //==== Awake ====//
- private void Awake()
- {
- //save or loda file checker
- if (JsonManager.SaveCheck())
- {
- SaveLoad(DataState.load);
- }
- else
- {
- SaveLoad(DataState.save);
- }
- }
- //==== Save Load Check Data ====//
- private void SaveLoad(DataState dataState)
- {
- //true = save, false = load
- if (dataState == DataState.save)
- {
- SaveData("AutoSave1");
- }
- else if (dataState == DataState.load)
- {
- LoadData();
- LoadDataApply(gameData.lastSavedTitle);
- }
- }
- //==== Save Data ====//
- public void SaveData(string _saveName)
- {
- gameData.lastSavedTitle = _saveName;
- foreach (var _playerData in gameData.playerData)
- {
- if (_playerData.saveName == _saveName)
- {
- SaveOverride(_saveName);
- return;
- }
- }
- //---- New Save ----//
- gameData.playerData.Add(new PlayerData(
- _saveName,
- System.DateTime.Now.ToString("g"),
- GameManager.level,
- playerName
- ));
- Save();
- }
- //==== Save Override ====//
- public void SaveOverride(string _saveName)
- {
- for (int i = 0; i < gameData.playerData.Count; i++)
- {
- if (gameData.playerData[i].saveName == _saveName)
- {
- //save data
- gameData.playerData[i].saveName = _saveName;
- gameData.playerData[i].saveTimeDate = System.DateTime.Now.ToString("g");
- //game data
- gameData.playerData[i].levelId = levelId;
- //player data
- gameData.playerData[i].playerName = playerName;
- }
- }
- Save();
- }
- //==== Crate a save file ====//
- public void Save() {
- JsonManager.Save(gameData);
- }
- //==== Loaded data Apply ====//
- public void LoadDataApply(string loadName)
- {
- for (int i = 0; i < gameData.playerData.Count; i++)
- {
- if (gameData.playerData[i].saveName == loadName)
- {
- GameManager.levelID = gameData.playerData[i].levelID;
- playerName = gameData.playerData[i].playerName;
- }
- }
- gameData.lastSavedTitle = loadName;
- SaveData(loadName);
- }
- //==== Load Data ====//
- public void LoadData()
- {
- gameData = JsonManager.Load();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement