본문 바로가기

정리 카테고리/실습_과제

Unity_실습_케릭터 선택과 씬 변경


-데이터를 불러온후 버튼으로 출력

-선택한 버튼에 맞추어 씬을 바꾸고 난후 케릭터 출력




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;
using UnityEngine.SceneManagement;
public class Game : MonoBehaviour {
    private int heroid;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        
    }
    public void Init(int heroid)
    {
        this.heroid = heroid;
        Debug.Log(this.heroid);
        //App 오브젝트에서 정보 가져오기
        var app = GameObject.Find("App").GetComponent<App>();
        var heroData = app.dicHero[heroid];
        var obj = Resources.Load(heroData.assetName) as GameObject;
        var obji = Instantiate(obj);
    }
 
    
}
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Title : MonoBehaviour {
    public List<HeroData> heroData = new List<HeroData>();
    private App app;
    private int selectedCharacterId = 0;
    void Start () {
        this.app = GameObject.Find("App").GetComponent<App>();
        var heroData = app.dicHero[100];
        Debug.Log(heroData.id + heroData.name);
    }
 
    public void Init(List<HeroData> heroData)
    {
        this.heroData = heroData;
    }
    // Update is called once per frame
    void Update () {
        
    }
    private void OnGUI()
    {
        for (int i = 0; i < this.heroData.Count; i++)
        {
            //
            var data = this.heroData[i];
            if (GUI.Button(new Rect(i * 100010080), data.name))
            {
                Debug.Log("<color=red>" + data.id + data.name + "</color>");
                selectedCharacterId = data.id;
                this.app.LoadGameScene(data.id);
                SceneManager.LoadScene("Game");
 
            }
        }
    }
}
 
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
56
57
58
59
60
61
62
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
 
public class App : MonoBehaviour {
    public Dictionary<int, HeroData> dicHero;
    private List<HeroData> heroList;
    private void Awake()
    {
        GameObject.DontDestroyOnLoad(this);
    }
    void Start () {
        //데이터 시트 로드
        dicHero = new Dictionary<int, HeroData>();
        heroList = new List<HeroData>();
        var asset = Resources.Load("Data/hero_data"as TextAsset;
        var heroDataList = JsonConvert.DeserializeObject<List<HeroData>>(asset.text);
       
 
        foreach (var data in heroDataList) this.dicHero.Add(data.id, data);
        foreach (var pair in dicHero) this.heroList.Add(pair.Value);
        //씬전환
        LoadTitleScene();
        SceneManager.LoadScene("Title");
    }
    void LoadTitleScene()
    {
        StartCoroutine(LoadTitleSceneAsync());
    }
    IEnumerator LoadTitleSceneAsync()
    {
        AsyncOperation oper = SceneManager.LoadSceneAsync("Title");
        yield return oper.isDone;
        var title = GameObject.Find("Title").GetComponent<Title>();
        Debug.Log(title);
        title.Init(heroList);
 
    }
    public void LoadGameScene(int heroid)
    {
        StartCoroutine(LoadGameSceneAsync(heroid));
    }
    IEnumerator LoadGameSceneAsync(int heroid)
    {
        AsyncOperation oper = SceneManager.LoadSceneAsync("Game");
        yield return oper.isDone;
        //yield return new WaitUntil(() => oper.isDone);//무명메서드와 람다식
        var game = GameObject.Find("Game").GetComponent<Game>();
        Debug.Log(game);
        game.Init(heroid);
 
    }
 
 
    void Update () {
        
    }
}
 
cs