[Unity] 2020.01.03 NGUI 캐릭터Info, Slider, Label, Json 로드 및 저장

APP 2020. 1. 3. 16:58

 

NGUI_HeroInfo.unitypackage
2.04MB

 

Test3.cs

메인스크립트

uILabel과 uIButton은 레벨을 바꿔서 저장해보기 위해서 만들었다.

 

Awake에서 json파일을 로드해서 딕셔너리에 넣는다.

 

Start에서 uIHeroInfo.Init(userInfo);를 사용해서 아바타,레벨,슬라이더를 변경한다.

버튼을 클릭하면 uiLabel에 있는 숫자가 해당 userInfo에 저장되어 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Test3 : MonoBehaviour
{
    public UIHeroInfo uIHeroInfo;
    public UILabel uILabel;
    public UIButton uIButton;
    private void Awake()
    {
        DataLoader.GetInstance().LoadIconNameData();
        DataLoader.GetInstance().LoadUserInfo();
    }
 
    private void Start()
    {
        var userInfo = DataLoader.GetInstance().dicUserInfo[100];
 
        this.uIButton.onClick.Add(new EventDelegate(() =>
        {
            userInfo.level = int.Parse(uILabel.text);
            DataLoader.GetInstance().SaveUserInfo(userInfo);
        }));
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

UIHeroInfo.cs

userInfo를 받아서 자신의 UI를 변경한다.

 

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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class UIHeroInfo : MonoBehaviour
{
    public UISprite thumb;
    public UILabel lbLevel;
    public UISlider sliderHp;
    public UISlider sliderXp;
    public user_info userInfo;    
 
    public void Init(user_info userInfo)
    {
        this.userInfo = userInfo;
 
        UpdateUIHeroInfo(userInfo);       
    }
    private void UpdateUIHeroInfo(user_info user_Info)
    {
        var iconNameData = DataLoader.GetInstance().dicIconNameData[userInfo.thumb_num];
        var thumbName = iconNameData.icon_name;
 
 
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
 

 DataLoader.cs

딕셔너리를 모든 곳에서 불러서 사용하기 위한 싱글톤 객체이다.

Load로 시작하는 메서드는 딕셔너리에 json파일의 내용을 객체화해서 담는다.

 

Save는 변경된  dictionary의 내용을 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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.IO;
using System.Text;
public class DataLoader
{
    private static DataLoader Instance;
 
    public Dictionary<int, icon_name_data> dicIconNameData;
    public Dictionary<int, user_info> dicUserInfo;
 
    public static DataLoader GetInstance()
    {
        if (DataLoader.Instance == null)
        {
            DataLoader.Instance = new DataLoader();
        }
        return DataLoader.Instance;
    }
    public void LoadIconNameData()
    {
        string path = "Data/icon_name_data";
        string json = Resources.Load<TextAsset>(path).text;
        var arr = JsonConvert.DeserializeObject<icon_name_data[]>(json);
        dicIconNameData = new Dictionary<int, icon_name_data>();
        foreach(var data in arr)
        {
            dicIconNameData.Add(data.id, data);
        }
    }
 
    public void LoadUserInfo()
    {
        string path = "Info/user_info";
        string json = Resources.Load<TextAsset>(path).text;
        var arr = JsonConvert.DeserializeObject<user_info[]>(json);
        dicUserInfo = new Dictionary<int, user_info>();
        foreach (var data in arr)
        {
            dicUserInfo.Add(data.id, data);
        }
    }
    public void SaveUserInfo(user_info userInfo)
    {
        if(dicUserInfo[userInfo.id]!=null)
        {
            dicUserInfo[userInfo.id] = userInfo;
        }
        else
        {
            dicUserInfo[userInfo.id] = userInfo;
        }
        var jArray = new JArray();
        foreach (var kv in dicUserInfo)
        {
            JObject o = JObject.FromObject(kv.Value);
            jArray.Add(o);
        }
        string path = @"C:\Workspace\Unity\NGUI\Assets\Resources\Info\user_info.json";
 
        File.WriteAllText(path, JsonConvert.SerializeObject(jArray));
    }
 
 
    public icon_name_data GetIconNameData(int id)
    {
        return this.dicIconNameData[id];
    }
    public user_info GetUserInfo(int id)
    {
        return this.dicUserInfo[id];
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

icon_name_data.cs

유저 아이콘의 이름이 들어있는 데이터를 담는 클래스

1
2
3
4
5
6
7
8
9
10
11
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class icon_name_data
{
    public int id;
    public string name;
    public string icon_name;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

user_info.cs

유저의 정보가 들어있는 클래스 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class user_info 
{
    public int id;
    public int thumb_num;
    public string name;
    public int level;
    public float maxHp;
    public float hp;
    public float maxXp;
    public float xp;
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

 

 

 

 

 

 

 

 

: