From 48552dfcb6aa0e587d291516afb81d9f1eb88be4 Mon Sep 17 00:00:00 2001 From: Rabidus Date: Tue, 22 Aug 2023 17:02:25 +0300 Subject: [PATCH] Added Save/load system --- Assets/SaveLoadController.cs | 72 +++++++++++++++++++ Assets/SaveLoadController.cs.meta | 11 +++ Assets/Scripts/PlayerInfo.cs | 5 ++ Assets/Scripts/PlayerSetup.cs | 18 +++-- Assets/StreamingAssets.meta | 8 --- .../UnityServicesProjectConfiguration.json | 1 - ...nityServicesProjectConfiguration.json.meta | 7 -- ProjectSettings/ProjectSettings.asset | 1 + 8 files changed, 100 insertions(+), 23 deletions(-) create mode 100644 Assets/SaveLoadController.cs create mode 100644 Assets/SaveLoadController.cs.meta delete mode 100644 Assets/StreamingAssets.meta delete mode 100644 Assets/StreamingAssets/UnityServicesProjectConfiguration.json delete mode 100644 Assets/StreamingAssets/UnityServicesProjectConfiguration.json.meta diff --git a/Assets/SaveLoadController.cs b/Assets/SaveLoadController.cs new file mode 100644 index 00000000..1971455f --- /dev/null +++ b/Assets/SaveLoadController.cs @@ -0,0 +1,72 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; +using System.Runtime.Serialization.Formatters.Binary; +using UnityEngine; + +public class SaveLoadController : MonoBehaviour +{ + public static SaveLoadController Instance; + + private const string DIR_PATH = "/game_save/players/"; + private const string FILE_PATH = "players.txt"; + + private void Awake() + { + if(Instance == null) + { + Instance = this; + } + else if (Instance != this) + { + Destroy(this); + } + DontDestroyOnLoad(this); + } + + public void Save(T so) where T: ScriptableObject + { + string fullDirPath = Application.persistentDataPath + DIR_PATH; + string fullFilePath = Application.persistentDataPath + DIR_PATH + FILE_PATH; + + if (!Directory.Exists(fullDirPath)) + { + Directory.CreateDirectory(fullDirPath); + } + + BinaryFormatter bf = new BinaryFormatter(); + FileStream fs = File.Create(fullFilePath); + var json = JsonUtility.ToJson(so); + bf.Serialize(fs, json); + fs.Close(); + } + + public void Load(ref T so) where T: ScriptableObject + { + string fullDirPath = Application.persistentDataPath + DIR_PATH; + string fullFilePath = Application.persistentDataPath + DIR_PATH + FILE_PATH; + + Debug.Log(fullFilePath); + Debug.Log(fullDirPath); + + if (!Directory.Exists(fullDirPath)) + { + Directory.CreateDirectory(fullDirPath); + } + + BinaryFormatter bf = new BinaryFormatter(); + + if (File.Exists(fullFilePath)) + { + FileStream fs = File.Open(fullFilePath, FileMode.Open); + so = ScriptableObject.CreateInstance(); + JsonUtility.FromJsonOverwrite((string)bf.Deserialize(fs), so); + + fs.Close(); + } + else + { + so = ScriptableObject.CreateInstance(); + } + } +} diff --git a/Assets/SaveLoadController.cs.meta b/Assets/SaveLoadController.cs.meta new file mode 100644 index 00000000..38cb6465 --- /dev/null +++ b/Assets/SaveLoadController.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 93376b36feb94b74ca3e784ec321fc7e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/Scripts/PlayerInfo.cs b/Assets/Scripts/PlayerInfo.cs index 2d35b99b..5e73c6bb 100644 --- a/Assets/Scripts/PlayerInfo.cs +++ b/Assets/Scripts/PlayerInfo.cs @@ -20,4 +20,9 @@ public class PlayerInfo : ScriptableObject { public List Players = new List(); + public void AddNewPlayer(Player player) + { + Players.Add(player); + } + } diff --git a/Assets/Scripts/PlayerSetup.cs b/Assets/Scripts/PlayerSetup.cs index 953f7813..5019bcb9 100644 --- a/Assets/Scripts/PlayerSetup.cs +++ b/Assets/Scripts/PlayerSetup.cs @@ -1,3 +1,4 @@ +using System; using System.Collections; using System.Collections.Generic; using UnityEngine; @@ -5,7 +6,6 @@ using UnityEngine; public class PlayerSetup : MonoBehaviour { public static PlayerSetup Instance; - [SerializeField] private PlayerInfo _playerInfo; [SerializeField] private Player _currentPlayer; @@ -25,18 +25,27 @@ public class PlayerSetup : MonoBehaviour } DontDestroyOnLoad(gameObject); + + LoadPlayerData(); + } + + private void LoadPlayerData() + { + SaveLoadController.Instance.Load(ref _playerInfo); } public Player CreateNewPlayer() { _currentPlayer = new Player(); - SavePlayer(); + _playerInfo.AddNewPlayer(_currentPlayer); + SaveLoadController.Instance.Save(_playerInfo); return _currentPlayer; } public void SetPlayerName(string name) { _currentPlayer.Name = name; + SaveLoadController.Instance.Save(_playerInfo); } public void SetPlayerScore(float score) @@ -53,9 +62,4 @@ public class PlayerSetup : MonoBehaviour { return _currentPlayer.UnlockedMonumets.Contains(info); } - - public void SavePlayer() - { - _playerInfo.Players.Add(_currentPlayer); - } } diff --git a/Assets/StreamingAssets.meta b/Assets/StreamingAssets.meta deleted file mode 100644 index e5a793c5..00000000 --- a/Assets/StreamingAssets.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: 07bfafa14538ac741b2cb55975986490 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/StreamingAssets/UnityServicesProjectConfiguration.json b/Assets/StreamingAssets/UnityServicesProjectConfiguration.json deleted file mode 100644 index c0a91ee6..00000000 --- a/Assets/StreamingAssets/UnityServicesProjectConfiguration.json +++ /dev/null @@ -1 +0,0 @@ -{"Keys":["com.unity.services.core.cloud-environment","com.unity.services.core.version"],"Values":[{"m_Value":"production","m_IsReadOnly":false},{"m_Value":"1.4.3","m_IsReadOnly":true}]} \ No newline at end of file diff --git a/Assets/StreamingAssets/UnityServicesProjectConfiguration.json.meta b/Assets/StreamingAssets/UnityServicesProjectConfiguration.json.meta deleted file mode 100644 index 986d5a2f..00000000 --- a/Assets/StreamingAssets/UnityServicesProjectConfiguration.json.meta +++ /dev/null @@ -1,7 +0,0 @@ -fileFormatVersion: 2 -guid: ed0a60712ef404f4b9e202287acbbd51 -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/ProjectSettings/ProjectSettings.asset b/ProjectSettings/ProjectSettings.asset index ec96cce9..097e3c8f 100644 --- a/ProjectSettings/ProjectSettings.asset +++ b/ProjectSettings/ProjectSettings.asset @@ -154,6 +154,7 @@ PlayerSettings: - {fileID: 0} - {fileID: 0} - {fileID: 0} + - {fileID: 0} - {fileID: -6517218471499782410, guid: 1a4c68ca72a83449f938d669337cb305, type: 2} - {fileID: 648089955447955448, guid: ac7df53da7627c941aa8b5303dd3d14f, type: 2} metroInputSource: 0