
Test05UIScrollView
csharp
닫기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();
this.cellList.Add(cell);
}
}
}
DataManager
csharp
닫기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
csharp
닫기using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Test05Main : MonoBehaviour
{
[SerializeField]
private Test05UIChestScrollView uiChestScrollView;
private void Start()
{
DataManager.instance.LoadChestData();
this.uiChestScrollView.Init();
}
}
ChestData
csharp
닫기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;
}
'유니티 심화' 카테고리의 다른 글
LearnUGUI - Test06 [ Mission ] - 저장 / 불러오기 [미완] (0) | 2023.09.08 |
---|---|
LearnUGUI - Test06 [ Mission ] - 미션 Cell 데이터 연동하기 (0) | 2023.09.08 |
LearnUGUI - Test05 [Shop] (0) | 2023.09.07 |
LearnUGUI - Test04[Stage] (0) | 2023.09.06 |
LearnUGUI - Practice04 [Stage] (0) | 2023.09.06 |