유니티 심화

SaveAndLoad - UISlot

다모아 2023. 9. 1. 18:13

1. UISlot
1-1. Init 만들어서 id 받고 id 저장해두기
1-2 

UISlot으로 뭘 하려고 만든거지?
--> HeroData에서 id를 전달받고 전해줘서 UISlot에다가 저장시켜놓는다
>> 왜?
--> UISlot은 UISlot Sciprts가 관리하는게 더 괜찮아보여서?

HeroDatas를 List로 초기화 하는건 dictionary.Values를 배열로 받아야해서 

2. App
2-1. UISlot[] for문으로
2-2. 


App

using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.U2D;
using System.Linq;

public class App : MonoBehaviour
{
    //프리팹이 저장될 변수
    [SerializeField]
    private List<GameObject> prefabs = new List<GameObject>();

    [SerializeField]
    private Button[] btns;

    [SerializeField]
    private Image thumb;

    [SerializeField]
    private SpriteAtlas atlas;
    //UISlot 할당 받아주고
    [SerializeField]
    private UISlot[] uiSlot;

    private void Start()
    {
        //데이터 로드
       DataManager.instance.LoadHeroData();

        //프리팹 리소스 로드
        this.LoadPrefabs();

        //foreach(Button btn in btns)
        //{
        //    btn.onClick.AddListener(() => {
        //        Text text = btn.gameObject.GetComponentInChildren<Text>();
        //        Debug.Log(text.text);
        //        int index = Convert.ToInt32(text.text) - 100;
        //        GameObject prefab = this.prefabs[index];
        //        GameObject heroGo = new GameObject();
        //        heroGo.name = "Hero";
        //        Instantiate(prefab, heroGo.transform);
        //    });
        //}
        //this.thumb.sprite = this.atlas.GetSprite("thumb_alien");

        //HeroData에서 쓸 데이터 가져오기 할당 받기
        List<HeroData> heroDatas = DataManager.instance.HeroDatas();
        //for문으로 heroDatas만큼
        for(int i = 0; i < heroDatas.Count; i++)
        {
            //이걸 사용하는 이유가 뭔데?? ---> slot.Init 사용해서 아이디 저장해두기
            UISlot slot = this.uiSlot[i];
            //데이터 옮기기 --> 옮겨서 뭐하려고 --> id 받아오게, sprite_name도 받아올 수 있음
            HeroData data = heroDatas[i];
            Sprite sp = this.atlas.GetSprite(data.sprite_name);
            slot.Init(data.id, sp);
        }
        
    }

    private void LoadPrefabs()
    {
        List<string> heroPrefabNames = DataManager.instance.GetHeroPrefabNames();

        foreach(string prefabName in heroPrefabNames)
        {
            string path = string.Format("Prefabs/{0}", prefabName);
            Debug.Log(path);
            GameObject prefab = Resources.Load<GameObject>(path);
            this.prefabs.Add(prefab);
            Debug.LogFormat("프리팹들이 로드 되었습니다. count: {0}", this.prefabs.Count);
        }
    }
}

 

UISlot

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class UISlot : MonoBehaviour
{
    private int id;
    private Sprite sp;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    public void Init(int id, Sprite sp)
    {
        this.id = id;
        this.sp = sp;
    }
}

 

DataManager

using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class DataManager
{
    public static readonly DataManager instance = new DataManager();

    private Dictionary<int, HeroData> dicHeroDatas = new Dictionary<int, HeroData>();

    //생성자
    private DataManager()
    {

    }

    public void LoadHeroData()
    {
        TextAsset asset = Resources.Load<TextAsset>("Data/hero_data");
        Debug.Log(asset.text); //json 문자열

        //역직렬화
        HeroData[] heroDatas = JsonConvert.DeserializeObject<HeroData[]>(asset.text);
        foreach (HeroData data in heroDatas)
        {
            Debug.LogFormat("{0}, {1}, {2}, {3}, {4}, {5}", data.id, data.name, data.max_hp, data.damage, data.prefab_name, data.sprite_name);
            this.dicHeroDatas.Add(data.id, data); // 사전에 추가
        }

        Debug.LogFormat("<color=yellow>HeroData 로드 완료 : {0}</color>", this.dicHeroDatas.Count);
    }

    public List<string> GetHeroPrefabNames()
    {
        List<string> list = new List<string>();
         foreach(HeroData data in this.dicHeroDatas.Values)
        {
            list.Add(data.prefab_name);
        }
        return list;
    }

    //정보 전달
    public List<HeroData> HeroDatas()
    {
        return this.dicHeroDatas.Values.ToList();
    }
}

 

'유니티 심화' 카테고리의 다른 글

LearnUGUI - Test02 [UIPopupName]  (0) 2023.09.05
LearnUGUI  (0) 2023.09.04
SaveAndLoad - 캐릭터 프리팹 인스턴스화하기  (0) 2023.09.01
SaveAndLoad  (0) 2023.09.01
조이스틱 인식오류  (0) 2023.09.01