[Unity] 2020.02.18 포트폴리오 작업내용

APP 2020. 2. 19. 10:49

유니티의 특정 폴더에 데이터를 불러오기/저장하는 기능 구현

 

application.persistentdatapath에 user_info.json파일을 저장/불러오기 하는 기능으로

 

게임이 1회 돌아가면 골드의 양이 반영되어 로컬드라이브에 남도록 하였음

 

 

 

 

테스트용 스크립트 TestSaveData.cs

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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
public class TestSaveData : MonoBehaviour
{
    public string path;
    private UserInfo userInfo;
 
    public UIButton btnLoad;
    public UIButton btnChangeInfo;
    public UIButton btnSave;
 
    public UILabel uId;
    public UILabel userName;
    public UILabel heroDamageLv;
    public UILabel heroAtkSpeedLv;
 
    void Start()
    {
        path = $"{Application.persistentDataPath}/user_info.json";
        Debug.Log(path);
 
        this.btnLoad.onClick.Add(new EventDelegate(() =>
        {
            if (File.Exists(path))
            {
                string json = File.ReadAllText(path);
                userInfo = JsonConvert.DeserializeObject<UserInfo>(json);
            }
            else
            {
                userInfo = new UserInfo();
                Debug.Log("저장된 파일이 없습니다");
            }
            ShowInfo();
        }));
 
        this.btnChangeInfo.onClick.Add(new EventDelegate(() =>
        {
            ChangeInfo();
        }));
 
        this.btnSave.onClick.Add(new EventDelegate(() =>
        {
            string json = JsonConvert.SerializeObject(userInfo);
 
            File.WriteAllText(path, json);
        }));
 
    }
    public void GetMessage()
    {
 
    }
    private void ShowInfo()
    {
        uId.text = userInfo.uId;
        userName.text = userInfo.userName;
        heroDamageLv.text = userInfo.heroDamageLv.ToString();
        heroAtkSpeedLv.text = userInfo.heroAtkSpeedLv.ToString();
    }
    private void ChangeInfo()
    {
        userInfo.uId = uId.text;
        userInfo.userName = userName.text;
        
        if(int.TryParse(heroDamageLv.text, out userInfo.heroDamageLv))
        {
            Debug.Log("heroDamageLv 정상입력");
        }
        else
        {
            Debug.Log("heroDamageLv 비정상");
        }
        if (int.TryParse(heroAtkSpeedLv.text, out userInfo.heroAtkSpeedLv))
        {
            Debug.Log("heroAtkSpeedLv 정상입력");
        }
        else
        {
            Debug.Log("heroAtkSpeedLv 비정상");
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
: