ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 2020-05-11 [unity 복습]
    C#/과제 2020. 5. 12. 00:57

    1. delgate를 배웠다지만 이걸 어디서 써서 활용해야할지 집에서는 감이 안잡혔음 

    2. 우선 객체를 만드는것만 해봄 Clone은 정상적으로 만드는데까지는 이루어졌음 

     

     

     

    Lobby 코드 > 

    더보기
    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.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
     
    public class Lobby : MonoBehaviour
    {
        public Button button;
     
        private Hero hero;
        private DataManager dataManager;
        // Start is called before the first frame update
        void Start()
        {
            this.dataManager = DataManager.GetInstance();
            this.dataManager.LoadDatas();
     
            button.onClick.AddListener(() => {
     
                CreateHero(100);
                
            });
     
     
        }
        public Hero CreateHero(int id)
        {
            var data = this.dataManager.GetDatasById(id);
            var path = string.Format("Prefabs/{0}", data.res_name);
            Debug.Log(path);
     
            var hero = Resources.Load("Prefabs/Hero"as GameObject;
            var heroGO = Instantiate<GameObject>(hero);
     
            var heroprefab = Resources.Load(path) as GameObject;
            var heroprefabGO = Instantiate<GameObject>(heroprefab, Vector3.zero, transform.rotation);
     
     
            this.hero = heroprefab.AddComponent<Hero>();
            heroprefabGO.transform.SetParent(heroGO.transform);
     
            this.hero.id = data.id;
            this.hero.heroname = data.name;
            this.hero.res_name = data.res_name;
            this.hero.hp = data.hp;
            this.hero.damage = data.damage;
            this.hero.speed = data.speed;
     
            return this.hero;
        }
     
        // Update is called once per frame
        void Update()
        {
            
        }
    }
     
    cs

     

     

    DataManager > 

    더보기
    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
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using Newtonsoft.Json;
    using System.Linq;
     
    public class DataManager 
    {
     
        public static DataManager Instance;
        public Dictionary<int, HeroData> dichero;
     
        private DataManager()
        {
            this.dichero = new Dictionary<int, HeroData>();
        }
     
        public static DataManager GetInstance()
        {
            if (DataManager.Instance == null)
            {
                DataManager.Instance = new DataManager();
            }
             return DataManager.Instance;
        }
     
        public void LoadDatas()
        {
            TextAsset textAsset = Resources.Load("Data/character_data") as TextAsset;
            string json = textAsset.text;
     
            this.dichero = JsonConvert.DeserializeObject<HeroData[]>(json).ToDictionary(x => x.id);
     
        }
     
        public HeroData GetDatasById(int id)
        {
            return this.dichero[id];
        }
    }
     
    cs

     

    Hero > 

    더보기
    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
    using System.Collections;
    using System.Collections.Generic;
    using System.Data;
    using System.Runtime.InteropServices.WindowsRuntime;
    using UnityEngine;
    using UnityEngine.UI;
     
    public class Hero : MonoBehaviour
    {
       
     
        public int id;
        public string heroname;
        public string res_name;
        public int hp;
        public int damage;
        public float speed;
        // Start is called before the first frame update
        void Start()
        {
     
     
     
        }
     
     
        // Update is called once per frame
        void Update()
        {
            
        }
    }
     
    cs

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
     
    public class HeroData 
    {
        public int id;
        public string name;
        public string res_name;
        public int hp;
        public int damage;
        public float speed;
      
    }
     
    cs

     

Designed by Tistory.