카테고리 없음

2018.04.05 수업내용 (Json파싱, StreamReader, StreamWriter)

일등하이 2018. 5. 15. 21:40
반응형
App.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;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Newtonsoft.Json;
 
public class App : MonoBehaviour {
 
    public Dictionary<int, HeroData> dicHeroData;
 
    private void Awake()
    {
        var path = string.Format("{0}/hero_info.json", Application.persistentDataPath);
 
        Debug.Log(path);
 
        GameObject.DontDestroyOnLoad(this);
    }
 
 
    // Use this for initialization
    void Start ()
    {
        //데이터 시트 로드 
        this.dicHeroData = new Dictionary<int, HeroData>();
        var asset = Resources.Load("Data/hero_data"as TextAsset;
        var heroDataList = JsonConvert.DeserializeObject<List<HeroData>>(asset.text);
        foreach (var data in heroDataList)
        {
            this.dicHeroData.Add(data.id, data);
        }
        var heroData = this.dicHeroData[100];
        Debug.LogFormat("{0}, {1}", heroData.id, heroData.name);
        StartCoroutine(this.LoadTitleScene());
        
    }
 
    IEnumerator LoadTitleScene()
    {
        var oper = SceneManager.LoadSceneAsync("Title");
        while (true)
        {
            if (oper.isDone)
                break;
 
            yield return null;
        }
 
        var title = GameObject.Find("Title").GetComponent<Title>();
        title.Init(thisthis.dicHeroData);
    }
    
 
    // Update is called once per frame
    void Update () {
        
    }
 
    public void LoadGameScene(HeroInfo heroInfo)
    {
        StartCoroutine(LoadGameSceneAsync(heroInfo));
    }
 
    public void LoadGameScene(HeroData heroData)
    {
        StartCoroutine(LoadGameSceneAsync(heroData));
    }
 
    IEnumerator LoadGameSceneAsync(HeroData heroData)
    {
        AsyncOperation oper = SceneManager.LoadSceneAsync("Game");
        yield return new WaitUntil(() => oper.isDone);
        var game = GameObject.Find("Game").GetComponent<Game>();
        game.Init(heroData);
    }
 
    IEnumerator LoadGameSceneAsync(HeroInfo heroInfo)
    {
        AsyncOperation oper = SceneManager.LoadSceneAsync("Game");
        yield return new WaitUntil(() => oper.isDone);
        var game = GameObject.Find("Game").GetComponent<Game>();
        game.Init(heroInfo);
    }
 
}
 
cs


Game.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.IO;
using System.Text;
 
public class Game : MonoBehaviour
{
    private HeroData heroData;
    private HeroInfo heroInfo;
 
    public void Init(HeroData heroData)
    {
        //캐릭터 생성 
        this.heroData = heroData;
        this.heroInfo = new HeroInfo();
        this.heroInfo.id = this.heroData.id;
        var app = GameObject.Find("App").GetComponent<App>();
        var obj = Resources.Load("Prefab/" + this.heroData.assetName);
        var prefab = Instantiate(obj) as GameObject;
        Debug.Log(prefab);
    }
 
    public void Init(HeroInfo heroInfo)
    {
        //캐릭터 생성 
        this.heroInfo = heroInfo;
        var app = GameObject.Find("App").GetComponent<App>();
        this.heroData = app.dicHeroData[this.heroInfo.id];
        var obj = Resources.Load("Prefab/" + this.heroData.assetName);
        var prefab = Instantiate(obj) as GameObject;
        Debug.Log(prefab);
    }
 
    private void OnApplicationQuit()
    {
        var path = string.Format("{0}/hero_info.json", Application.persistentDataPath);
        var isNewbie = !File.Exists(path);
 
        Debug.Log("isNewbie: " + isNewbie);
 
        if (isNewbie)
        {
            var json2 = JsonConvert.SerializeObject(this.heroInfo);
            SaveData(path, json2);
        }
        else
        {
            StreamReader reader = new StreamReader(path, Encoding.Unicode);
            var json1 = reader.ReadToEnd();
            reader.Close();
 
            var json2 = JsonConvert.SerializeObject(this.heroInfo);
 
            Debug.LogFormat("{0}, {1}", json1.Length, json2.Length);
 
            var isEqual = string.Equals(json1, json2);
 
            Debug.Log("isEqual: " + isEqual);
 
            if (!isEqual)
            {
                SaveData(path, json2.Trim());
            }
            else
            {
                Debug.LogFormat("변경된 데이터가 없습니다.");
            }
           
        }
    }
 
    private void SaveData(string path, string json)
    {
        StreamWriter writer = new StreamWriter(path, falseEncoding.Unicode);
        writer.Write(json);
        writer.Close();
        Debug.LogFormat("데이터가 저장 되었습니다. {0}, {1}", path, json);
    }
    
}
 
cs


HeroData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
 
[Serializable]
public class HeroData  {
    //id    name    damage    hp    moveSpeed    assetName
    public int id;
    public string name;
    public int damage;
    public int hp;
    public float moveSpeed;
    public string assetName;
}
 
cs

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


Tile.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Newtonsoft.Json;
using System.IO;
using System.Text;
 
public class Title : MonoBehaviour {
 
    public List<HeroData> heroDataList;
    private App app;
    private bool isNewBie;
    private Dictionary<int, HeroData> dicHeroData;
 
    public void Init(App app, Dictionary<int, HeroData> dicHeroData)
    {
        this.app = app;
        this.dicHeroData = dicHeroData;
        this.heroDataList = new List<HeroData>();
 
        foreach (var pair in dicHeroData)
        {
            this.heroDataList.Add(pair.Value);
        }
 
        var path = string.Format("{0}/hero_info.json", Application.persistentDataPath);
        this.isNewBie = !File.Exists(path);
        if (this.isNewBie)
        {
 
        }
        else
        {
            StreamReader reader = new StreamReader(path, Encoding.Unicode);
            var json = reader.ReadToEnd();
            var heroInfo = JsonConvert.DeserializeObject<HeroInfo>(json);
            var heroData = this.dicHeroData[heroInfo.id];
            Debug.LogFormat("heroData: {0}, {1}", heroData.id, heroData.name);
            this.app.LoadGameScene(heroData);
        }
 
    }
 
    private void OnGUI()
    {
        if (this.isNewBie)
        {
            for (int i = 0; i < this.heroDataList.Count; i++)
            {
                var data = this.heroDataList[i];
                if (GUI.Button(new Rect(i * 110010080), data.name))
                {
                    Debug.LogFormat("data: {0}, {1}", data.id, data.name);
                    this.app.LoadGameScene(data);
                }
            }
        }
    }
}
 
cs



반응형