Advertisement
Dieton

PlayerJsonSaveLoad

May 18th, 2023 (edited)
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.83 KB | Gaming | 0 0
  1. ///Dieton 2023
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5.  
  6. public class PlayerJsonSaveLoad: MonoBehaviour
  7. {
  8.     //---- playerData ----//
  9.     [SerializeField]
  10.     public SaveLoadData gameData;
  11.  
  12.     public string playerName;
  13.  
  14.     //==== Awake ====//
  15.     private void Awake()
  16.     {
  17.         //save or loda file checker
  18.         if (JsonManager.SaveCheck())
  19.         {
  20.             SaveLoad(DataState.load);
  21.         }
  22.         else
  23.         {
  24.             SaveLoad(DataState.save);
  25.         }
  26.     }
  27.  
  28.      //==== Save Load Check Data ====//
  29.     private void SaveLoad(DataState dataState)
  30.     {
  31.         //true = save, false = load
  32.         if (dataState == DataState.save)
  33.         {
  34.             SaveData("AutoSave1");
  35.         }
  36.         else if (dataState == DataState.load)
  37.         {
  38.             LoadData();
  39.             LoadDataApply(gameData.lastSavedTitle);
  40.         }
  41.     }
  42.  
  43.     //==== Save Data ====//
  44.     public void SaveData(string _saveName)
  45.     {
  46.         gameData.lastSavedTitle = _saveName;
  47.  
  48.         foreach (var _playerData in gameData.playerData)
  49.         {
  50.             if (_playerData.saveName == _saveName)
  51.             {
  52.                 SaveOverride(_saveName);
  53.                 return;
  54.             }
  55.         }
  56.  
  57.         //---- New Save ----//
  58.         gameData.playerData.Add(new PlayerData(
  59.             _saveName,
  60.             System.DateTime.Now.ToString("g"),
  61.             GameManager.level,
  62.             playerName
  63.         ));
  64.  
  65.         Save();
  66.     }
  67.  
  68.     //==== Save Override ====//
  69.     public void SaveOverride(string _saveName)
  70.     {
  71.         for (int i = 0; i < gameData.playerData.Count; i++)
  72.         {
  73.             if (gameData.playerData[i].saveName == _saveName)
  74.             {
  75.                 //save data
  76.                 gameData.playerData[i].saveName = _saveName;
  77.                 gameData.playerData[i].saveTimeDate = System.DateTime.Now.ToString("g");
  78.                 //game data
  79.                 gameData.playerData[i].levelId = levelId;
  80.                 //player data
  81.                 gameData.playerData[i].playerName = playerName;
  82.             }
  83.         }
  84.  
  85.         Save();
  86.     }
  87.  
  88.     //==== Crate a save file ====//
  89.     public void Save() {
  90.         JsonManager.Save(gameData);
  91.     }
  92.  
  93.     //==== Loaded data Apply ====//
  94.     public void LoadDataApply(string loadName)
  95.     {
  96.         for (int i = 0; i < gameData.playerData.Count; i++)
  97.         {
  98.             if (gameData.playerData[i].saveName == loadName)
  99.             {
  100.                 GameManager.levelID = gameData.playerData[i].levelID;
  101.                 playerName = gameData.playerData[i].playerName;
  102.             }
  103.         }
  104.  
  105.         gameData.lastSavedTitle = loadName;
  106.  
  107.         SaveData(loadName);
  108.     }
  109.  
  110.     //==== Load Data ====//
  111.     public void LoadData()
  112.     {
  113.         gameData = JsonManager.Load();
  114.     }
  115. }
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement