'unity 역직렬화'에 해당되는 글 1건

  1. 2019.11.07 11.07 Unity monster객체에 역직렬화 데이터와 모델 데이터 넣기.

11.07 Unity monster객체에 역직렬화 데이터와 모델 데이터 넣기.

C#/실습 2019. 11. 7. 17:51

예제:

코드:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class App : MonoBehaviour
{
    Ingame ingame;
    // Start is called before the first frame update
    void Start()
    {
        GameObject ingameGO = new GameObject("Ingame");
        this.ingame = ingameGO.AddComponent<Ingame>();
        ingame.StartGame();
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ingame : MonoBehaviour
{
    Chicken chicken;
    Monster monster;
    Hero hero;
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
 
    internal void StartGame()
    {
        GameObject heroGO = new GameObject("Hero");
        GameObject monsterGO = new GameObject("Monster");
        GameObject chickenGO = new GameObject("Chicken");
 
        this.chicken = chickenGO.AddComponent<Chicken>();
        this.monster = monsterGO.AddComponent<Monster>();
        this.hero = heroGO.AddComponent<Hero>();
 
        //info 받아오기
        TextAsset asset = Resources.Load<TextAsset>("Data/character_data2"); //프로젝트에 존재하는 모든 파일을 어셋이라고 한다.
        CharacterData[] characterDatas = JsonConvert.DeserializeObject<CharacterData[]>(asset.text);
        Dictionary<int, CharacterData> dicCharacterDatas = new Dictionary<int, CharacterData>();
        foreach (var data in characterDatas)
        {
        }
        var characterdata = dicCharacterDatas[100];
 
        CharacterInfo characterInfo = new CharacterInfo(characterdata.id, characterdata.name, characterdata.hp, characterdata.damage);
 
        //model 가져오기
        string path = string.Format("Prefabs/{0}", characterdata.prefab_name);
        GameObject prefabCyclopes = Resources.Load<GameObject>(path);
        GameObject modelCyclopes = Instantiate(prefabCyclopes);
        modelCyclopes.transform.SetParent(heroGO.transform, false);
        modelCyclopes.transform.localPosition = Vector3.zero;
 
        Debug.LogFormat("이름: {0} 체력: {1}  공격력: {2}"hero.info.name, hero.info.hp, hero.info.damage);
 
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
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 Hero : MonoBehaviour
{
    public CharacterInfo info;
    private Vector3 Pos;
    GameObject model;
 
    public void Init(CharacterInfo info, GameObject model, Vector3 pos)
    {
        this.info = info;
        this.Pos = pos;
        this.model = model;
    }
    // Start is called before the first frame update
    void Start()
    {
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CharacterData 
{
    public int id;
    public string name;
    public string prefab_name;
    public int hp;
    public int damage;
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class CharacterInfo
{
    private int id;
    public string name;
    public int hp;
    public int damage;
    public int ID { get { return this.id; } }
    public CharacterInfo(int id, string name, int hp, int damage)
    {
        this.id = id;
        this.name = name;
        this.hp = hp;
        this.damage = damage;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

json 파일:
[
  {
    "id": 100,
    "name": "Cyclopes",
    "prefab_name": "Boximon Cyclopes",
    "hp": 100,
    "damage": 3
  },
  {
    "id": 200,
    "name": "Fiery",
    "prefab_name": "Boximon Fiery",
    "hp": 95,
    "damage": 4
  }
]

: