Skip to content

Commit 9a6f967

Browse files
committed
Save To Json
Save To Json
1 parent 4cc694a commit 9a6f967

File tree

11 files changed

+305
-5
lines changed

11 files changed

+305
-5
lines changed

Save System Profect/Assets/Scene/SampleScene.unity

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ GameObject:
430430
- component: {fileID: 628291007}
431431
- component: {fileID: 628291005}
432432
m_Layer: 0
433-
m_Name: '[PLAYERPREFS MANAGER]'
433+
m_Name: '[SAVE TO PLAYERPREFS MANAGER]'
434434
m_TagString: Untagged
435435
m_Icon: {fileID: 0}
436436
m_NavMeshLayer: 0
@@ -588,8 +588,81 @@ Transform:
588588
m_ConstrainProportionsScale: 0
589589
m_Children: []
590590
m_Father: {fileID: 0}
591-
m_RootOrder: 4
591+
m_RootOrder: 5
592592
m_LocalEulerAnglesHint: {x: 50, y: -30, z: 0}
593+
--- !u!1 &806737137
594+
GameObject:
595+
m_ObjectHideFlags: 0
596+
m_CorrespondingSourceObject: {fileID: 0}
597+
m_PrefabInstance: {fileID: 0}
598+
m_PrefabAsset: {fileID: 0}
599+
serializedVersion: 6
600+
m_Component:
601+
- component: {fileID: 806737138}
602+
- component: {fileID: 806737139}
603+
m_Layer: 0
604+
m_Name: '[SAVE TO JSON MANAGER]'
605+
m_TagString: Untagged
606+
m_Icon: {fileID: 0}
607+
m_NavMeshLayer: 0
608+
m_StaticEditorFlags: 0
609+
m_IsActive: 0
610+
--- !u!4 &806737138
611+
Transform:
612+
m_ObjectHideFlags: 0
613+
m_CorrespondingSourceObject: {fileID: 0}
614+
m_PrefabInstance: {fileID: 0}
615+
m_PrefabAsset: {fileID: 0}
616+
m_GameObject: {fileID: 806737137}
617+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
618+
m_LocalPosition: {x: 0, y: 0, z: 0}
619+
m_LocalScale: {x: 1, y: 1, z: 1}
620+
m_ConstrainProportionsScale: 0
621+
m_Children: []
622+
m_Father: {fileID: 0}
623+
m_RootOrder: 2
624+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
625+
--- !u!114 &806737139
626+
MonoBehaviour:
627+
m_ObjectHideFlags: 0
628+
m_CorrespondingSourceObject: {fileID: 0}
629+
m_PrefabInstance: {fileID: 0}
630+
m_PrefabAsset: {fileID: 0}
631+
m_GameObject: {fileID: 806737137}
632+
m_Enabled: 1
633+
m_EditorHideFlags: 0
634+
m_Script: {fileID: 11500000, guid: dedb7d329a929e34db45ca0741950ea6, type: 3}
635+
m_Name:
636+
m_EditorClassIdentifier:
637+
typePlatform: 0
638+
characterid: 0
639+
selectedCharacter:
640+
id: 0
641+
damage: 0
642+
healt: 0
643+
speed: 0
644+
name:
645+
characters:
646+
- id: 0
647+
damage: 0
648+
healt: 0
649+
speed: 0
650+
name:
651+
lastCarIndex: 0
652+
selectedCar:
653+
speed: 0
654+
mass: 0
655+
isPassenger: 0
656+
name:
657+
doors:
658+
cars:
659+
- speed: 0
660+
mass: 0
661+
isPassenger: 0
662+
name:
663+
doors: 000000000000000000000000
664+
savePath:
665+
saveFileName: data.json
593666
--- !u!1 &947137420
594667
GameObject:
595668
m_ObjectHideFlags: 0
@@ -807,7 +880,7 @@ Transform:
807880
m_ConstrainProportionsScale: 0
808881
m_Children: []
809882
m_Father: {fileID: 0}
810-
m_RootOrder: 3
883+
m_RootOrder: 4
811884
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
812885
--- !u!1 &1077484690
813886
GameObject:
@@ -1235,7 +1308,7 @@ Transform:
12351308
m_Children:
12361309
- {fileID: 1314080177}
12371310
m_Father: {fileID: 0}
1238-
m_RootOrder: 5
1311+
m_RootOrder: 6
12391312
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
12401313
--- !u!1 &1560134789
12411314
GameObject:
@@ -1417,5 +1490,5 @@ Transform:
14171490
m_ConstrainProportionsScale: 0
14181491
m_Children: []
14191492
m_Father: {fileID: 0}
1420-
m_RootOrder: 2
1493+
m_RootOrder: 3
14211494
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}

Save System Profect/Assets/Scripts/Save to Json.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using Scripts.SaveToJson.Struct;
2+
using UnityEngine;
3+
using System;
4+
using System.IO;
5+
6+
namespace Scripts.SaveToJson
7+
{
8+
public class SaveToJson : MonoBehaviour
9+
{
10+
public enum TypePlatform
11+
{
12+
PC,
13+
Android
14+
}
15+
public TypePlatform typePlatform;
16+
17+
[Header("Character")]
18+
[SerializeField] private int characterid;
19+
[SerializeField] private CharacterStruct selectedCharacter;
20+
[SerializeField] private CharacterStruct[] characters;
21+
[Space(10)]
22+
23+
[Header("Car")]
24+
[SerializeField] private int lastCarIndex;
25+
[SerializeField] private CarStruct selectedCar;
26+
[SerializeField] private CarStruct[] cars;
27+
[Space(10)]
28+
29+
[Header("Save Config")]
30+
[SerializeField] private string savePath;
31+
[SerializeField] private string saveFileName = "data.json";
32+
33+
private void Awake()
34+
{
35+
switch (typePlatform)
36+
{
37+
case TypePlatform.PC:
38+
savePath = Path.Combine(Application.persistentDataPath, saveFileName);
39+
break;
40+
case TypePlatform.Android:
41+
savePath = Path.Combine(Application.dataPath, saveFileName);
42+
break;
43+
}
44+
45+
LoadToFile();
46+
}
47+
48+
private void OnEnable()
49+
{
50+
ButtonManager.clickButtonSaveData += SaveToFile;
51+
ButtonManager.clickButtonLoadData += LoadToFile;
52+
// ButtonManager.clickButtonDeleteData += OnAllDeleteData;
53+
}
54+
55+
private void OnDisable()
56+
{
57+
ButtonManager.clickButtonSaveData -= SaveToFile;
58+
ButtonManager.clickButtonLoadData -= LoadToFile;
59+
// ButtonManager.clickButtonDeleteData -= OnAllDeleteData;
60+
}
61+
62+
private void SaveToFile()
63+
{
64+
GameDataStruct gameData = new GameDataStruct
65+
{
66+
characterid = this.characterid,
67+
selectedCharacter = this.selectedCharacter,
68+
characters = this.characters,
69+
70+
lastCarIndex = this.lastCarIndex,
71+
selectedCar = this.selectedCar,
72+
cars = this.cars
73+
};
74+
75+
string json = JsonUtility.ToJson(gameData, true);
76+
77+
try
78+
{
79+
File.WriteAllText(savePath, json);
80+
}
81+
catch(Exception e)
82+
{
83+
Debug.Log("{GameLog} => [SaveToJson] - (<color=red>Error</color>) - SaveToFile ->" + e.Message);
84+
}
85+
}
86+
private void LoadToFile()
87+
{
88+
if (!File.Exists(savePath))
89+
{
90+
Debug.Log("{GameLog} => [SaveToJson] - LoadToFile -> File is Not Found");
91+
return;
92+
}
93+
94+
try
95+
{
96+
string json = File.ReadAllText(savePath);
97+
98+
GameDataStruct gameDaraFromJson = JsonUtility.FromJson<GameDataStruct>(json);
99+
this.characterid = gameDaraFromJson.characterid;
100+
this.selectedCharacter = gameDaraFromJson.selectedCharacter;
101+
this.characters = gameDaraFromJson.characters;
102+
103+
this.lastCarIndex = gameDaraFromJson.lastCarIndex;
104+
this.selectedCar = gameDaraFromJson.selectedCar;
105+
this.cars = gameDaraFromJson.cars;
106+
}
107+
catch (Exception e)
108+
{
109+
Debug.Log("{GameLog} => [SaveToJson] - (<color=red>Error</color>) - LoadToFile ->" + e.Message);
110+
}
111+
}
112+
113+
private void OnApplicationQuit()
114+
{
115+
SaveToFile();
116+
}
117+
118+
private void OnApplicationPause(bool pause)
119+
{
120+
if(Application.platform == RuntimePlatform.Android)
121+
{
122+
SaveToFile();
123+
}
124+
}
125+
}
126+
}
127+

Save System Profect/Assets/Scripts/Save to Json/SaveToJson.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.

Save System Profect/Assets/Scripts/Save to Json/Structure.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Scripts.SaveToJson.Struct
2+
{
3+
[System.Serializable]
4+
public struct CarStruct
5+
{
6+
public float speed;
7+
public int mass;
8+
public bool isPassenger;
9+
10+
public string name;
11+
public int[] doors;
12+
}
13+
}

Save System Profect/Assets/Scripts/Save to Json/Structure/CarStruct.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: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Scripts.SaveToJson.Struct{
2+
[System.Serializable]
3+
public struct CharacterStruct{
4+
public int id;
5+
public int damage;
6+
7+
public float healt;
8+
public float speed;
9+
10+
public string name;
11+
}
12+
}
13+

Save System Profect/Assets/Scripts/Save to Json/Structure/CharacterStruct.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: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Scripts.SaveToJson.Struct
2+
{
3+
[System.Serializable]
4+
public struct GameDataStruct
5+
{
6+
public int characterid;
7+
public CharacterStruct selectedCharacter;
8+
public CharacterStruct[] characters;
9+
10+
public int lastCarIndex;
11+
public CarStruct selectedCar;
12+
public CarStruct[] cars;
13+
}
14+
}

0 commit comments

Comments
 (0)