Added Save/load system
This commit is contained in:
parent
6771ed8340
commit
48552dfcb6
|
@ -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>(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<T>(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<T>();
|
||||||
|
JsonUtility.FromJsonOverwrite((string)bf.Deserialize(fs), so);
|
||||||
|
|
||||||
|
fs.Close();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
so = ScriptableObject.CreateInstance<T>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,11 @@
|
||||||
|
fileFormatVersion: 2
|
||||||
|
guid: 93376b36feb94b74ca3e784ec321fc7e
|
||||||
|
MonoImporter:
|
||||||
|
externalObjects: {}
|
||||||
|
serializedVersion: 2
|
||||||
|
defaultReferences: []
|
||||||
|
executionOrder: 0
|
||||||
|
icon: {instanceID: 0}
|
||||||
|
userData:
|
||||||
|
assetBundleName:
|
||||||
|
assetBundleVariant:
|
|
@ -20,4 +20,9 @@ public class PlayerInfo : ScriptableObject
|
||||||
{
|
{
|
||||||
public List<Player> Players = new List<Player>();
|
public List<Player> Players = new List<Player>();
|
||||||
|
|
||||||
|
public void AddNewPlayer(Player player)
|
||||||
|
{
|
||||||
|
Players.Add(player);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
using System;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
@ -5,7 +6,6 @@ using UnityEngine;
|
||||||
public class PlayerSetup : MonoBehaviour
|
public class PlayerSetup : MonoBehaviour
|
||||||
{
|
{
|
||||||
public static PlayerSetup Instance;
|
public static PlayerSetup Instance;
|
||||||
[SerializeField]
|
|
||||||
private PlayerInfo _playerInfo;
|
private PlayerInfo _playerInfo;
|
||||||
[SerializeField]
|
[SerializeField]
|
||||||
private Player _currentPlayer;
|
private Player _currentPlayer;
|
||||||
|
@ -25,18 +25,27 @@ public class PlayerSetup : MonoBehaviour
|
||||||
}
|
}
|
||||||
|
|
||||||
DontDestroyOnLoad(gameObject);
|
DontDestroyOnLoad(gameObject);
|
||||||
|
|
||||||
|
LoadPlayerData();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void LoadPlayerData()
|
||||||
|
{
|
||||||
|
SaveLoadController.Instance.Load(ref _playerInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Player CreateNewPlayer()
|
public Player CreateNewPlayer()
|
||||||
{
|
{
|
||||||
_currentPlayer = new Player();
|
_currentPlayer = new Player();
|
||||||
SavePlayer();
|
_playerInfo.AddNewPlayer(_currentPlayer);
|
||||||
|
SaveLoadController.Instance.Save(_playerInfo);
|
||||||
return _currentPlayer;
|
return _currentPlayer;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetPlayerName(string name)
|
public void SetPlayerName(string name)
|
||||||
{
|
{
|
||||||
_currentPlayer.Name = name;
|
_currentPlayer.Name = name;
|
||||||
|
SaveLoadController.Instance.Save(_playerInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SetPlayerScore(float score)
|
public void SetPlayerScore(float score)
|
||||||
|
@ -53,9 +62,4 @@ public class PlayerSetup : MonoBehaviour
|
||||||
{
|
{
|
||||||
return _currentPlayer.UnlockedMonumets.Contains(info);
|
return _currentPlayer.UnlockedMonumets.Contains(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void SavePlayer()
|
|
||||||
{
|
|
||||||
_playerInfo.Players.Add(_currentPlayer);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +0,0 @@
|
||||||
fileFormatVersion: 2
|
|
||||||
guid: 07bfafa14538ac741b2cb55975986490
|
|
||||||
folderAsset: yes
|
|
||||||
DefaultImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
|
@ -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}]}
|
|
|
@ -1,7 +0,0 @@
|
||||||
fileFormatVersion: 2
|
|
||||||
guid: ed0a60712ef404f4b9e202287acbbd51
|
|
||||||
DefaultImporter:
|
|
||||||
externalObjects: {}
|
|
||||||
userData:
|
|
||||||
assetBundleName:
|
|
||||||
assetBundleVariant:
|
|
|
@ -154,6 +154,7 @@ PlayerSettings:
|
||||||
- {fileID: 0}
|
- {fileID: 0}
|
||||||
- {fileID: 0}
|
- {fileID: 0}
|
||||||
- {fileID: 0}
|
- {fileID: 0}
|
||||||
|
- {fileID: 0}
|
||||||
- {fileID: -6517218471499782410, guid: 1a4c68ca72a83449f938d669337cb305, type: 2}
|
- {fileID: -6517218471499782410, guid: 1a4c68ca72a83449f938d669337cb305, type: 2}
|
||||||
- {fileID: 648089955447955448, guid: ac7df53da7627c941aa8b5303dd3d14f, type: 2}
|
- {fileID: 648089955447955448, guid: ac7df53da7627c941aa8b5303dd3d14f, type: 2}
|
||||||
metroInputSource: 0
|
metroInputSource: 0
|
||||||
|
|
Loading…
Reference in New Issue