-
2020-05-11 [unity 복습]C#/과제 2020. 5. 12. 00:57
1. delgate를 배웠다지만 이걸 어디서 써서 활용해야할지 집에서는 감이 안잡혔음
2. 우선 객체를 만드는것만 해봄 Clone은 정상적으로 만드는데까지는 이루어졌음
Lobby 코드 >
더보기12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758using 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 updatevoid 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 framevoid Update(){}}cs DataManager >
더보기1234567891011121314151617181920212223242526272829303132333435363738394041using 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 >
더보기123456789101112131415161718192021222324252627282930313233using 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 updatevoid Start(){}// Update is called once per framevoid Update(){}}cs 123456789101112131415using 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 'C# > 과제' 카테고리의 다른 글
2020-05-12 [금일 수업 반복 / 구체 캐릭터 따라다니기, 왕복이동] (0) 2020.05.13 문제 3 / 문제 5 (0) 2020.04.26 2020-04-23 [JSON 활용 및 초반 과정 및 생각 정리] (0) 2020.04.23 객체지향 3요소 / 5 원칙 둘러보고 간단 서술 (0) 2020.04.23 2020-04-18/19 [1차원 배열 활용] (0) 2020.04.20