2.Unity Info관리

개발 일지/Move Cube 2020. 8. 17. 15:36

1.UserInfo

1
2
3
4
5
6
7
8
9
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class UserInfo
{
    public int stageLevel;
}
 
cs


2.InfoManager

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
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using Newtonsoft.Json;
 
public class InfoManager
{
    private static InfoManager instance;
    private string path = "/Info";
 
    //Info Dictionary
    private UserInfo userInfo;
 
    private InfoManager()
    {
        this.userInfo = new UserInfo();
    }
 
    public static InfoManager GetInstance()
    {
        if (InfoManager.instance == null)
        {
            InfoManager.instance = new InfoManager();
        }
 
        return InfoManager.instance;
    }
 
    #region 유저인포 로드
    public void LoadInfo()
    {
        this.LoadInfoImpl();
    }
    
    private void LoadInfoImpl()
    {
        var checkDirectory = Directory.Exists(Application.persistentDataPath + path);
 
        if (checkDirectory)
        {
            //persistentDataPath에 폴더가 존재하면
            var checkInfo = File.Exists(Application.persistentDataPath + path + "/userInfo.json");
            if (checkInfo)
            {
                //userInfo.json 파일이 존재하면
                Debug.Log("기존");
                Debug.Log(Application.persistentDataPath + path + "/userInfo.json");
                var text = File.ReadAllText(Application.persistentDataPath + path + "/userInfo.json");
                this.userInfo = JsonConvert.DeserializeObject<UserInfo>(text);
            }
            else
            {
                //userInfo.json 파일이 존재하지 않으면
                Debug.Log("신규");
                this.SetNewUserInfo();
                this.SaveUserInfo(false);
            }
        }
        else
        {
            //persistentDataPath에 폴더가 존재하지 않으면 + 폴더가 없으니 파일도 존재하지 않음
            Debug.Log("신규");
            Directory.CreateDirectory(Application.persistentDataPath + path);//폴더 생성
            this.SetNewUserInfo();
            this.SaveUserInfo(false);
        }
    }
    #endregion
 
    #region 신규유저 인포 세팅
    private void SetNewUserInfo()
    {
        this.userInfo = new UserInfo();
        //새로하기 선택시 세팅값 설정
        this.userInfo.stageLevel = 1;
    }
    #endregion
 
    #region 유저인포 리턴
    public UserInfo GetUserInfo()
    {
        return this.userInfo;
    }
    #endregion
 
    #region 유저인포 저장
    public void SaveUserInfo(bool isLevelUp)
    {
        if (isLevelUp)
        {
            Debug.Log("레벨업!");
            this.userInfo.stageLevel++;
        }
        var json = JsonConvert.SerializeObject(this.userInfo);
        File.WriteAllText(Application.persistentDataPath + path + "/userInfo.json", json, System.Text.Encoding.UTF8);
        Debug.Log("Info저장 완료");
    }
    #endregion
}
 
cs


UserInfo에 다른 값 추가시 ( 2개 이상의 값 ) ID를 통해 Dictionary로 관리하기

'개발 일지 > Move Cube' 카테고리의 다른 글

6.Google Play Beta.ver//구글 플레이 베타버전 업로드  (0) 2020.08.17
5.Stage, Cube, CubeMove  (0) 2020.08.17
4.Title/StageSelect  (0) 2020.08.17
3.App -> Logo  (0) 2020.08.17
1.Unity GameSceneManager  (0) 2020.08.17
: