Unity/과제
유니티 게임시작화면관리하기
yangjae
2019. 4. 16. 03:29
(Build Setting 할 때 주의할 점 : 순서대로 삽입해라)
(Resources폴더를 만들고 Resources.Load함수를 이용해 접근이 가능)
결과화면
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharacterAnimData : MonoBehaviour { public int id; public string idle_anim_name; public string run_anim_name; public string attack_anim_name; public string hit_anim_name; } | cs |
(CharacterAnimData)
1 2 3 4 5 6 7 8 9 10 11 12 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class CharacterData { public int id; public string name; public string prefab_name; public int anim_data_id; } | cs |
(CharacterData)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | using System.Collections; using System.Collections.Generic; using UnityEngine; public partial class App : MonoBehaviour { public enum eSceneType { None = -1, App, Logo, Title, InGame, DataLoad } } | cs |
(App)
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public partial class App : MonoBehaviour { private AsyncOperation asyncOperation; private eSceneType sceneType; public Camera uiCamera; private void Awake() { this.sceneType = eSceneType.None; Object.DontDestroyOnLoad(this); // 이 씬에있는 Object는 삭제되지않는다 } void Start() { this.LoadScene(eSceneType.Logo); } public void LoadScene(eSceneType sceneType) { if (this.sceneType != sceneType) { this.sceneType = sceneType; asyncOperation = SceneManager.LoadSceneAsync((int)sceneType); asyncOperation.completed += (go) => { switch (this.sceneType) { case eSceneType.Logo: { this.LoadScene(eSceneType.Title); } break; case eSceneType.Title: { var title = GameObject.FindObjectOfType<Title>(); title.OnClicButton = () => { this.LoadScene(eSceneType.DataLoad); }; title.ClicButton(); } break; case eSceneType.DataLoad: { this.LoadScene(eSceneType.InGame); } break; case eSceneType.InGame: { var inGame = GameObject.FindObjectOfType<InGame>(); inGame.Init(); } break; } }; } } } | cs |
(App)
1 2 3 4 5 6 7 8 9 10 11 12 13 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class DataLoad : MonoBehaviour { private void Start() { Debug.Log("DataLoad 실행"); DataManager.GetInstance().LoadDatas(); } } | cs |
(DataLoad)
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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using Newtonsoft.Json; public class DataManager { private static DataManager Instance; public Dictionary<int, CharacterData> dicCharacterData; public Dictionary<int, CharacterAnimData> dicCharacterAnimData; private DataManager() { this.dicCharacterData = new Dictionary<int, CharacterData>(); this.dicCharacterAnimData = new Dictionary<int, CharacterAnimData>(); } public static DataManager GetInstance() { if(DataManager.Instance == null) { DataManager.Instance = new DataManager(); } return DataManager.Instance; } public void LoadDatas() { var path = "Datas/character_data"; var textAsset = Resources.Load<TextAsset>(path); var json = textAsset.text; var arrCharacterData = JsonConvert.DeserializeObject<CharacterData[]>(json); foreach(var data in arrCharacterData) { dicCharacterData.Add(data.id, data); } } public CharacterData GetCharacterDataById(int id) { return this.dicCharacterData[id]; } } | cs |
(DataManager)
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; public class InGame : MonoBehaviour { private CharacterInfo info; void Start() { Debug.Log("InGame실행"); } public void Init() { var heroData = DataManager.GetInstance().dicCharacterData[0]; Debug.LogFormat("{0}이 생성되었습니다", heroData.name); var monsterData = DataManager.GetInstance().dicCharacterData[1]; Debug.LogFormat("{0}가 생성되었습니다", monsterData.name); } } | 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 | using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System; public class Title : MonoBehaviour { public Button btn; public Action OnClicButton; void Start() { Debug.Log("Title실행"); } public void ClicButton() { btn.onClick.AddListener(() => { OnClicButton(); }); } } | cs |
(Title)