Post

KDT Unity 5주차 팀 프로젝트

2024-01-23 TIL

Json 파일을 이용하여 정보가져오기 및 수정하기

어제 정리한 내용은 json을 가져와 다시 정보를 수정할수 없었다.

수정까지 가능하게 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using Newtonsoft.Json;
using System.IO;
using UnityEngine;

public class DataManager
{
    /// <summary>
    /// 해당 json파일이 있는지 확인하는 메서드
    /// </summary>
    /// <param name="fileName"></param>
    public void CheckFile(string fileName)
    {
        string persistentPath = Path.Combine(Application.persistentDataPath, fileName); // 데이터 폴더의 파일 경로

        // 데이터 폴더에 파일이 없으면 "Resources" 폴더에서 복사
        if (!File.Exists(persistentPath))
        {
            TextAsset textAsset = Resources.Load<TextAsset>(fileName); // "Resources" 폴더에서 파일 읽기
            File.WriteAllText(persistentPath, textAsset.text); // 데이터 폴더에 파일 쓰기
        }
    }

    /// <summary>
    /// json파일을 읽어오는 메서드
    /// </summary>
    /// <returns></returns>
    public StageClear Lord()

    {
        string fileName = "PlayerDate";

        CheckFile(fileName);

        string persistentPath = Path.Combine(Application.persistentDataPath, fileName); // 데이터 폴더의 파일 경로

        // 데이터 폴더의 파일에서 JSON 문자열 읽기
        string json = File.ReadAllText(persistentPath);
        // JSON 문자열을 객체로 변환
        StageClear stageClear = JsonConvert.DeserializeObject<StageClear>(json);
        return stageClear;
    }

    /// <summary>
    /// 한번에 모든 데이터를 바꾸는 메서드
    /// </summary>
    /// <param name="stageDate"></param>
    public void ModifyWrite(StageClear stageDate)
    {

        string fileName = "PlayerDate";

        CheckFile(fileName);

        string persistentPath = Path.Combine(Application.persistentDataPath, fileName); // 데이터 폴더의 파일 경로

        // 객체를 다시 JSON 문자열로 변환
        string newJson = JsonConvert.SerializeObject(stageDate);

        // JSON 문자열을 데이터 폴더의 파일에 저장
        File.WriteAllText(persistentPath, newJson);
    }
    /// <summary>
    /// 스테이지 클리어값을 바꾸는 메서드
    /// </summary>
    /// <param name="stageClear"></param>
    public void ModifyStageClear(int stageClear)
    {
        string fileName = "PlayerDate";

        CheckFile(fileName);

        string persistentPath = Path.Combine(Application.persistentDataPath, fileName); // 데이터 폴더의 파일 경로

        // 파일에서 JSON 문자열 읽기
        string json = File.ReadAllText(persistentPath);

        // JSON 문자열을 객체로 변환
        StageClear stageDate = JsonConvert.DeserializeObject<StageClear>(json);

        // 속성 수정
        stageDate.stageClear = stageClear;

        // 객체를 다시 JSON 문자열로 변환
        string newJson = JsonConvert.SerializeObject(stageDate);

        // JSON 문자열을 데이터 폴더의 파일에 저장
        File.WriteAllText(persistentPath, newJson);
    }

    /// <summary>
    /// 현재 플레이중인 스테이지 이름을 바꾸는 메서드
    /// </summary>
    /// <param name="PlayStage"></param>
    public void ModifyPlayStage(string PlayStage)
    {
        string fileName = "PlayerDate";

        CheckFile(fileName);

        string persistentPath = Path.Combine(Application.persistentDataPath, fileName); // 데이터 폴더의 파일 경로

        // 파일에서 JSON 문자열 읽기
        string json = File.ReadAllText(persistentPath);

        // JSON 문자열을 객체로 변환
        StageClear stageDate = JsonConvert.DeserializeObject<StageClear>(json);

        // 속성 수정
        stageDate.playStage = PlayStage;

        // 객체를 다시 JSON 문자열로 변환
        string newJson = JsonConvert.SerializeObject(stageDate);

        // JSON 문자열을 데이터 폴더의 파일에 저장
        File.WriteAllText(persistentPath, newJson);
    }
    /// <summary>
    /// 마지막 체크포인트 위치를 저장하는 메서드
    /// </summary>
    /// <param name="vector3"></param>
    public void ModifyLastCheckPoint(Vector3 vector3)
    {
        string fileName = "PlayerDate";

        CheckFile(fileName);

        string persistentPath = Path.Combine(Application.persistentDataPath, fileName); // 데이터 폴더의 파일 경로

        // 파일에서 JSON 문자열 읽기
        string json = File.ReadAllText(persistentPath);

        // JSON 문자열을 객체로 변환
        StageClear stageDate = JsonConvert.DeserializeObject<StageClear>(json);

        // 속성 수정
        stageDate.lastCheckPoint = new PlayerVector3 { x = vector3.x, y = vector3.y, z = vector3.z };

        // 객체를 다시 JSON 문자열로 변환
        string newJson = JsonConvert.SerializeObject(stageDate);

        // JSON 문자열을 데이터 폴더의 파일에 저장
        File.WriteAllText(persistentPath, newJson);
    }

    /// <summary>
    /// 마지막에 있던 위치를 저장하는 메서드
    /// </summary>
    /// <param name="vector3"></param>
    public void ModifyNowPlayerPoint(Vector3 vector3)
    {
        string fileName = "PlayerDate";

        CheckFile(fileName);

        string persistentPath = Path.Combine(Application.persistentDataPath, fileName); // 데이터 폴더의 파일 경로

        // 파일에서 JSON 문자열 읽기
        string json = File.ReadAllText(persistentPath);

        // JSON 문자열을 객체로 변환
        StageClear stageDate = JsonConvert.DeserializeObject<StageClear>(json);

        // 속성 수정
        stageDate.nowPlayerPoint = new PlayerVector3 { x = vector3.x, y = vector3.y, z = vector3.z };

        // 객체를 다시 JSON 문자열로 변환
        string newJson = JsonConvert.SerializeObject(stageDate);

        // JSON 문자열을 데이터 폴더의 파일에 저장
        File.WriteAllText(persistentPath, newJson);
    }
    public class StageClear
    {
        //현재 클리어한 스테이지 수
        public int stageClear;
        //이어하기시 플레이중인 스테이지 이름
        public string playStage;
        //이어하기시 마지막에 저장된 체크포인트
        public PlayerVector3 lastCheckPoint;
        //이어하기시 플레이어가 최종으로 있던 위치
        public PlayerVector3 nowPlayerPoint;
        //이어할 데이터가 있는지 확인 초기값 flase
        public bool isPlaying;

        //lastCheckPoint값을 벡터로 바꿔 가져오는 메서드
        public Vector3 LastCheckPointToVector3()
        {
            return new Vector3(lastCheckPoint.x, lastCheckPoint.y, lastCheckPoint.z);
        }
        //nowPlayerPoint값을 벡터로 바꿔 가져오는 메서드
        public Vector3 NowPlayerPointToVector3()
        {
            return new Vector3(nowPlayerPoint.x, nowPlayerPoint.y, nowPlayerPoint.z);
        }
    }

    public class PlayerVector3
    {
        public float x;
        public float y;
        public float z;
    }
}

This post is licensed under CC BY 4.0 by the author.