지금 내가 느끼는게 ScrollView나 DataManager나 스크립트들이 무슨 역할을 하는지 잘 모르는거 같다.
..잘 모르겠어서 복습을 마저했는데 계속 해봐야할 것 같다.
메모장에 적어서 생각 정리를 좀 해야할 것 같다.
이 부분 어떻게 추가했는지 잘 몰랐다가 그냥 내가 + 버튼을 눌러서 UIAtlas를 입력해주면 알아서 AtlasManager가 Text를 읽고 경로를 찾아가는 것 같다.
AtlasManager
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class AtlasManager : MonoBehaviour
{
[SerializeField]
private string[] arrAtlasNames;
private Dictionary<string, SpriteAtlas> dicAtlases = new Dictionary<string, SpriteAtlas>();
public static AtlasManager instance;
private void Awake()
{
if (instance != null && instance != this)
{
Destroy(this);
throw new System.Exception("An instance of this singleton already exists.");
}
else
{
instance = this;
}
DontDestroyOnLoad(this.gameObject);
}
public void LoadAtlases()
{
foreach (string atlasName in this.arrAtlasNames)
{
SpriteAtlas atlas = Resources.Load<SpriteAtlas>(atlasName);
this.dicAtlases.Add(atlasName, atlas);
}
Debug.LogFormat("{0}개의 아틀라스를 로드 했습니다.", this.dicAtlases.Count);
}
public SpriteAtlas GetAtlas(string atlasName)
{
return this.dicAtlases[atlasName];
}
}
Test05UIChestCellAd
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Test05UIChestCellAd : Test05UIChestCell
{
//광고버튼
[SerializeField]
private Button btnAd;
public Action onClickAd;
public override void Init(ChestData data)
{
base.Init(data);
Debug.LogFormat("[UIChestCellAd] Init : {0}", this.chestType);
this.btnAd.onClick.AddListener(() => {
Debug.LogFormat("{0}, 광고보기", this.chestType);
this.onClickAd();
});
}
}
Test05UIChestCell
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;
public class Test05UIChestCell : MonoBehaviour
{
//상자타입
public enum eChestType
{
Wooden, Silver, Golden, Epic, Legendary
}
//구매버튼
[SerializeField]
protected Button btnBuy;
[SerializeField]
protected TMP_Text txtPrice;
[SerializeField]
protected TMP_Text txtName;
[SerializeField]
private Image icon;
//상자타입
[SerializeField]
protected eChestType chestType;
public eChestType ChestType
{
get
{
return this.chestType;
}
}
//가격
[SerializeField]
protected int price;
public int Price => this.price;
public Action onClickPrice;
public virtual void Init(ChestData data)
{
Debug.LogFormat("[Test05UIChestCell] Init : {0}", this.chestType);
this.price = data.price;
this.txtName.text = data.name;
this.txtPrice.text = this.price.ToString();
this.chestType = (eChestType)data.type;
SpriteAtlas atlas = AtlasManager.instance.GetAtlas("UIAtlas");
this.icon.sprite = atlas.GetSprite(data.sprite_name);
this.icon.SetNativeSize();
this.btnBuy.onClick.AddListener(() => {
Debug.LogFormat("{0}, {1}", this.chestType, this.price);
this.onClickPrice();
});
}
}
Test05UIChestScrollView
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class Test05UIChestScrollView : MonoBehaviour
{
[SerializeField]
private Transform contentTr;
[SerializeField]
private GameObject uiChestCellPrefab;
[SerializeField]
private GameObject uiChestCellAdPrefab;
private List<Test05UIChestCell> cellList = new List<Test05UIChestCell>();
public void Init()
{
List<ChestData> chestDatas = DataManager.instance.GetChestDatas();
foreach(ChestData data in chestDatas)
{
Debug.LogFormat("<color=yellow>[UIChestScrollView] {0}, {1}, {2}, {3}, {4}</color>", data.id, data.name, data.type, data.price, data.sprite_name);
Test05UIChestCell cell = null;
if((Test05UIChestCell.eChestType)data.type == Test05UIChestCell.eChestType.Wooden)
{
//Wooden이라면 Wooden 프리팹 생성
GameObject go = Instantiate(this.uiChestCellAdPrefab, this.contentTr);
cell = go.GetComponent<Test05UIChestCellAd>();
Test05UIChestCellAd cellAd = cell as Test05UIChestCellAd;
cellAd.onClickAd = () => {
Debug.LogFormat("<color=yellow>{0}, 광고보기</color>", cellAd.ChestType);
};
cellAd.onClickPrice = () => {
Debug.LogFormat("<color=yellow>{0}, {1}</color>", cell.ChestType, cell.Price);
};
}
else
{
//Wooden이 아닌경우 프리팹 생성?
GameObject go = Instantiate(this.uiChestCellPrefab, this.contentTr);
cell = go.GetComponent<Test05UIChestCell>();
cell.onClickPrice = () => {
Debug.LogFormat("<color=yellow>{0}, {1}</color>", cell.ChestType, cell.Price);
};
}
cell.Init(data);
this.cellList.Add(cell);
}
}
}
ChestData
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChestData
{
public int id;
public string name;
public int type;
public int price;
public string sprite_name;
}
DataManager
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DataManager
{
public static readonly DataManager instance = new DataManager();
public Dictionary<int, ChestData> dicChest = new Dictionary<int, ChestData>();
public void LoadChestData()
{
TextAsset asset = Resources.Load<TextAsset>("Chest_data");
Debug.Log(asset);
ChestData[] datas = JsonConvert.DeserializeObject<ChestData[]>(asset.text);
foreach(ChestData data in datas)
{
this.dicChest.Add(data.id, data);
}
}
public List<ChestData> GetChestDatas()
{
List<ChestData> list = new List<ChestData>();
foreach (ChestData data in this.dicChest.Values)
{
list.Add(data);
}
return list;
}
}
Test05Main
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test05Main : MonoBehaviour
{
[SerializeField]
private Test05UIChestScrollView uiChestScrollView;
private void Start()
{
AtlasManager.instance.LoadAtlases();
DataManager.instance.LoadChestData();
this.uiChestScrollView.Init();
}
}
'유니티 심화 > 복습' 카테고리의 다른 글
[복습] LearnUGUI - Practice05 [Shop], DataManager로 데이터 받아오기 (0) | 2023.09.08 |
---|---|
[복습] LearnUGUI - Practice05 [Shop] 잘 몰라서 메모장에 적어놓고 연습 (0) | 2023.09.07 |
[복습] LearnUGUI - Test04[Stage] (2) | 2023.09.07 |
[복습] LearnUGUI - Stage (0) | 2023.09.06 |
[복습]LearnUGUI - Button, Toggle (0) | 2023.09.05 |