Skip to content

Commit 74798f1

Browse files
committed
lab 9
1 parent 99315cc commit 74798f1

9 files changed

Lines changed: 147 additions & 11 deletions

File tree

Assets/Scenes/SampleScene.unity

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,7 @@ GameObject:
19121912
- component: {fileID: 1437617075}
19131913
- component: {fileID: 1437617074}
19141914
- component: {fileID: 1437617073}
1915+
- component: {fileID: 1437617076}
19151916
m_Layer: 0
19161917
m_Name: GameManager
19171918
m_TagString: Untagged
@@ -1959,6 +1960,18 @@ Transform:
19591960
m_Father: {fileID: 0}
19601961
m_RootOrder: 4
19611962
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
1963+
--- !u!114 &1437617076
1964+
MonoBehaviour:
1965+
m_ObjectHideFlags: 0
1966+
m_CorrespondingSourceObject: {fileID: 0}
1967+
m_PrefabInstance: {fileID: 0}
1968+
m_PrefabAsset: {fileID: 0}
1969+
m_GameObject: {fileID: 1437617072}
1970+
m_Enabled: 1
1971+
m_EditorHideFlags: 0
1972+
m_Script: {fileID: 11500000, guid: c5079e66ace0db948b1c1e5d4f7e3e02, type: 3}
1973+
m_Name:
1974+
m_EditorClassIdentifier:
19621975
--- !u!1 &1451123247 stripped
19631976
GameObject:
19641977
m_CorrespondingSourceObject: {fileID: 919132149155446097, guid: 699ab6959c727504684c715d41f4b221, type: 3}

Assets/Scripts/Data Persistance/DataPersistanceManager.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using UnityEngine;
45
using UnityEngine.Playables;
56

@@ -10,8 +11,11 @@ public class DataPersistanceManager : MonoBehaviour
1011
[Header("File Storage Config")]
1112
[SerializeField] private string fileName;
1213

14+
[SerializeField] private bool useEncryption;
15+
1316
private FileHandler dataHandler;
1417
private GameData gameData;
18+
private List<IDataPersistance> dataPersistanceObjects;
1519

1620
private void Awake()
1721
{
@@ -23,11 +27,9 @@ private void Awake()
2327
{
2428
Debug.Log("Found more than one Data Persistance Manager in the scene");
2529
}
26-
}
2730

28-
private void Start()
29-
{
30-
this.dataHandler = new FileHandler(Application.persistentDataPath, fileName);
31+
this.dataHandler = new FileHandler(Application.persistentDataPath, fileName, useEncryption);
32+
this.dataPersistanceObjects = FindAllDataPersistanceObjects();
3133
LoadGame();
3234
}
3335

@@ -46,18 +48,34 @@ public void LoadGame()
4648
NewGame();
4749
}
4850

49-
Debug.Log("Loaded score = " + gameData.score);
51+
foreach (IDataPersistance dataPersistanceObj in dataPersistanceObjects)
52+
{
53+
dataPersistanceObj.LoadData(gameData);
54+
}
55+
56+
//Debug.Log("Loaded score = " + gameData.score);
5057
}
5158

5259
public void SaveGame()
5360
{
61+
foreach (IDataPersistance dataPersistanceObj in dataPersistanceObjects)
62+
{
63+
dataPersistanceObj.SaveData(ref gameData);
64+
}
65+
5466
dataHandler.Save(gameData);
5567

56-
Debug.Log("Saved score = " + gameData.score);
68+
// Debug.Log("Saved score = " + gameData.score);
5769
}
5870

5971
private void OnApplicationQuit()
6072
{
6173
SaveGame();
6274
}
75+
private List<IDataPersistance> FindAllDataPersistanceObjects()
76+
{
77+
IEnumerable<IDataPersistance> dataPersistanceObjects = FindObjectsOfType<MonoBehaviour>().OfType<IDataPersistance>();
78+
79+
return new List<IDataPersistance>(dataPersistanceObjects);
80+
}
6381
}

Assets/Scripts/Data Persistance/FileHandler.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ public class FileHandler
1010
private string dataDirPath = "";
1111
private string dataFileName = "";
1212

13-
public FileHandler(string dataDirPath, string dataFileName)
13+
private bool useEncryption = false;
14+
private readonly string encryptionCode = "word";
15+
16+
public FileHandler(string dataDirPath, string dataFileName, bool useEncryption)
1417
{
1518
this.dataDirPath = dataDirPath;
1619
this.dataFileName = dataFileName;
20+
21+
this.useEncryption = useEncryption;
1722
}
1823

1924
public GameData Load()
@@ -34,6 +39,11 @@ public GameData Load()
3439
}
3540
}
3641

42+
if (useEncryption)
43+
{
44+
dataToLoad = EncryptDecrypt(dataToLoad);
45+
}
46+
3747
loadedData = JsonUtility.FromJson<GameData>(dataToLoad);
3848
}
3949
catch (Exception e)
@@ -58,6 +68,12 @@ public void Save(GameData data)
5868
// Serialize the C# game data object into JSON
5969
string dataToStore = JsonUtility.ToJson(data, true);
6070

71+
// Optionally encrypt the data
72+
if (useEncryption)
73+
{
74+
dataToStore = EncryptDecrypt(dataToStore);
75+
}
76+
6177
using (FileStream stream = new FileStream(fullPath, FileMode.Create))
6278
{
6379
using (StreamWriter writer = new StreamWriter(stream))
@@ -71,4 +87,13 @@ public void Save(GameData data)
7187
Debug.LogError("Error occured when trying to save data to file: " + fullPath + "\n" + e);
7288
}
7389
}
90+
private string EncryptDecrypt(string data)
91+
{
92+
string modifiedData = "";
93+
for (int i = 0; i < data.Length; i++)
94+
{
95+
modifiedData += (char)(data[i] ^ encryptionCode[i % encryptionCode.Length]);
96+
}
97+
return modifiedData;
98+
}
7499
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
public interface IDataPersistance
5+
{
6+
void LoadData(GameData data);
7+
void SaveData(ref GameData data);
8+
}

Assets/Scripts/Data Persistance/IDataPersistance.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
5+
public class RefExample : MonoBehaviour
6+
{
7+
int a = 10;
8+
int b = 20;
9+
10+
// Start is called before the first frame update
11+
void Start()
12+
{
13+
AddValue(a);
14+
SubtractValue(ref b);
15+
16+
Debug.Log(a);
17+
Debug.Log(b);
18+
}
19+
20+
void AddValue(int arg)
21+
{
22+
arg += 1;
23+
}
24+
25+
void SubtractValue(ref int b)
26+
{
27+
b -= 15;
28+
}
29+
}

Assets/Scripts/Data Persistance/RefExample.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/PlayerMovement.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using UnityEngine;
44

5-
public class PlayerMovement : MonoBehaviour
5+
public class PlayerMovement : MonoBehaviour, IDataPersistance
66
{
77
[Header("Movement")]
88
[SerializeField] private float movementSpeed = 10f;
@@ -63,4 +63,13 @@ public void TurnPlayer()
6363

6464
}
6565

66+
public void SaveData(ref GameData data)
67+
{
68+
data.playerPosition = transform.position;
69+
}
70+
71+
public void LoadData(GameData data)
72+
{
73+
transform.position = data.playerPosition;
74+
}
6675
}

Assets/Scripts/ScoreManager.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@
44
using TMPro;
55
using UnityEngine.SocialPlatforms.Impl;
66

7-
public class ScoreManager : MonoBehaviour
7+
public class ScoreManager : MonoBehaviour, IDataPersistance
88
{
99
public static ScoreManager scoreManager;
1010
public TextMeshProUGUI scoreText;
1111
int totalScore = 0;
1212

13-
private void Start()
13+
private void Awake()
1414
{
1515
if (scoreManager == null)
1616
{
1717
scoreManager = this;
1818
}
19+
}
1920

20-
scoreText.text = "Score: 0";
21+
private void Start()
22+
{
23+
scoreText.text = "Score: " + totalScore.ToString();
2124
}
2225

2326
public void UpdateScore(int score)
@@ -26,4 +29,13 @@ public void UpdateScore(int score)
2629
Debug.Log(totalScore);
2730
scoreText.text = "Score: " + totalScore.ToString();
2831
}
32+
public void SaveData(ref GameData data)
33+
{
34+
data.score = this.totalScore;
35+
}
36+
37+
public void LoadData(GameData data)
38+
{
39+
this.totalScore = data.score;
40+
}
2941
}

0 commit comments

Comments
 (0)