반응형

 

 

유니티를 이용하여 게임을 제작 시 유저의 데이터(점수, 위치)등을 저장해야 하는 경우가 있습니다.

 

그러기 위해서 Json이라는 형식을 이용하면 텍스트로 이루어져 있기 때문에 이해하기도 쉽고, 직렬화 / 역직렬화를 통해  

게임 내 데이터와 Json데이터의 상호 변환이 쉽게 가능합니다.

 

현재 제가 쓰는 2018.3.9 버전에서는 기본적으로 지원하기 때문에 따로 using 할 필요가 없습니다.

 

그럼 바로 실습을 해보겠습니다.

 

JsonTest라는 프로젝트를 하나 만들었습니다.

 

빈 오브젝트를 만듭니다.

 

Add Component를 눌러서 Script를 추가합니다.

스크립트를 더블클릭 합니다.

 

 

 

1. 코드를 확인합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
public class JsonTest : MonoBehaviour 
    // Start is called before the first frame update 
    void Start() 
    { 
         
    } 
 
    // Update is called once per frame 
    void Update() 
    { 
         
    } 
}
cs

이제 여기서 작업을 하겠습니다.

 

 

 

2. 클래스를 추가합니다. 

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
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
[System.Serializable] 
public class Data 
    public int m_nLevel; 
    public Vector3 m_vecPositon; 
 
    public void printData() 
    { 
        Debug.Log("Level : " + m_nLevel); 
        Debug.Log("Position : " + m_vecPositon); 
    } 
 
public class JsonTest : MonoBehaviour 
    // Start is called before the first frame update 
    void Start() 
    { 
         
    } 
 
    // Update is called once per frame 
    void Update() 
    { 
         
    } 
cs

 

 

 

 

3. 클래스 오브젝트를 생성하고, 데이터를 삽입합니다.

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
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
[System.Serializable] 
public class Data 
    public int m_nLevel; 
    public Vector3 m_vecPositon; 
 
    public void printData() 
    { 
        Debug.Log("Level : " + m_nLevel); 
        Debug.Log("Position : " + m_vecPositon); 
    } 
 
public class JsonTest : MonoBehaviour 
    // Start is called before the first frame update 
    void Start() 
    { 
        Data data = new Data(); 
        data.m_nLevel = 12
        data.m_vecPositon = new Vector3(3.4f, 5.6f, 7.8f); 
 
    } 
 
    // Update is called once per frame 
    void Update() 
    { 
         
    } 
}
cs

 

 

 

 

4. 오브젝트 데이터를 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
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
[System.Serializable] 
public class Data 
    public int m_nLevel; 
    public Vector3 m_vecPositon; 
 
    public void printData() 
    { 
        Debug.Log("Level : " + m_nLevel); 
        Debug.Log("Position : " + m_vecPositon); 
    } 
 
public class JsonTest : MonoBehaviour 
    // Start is called before the first frame update 
    void Start() 
    { 
        Data data = new Data(); 
        data.m_nLevel = 12
        data.m_vecPositon = new Vector3(3.4f, 5.6f, 7.8f); 
 
        string str = JsonUtility.ToJson(data); 
        
        Debug.Log("ToJson : " + str); 
    } 
 
    // Update is called once per frame 
    void Update() 
    { 
         
    } 
}
cs

-----------------------------------------------------------------------------------------

 

JsonUtility.ToJson함수를 이용하여 오브젝트를 json형식으로 변환합니다.

다음과 같이 실행됩니다.

 

 

 

5. 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
using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
 
[System.Serializable] 
public class Data 
    public int m_nLevel; 
    public Vector3 m_vecPositon; 
 
    public void printData() 
    { 
        Debug.Log("Level : " + m_nLevel); 
        Debug.Log("Position : " + m_vecPositon); 
    } 
 
public class JsonTest : MonoBehaviour 
    // Start is called before the first frame update 
    void Start() 
    { 
        Data data = new Data(); 
        data.m_nLevel = 12
        data.m_vecPositon = new Vector3(3.4f, 5.6f, 7.8f); 
 
        string str = JsonUtility.ToJson(data); 
 
        Debug.Log("ToJson : " + str); 
 
       Data data2 = JsonUtility.FromJson<Data>(str);
        data2.printData(); 
    } 
 
    // Update is called once per frame 
    void Update() 
    { 
         
    } 
 
 
cs

 

 

 

-----------------------------------------------------------------------------------------

 

JsonUtility.FromJson 함수로 오브젝트로 값을 넣습니다.

 

실행되면 결과는 다음과 같습니다.

 

 

 

 

6. 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
using System.Collections; 
using System.Collections.Generic; 
using System.IO; 
using UnityEngine; 
 
[System.Serializable] 
public class Data 
    public int m_nLevel; 
    public Vector3 m_vecPositon; 
 
    public void printData() 
    { 
        Debug.Log("Level : " + m_nLevel); 
        Debug.Log("Position : " + m_vecPositon); 
    } 
 
public class JsonTest : MonoBehaviour 
    // Start is called before the first frame update 
    void Start() 
    { 
        Data data = new Data(); 
        data.m_nLevel = 12
        data.m_vecPositon = new Vector3(3.4f, 5.6f, 7.8f); 
 
        string str = JsonUtility.ToJson(data); 
 
        Debug.Log("ToJson : " + str); 
 
       Data data2 = JsonUtility.FromJson<Data>(str);
        data2.printData(); 
 
        // file save 
 
        Data data3 = new Data(); 
        data3.m_nLevel = 99
        data3.m_vecPositon = new Vector3(8.1f, 9.2f, 7.2f); 
 
        
        File.WriteAllText(Application.dataPath + "/TestJson.json", JsonUtility.ToJson(data3)); 
    } 
 
    // Update is called once per frame 
    void Update() 
    { 
         
    } 
}
cs

 

Application.dataPath는 지금 작업 폴더를 말합니다.

실행을 하면 TestJson.json 파일이 생성됩니다.

 

 

 

 

 

7. 저장한 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
using System.Collections; 
using System.Collections.Generic; 
using System.IO; 
using UnityEngine; 
 
[System.Serializable] 
public class Data 
    public int m_nLevel; 
    public Vector3 m_vecPositon; 
 
    public void printData() 
    { 
        Debug.Log("Level : " + m_nLevel); 
        Debug.Log("Position : " + m_vecPositon); 
    } 
 
public class JsonTest : MonoBehaviour 
    // Start is called before the first frame update 
    void Start() 
    { 
        Data data = new Data(); 
        data.m_nLevel = 12
        data.m_vecPositon = new Vector3(3.4f, 5.6f, 7.8f); 
 
        string str = JsonUtility.ToJson(data); 
 
        Debug.Log("ToJson : " + str); 
 
       Data data2 = JsonUtility.FromJson<Data>(str);
        data2.printData(); 
 
        // file save 
 
        Data data3 = new Data(); 
        data3.m_nLevel = 99
        data3.m_vecPositon = new Vector3(8.1f, 9.2f, 7.2f); 
 
        File.WriteAllText(Application.dataPath + "/TestJson.json", JsonUtility.ToJson(data3)); 
 
        // file load 
 
        string str2 = File.ReadAllText(Application.dataPath + "/TestJson.json"); 
 
       Data data4 = JsonUtility.FromJson<Data>(str2);
        data4.printData(); 
    } 
 
    // Update is called once per frame 
    void Update() 
    { 
         
    } 
}
cs

저장한 파일을 로드합니다.

그 후 콘솔에서 출력합니다.

 

반응형

+ Recent posts