2018.03.28 수업내용 (데이터 저장, 불러오기)

Unity3D 2018. 5. 15. 21:25
반응형
hero_data.xlsx
엑셀 -> json 변환 
hero_data.txt파일저장 (유니코드)
'Resources'폴더 생성 
Resources.Load(hero_data)후 형변환
매핑할 데이터클래스 생성 
hero_data <-> HeroData.cs
System [Serializable]
Json.Net for Unity 패키지 임포트
패키지 저장 폴더 위치 >
드라이브:\Users\계정명\AppData\Roaming\Unity\Asset Store
SD Citizen Character 리소스 임포트 
Resources.Load를 통해 프리팹을 메모리에 올림 
Character클래스 생성 
HeroData를 셋팅 할수 있는 메서드 생성 
케릭터 생성후 데이터 셋팅 
인스펙터에 데이터가 제대로 들어왔는지 확인을 위해 
필드위에 [SerializeField]를 써줌 




1
2
3
4
5
6
7
8
9
10
11
12
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
 
[Serializable]
public class HeroData {
    public int id;
    public string name;
    public string assetName;
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
 
public class JsonTest : MonoBehaviour {
 
    Dictionary<int, HeroData> dicHeroData;
 
    void Start () {
 
        this.dicHeroData = new Dictionary<int, HeroData>();
 
        LoadJson("hero_data");
 
        //딕셔너리에 데이터들이 들어갔음...
 
        LoadPrefab(dicHeroData[200]); 
    }
 
    void LoadPrefab(HeroData heroData)
    {
        //케릭터를 생성할 준비가 됨 
        var obj = Resources.Load(heroData.assetName) as GameObject;    //형변환 
 
        var go = GameObject.Instantiate(obj); //실제로 케릭터가 생성됨 
 
        Character character = go.AddComponent<Character>();
 
        character.SetData(heroData);
 
    }
 
    void LoadJson(string path)
    {
        //var asset = Resources.Load(path) as TextAsset;  //형변환 
        var asset = (TextAsset)Resources.Load(path);      //형변환
        Debug.Log(asset);
 
        //매핑 시작 
        //var arrHeroData = JsonConvert.DeserializeObject<HeroData[]>(asset.text);
        var heroDataList = JsonConvert.DeserializeObject<List<HeroData>>(asset.text);
        
        foreach (var heroData in heroDataList)
        {
            this.dicHeroData.Add(heroData.id, heroData);
        }
 
        Debug.Log("dicHeroData count: " + this.dicHeroData.Count);  //3
 
        Debug.Log(this.dicHeroData[200].assetName); //ch_02_01
 
    }
}
 
cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
//게임 오브젝트에 Assign할거기 때문에 MonoBehaviour를 상속 받게 함 
//임꺽정 데이터를 넣어 줄거임 
 
public class Character : MonoBehaviour {
 
    [SerializeField]
    private string characterName;
 
    private HeroData heroData;
    
    public void SetData(HeroData heroData)
    {
        this.heroData = heroData;
        this.characterName = this.heroData.name;
    }
}
 
cs


Character character = GetComponent<Character>();
Animation animation = GetComponent<Animation>();


animation.Play("walk@loop");

void OnGUI() <- 유니티 내장 메서드 

OnGUI안에서 버튼 생성하고 클릭해서 출력 하기 


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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
 
public class JsonTest : MonoBehaviour {
 
    Dictionary<int, HeroData> dicHeroData;
    List<string> characterAnimList;
    Character character;
 
    void Start () {
 
        this.dicHeroData = new Dictionary<int, HeroData>();
        this.characterAnimList = new List<string>();
 
        LoadJson("hero_data");
 
        //딕셔너리에 데이터들이 들어갔음...
 
        LoadPrefab(dicHeroData[200]); 
    }
 
    void LoadPrefab(HeroData heroData)
    {
        //케릭터를 생성할 준비가 됨 
        var obj = Resources.Load(heroData.assetName) as GameObject;    //형변환 
 
        var go = GameObject.Instantiate(obj); //실제로 케릭터가 생성됨 
 
        this.character = go.AddComponent<Character>();
 
        this.character.SetData(heroData);
 
        var anim = this.character.GetComponent<Animation>();
 
        foreach (AnimationState state in anim)
        {
            Debug.LogFormat("stage name : {0}", state.name);
            characterAnimList.Add(state.name);
        }
 
        PlayAnimation("walk@loop");
    }
 
    void PlayAnimation(string animName)
    {
        var anim = this.character.GetComponent<Animation>();
        anim.Play(animName);
    }
 
    void LoadJson(string path)
    {
        //var asset = Resources.Load(path) as TextAsset;  //형변환 
        var asset = (TextAsset)Resources.Load(path);      //형변환
        Debug.Log(asset);
 
        //매핑 시작 
        //var arrHeroData = JsonConvert.DeserializeObject<HeroData[]>(asset.text);
        var heroDataList = JsonConvert.DeserializeObject<List<HeroData>>(asset.text);
        
        foreach (var heroData in heroDataList)
        {
            this.dicHeroData.Add(heroData.id, heroData);
        }
 
        Debug.Log("dicHeroData count: " + this.dicHeroData.Count);  //3
 
        Debug.Log(this.dicHeroData[200].assetName); //ch_02_01
 
    }
 
    private void OnGUI()
    {
        foreach (var animName in this.characterAnimList)
        {
            if (GUILayout.Button(animName))
            {
                this.PlayAnimation(animName);
            }
        }
 
    }
}
 
cs






반응형
: