Post

KDT Unity 5주차 팀 프로젝트

2024-01-22 TIL

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
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class DateTest : MonoBehaviour
{
    void Start()
    {
        Write();
    }
    public void Write()
    {
        TextAsset contents = Main.Resource.GetResource<TextAsset>("PlayerDate.Json");
        StageClear stageClear = JsonConvert.DeserializeObject<StageClear>(contents.text);

        stageClear.stageClear = 10;
        stageClear.playStage = "Stage20";
        stageClear.lastCheckPoint = new PlayerVector3 { x = 10.0f, y = 10.0f, z = 10.0f };

        string jsonStr = JsonConvert.SerializeObject(stageClear);
        using (StreamWriter file = File.CreateText(contents.text))
        {
            //StreamWriter을 사용하여 json 작성
            file.Write(contents);
        }
        Debug.Log(jsonStr); // JSON 문자열을 출력합니다.
    }
    public void Lord()
    {
        TextAsset contents = Main.Resource.GetResource<TextAsset>("PlayerDate.Json");
        StageClear stageClearCheck = JsonConvert.DeserializeObject<StageClear>(contents.text);


        Debug.Log(stageClearCheck.stageClear);
        Debug.Log(stageClearCheck.playStage);
        Debug.Log(stageClearCheck.lastCheckPointToVector3());
    }
    public class StageClear
    {
        public int stageClear;
        public string playStage;
        public PlayerVector3 lastCheckPoint;
        public Vector3 lastCheckPointToVector3()
        {
            return new Vector3(lastCheckPoint.x, lastCheckPoint.y, lastCheckPoint.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.