using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIShop : MonoBehaviour
{
public UIScrollView uiScrollView;
public UIShopMenu uiShopMenu;
public void Init()
{
this.uiScrollView.Init();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShopMain : MonoBehaviour
{
public UIShop uiShop;
void Start()
{
DataManager.instance.LoadShopChestData(); //데이터 로드
DataManager.instance.LoadShopGoldData();
EventManager.instance.onBtnAdClick = () =>
{
Debug.Log("광고를 보여줍니다. ");
};
EventManager.instance.onPurchaseChest = (id) => {
var data = DataManager.instance.GetShopChestData(id);
Debug.LogFormat("{0}, {1}, {2}", data.id, data.name, data.price);
};
EventManager.instance.onPurchaseGold = (id) =>
{
var data = DataManager.instance.GetShopChestData(id);
Debug.LogFormat("{0}, {1}, {2}", data.id, data.name, data.price);
};
EventManager.instance.onClickTab = () =>
{
this.uiShop.Init();
};
this.uiShop.Init(); //UIShop을 초기화
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIScrollView : MonoBehaviour
{
public Transform contentTrans;
public GameObject itemPrefab;
public GameObject itemADPrefab;
public UIShopMenu uiShopMenu;
public GameObject goldPrefab;
public UIScrollViewItem[] arrChest;
public UIScrollViewItemAD[] arrChestAD;
public UIScrollViewGold[] arrGold;
private List<ShopData> listChest;
private List<ShopGoldData> listGold;
public void Init()
{
this.listChest = DataManager.instance.GetShopChestDatas();
this.listGold = DataManager.instance.GetShopGoldDatas();
arrGold = contentTrans.GetComponentsInChildren<UIScrollViewGold>();
arrChest = contentTrans.GetComponentsInChildren<UIScrollViewItem>();
arrChestAD = contentTrans.GetComponentsInChildren<UIScrollViewItemAD>();
if (uiShopMenu.tabType == UIEnums.eShopTabType.Gold)
{
foreach (ShopGoldData data in listGold)
{
this.AddGold(data);
}
foreach (UIScrollViewItem chest in arrChest)
{
Destroy(chest.gameObject);
}
foreach (UIScrollViewItemAD chestAD in arrChestAD)
{
Destroy(chestAD.gameObject);
}
}
else
{
foreach (ShopData data in listChest)
{
if (data.type == 0)
{
//normal
this.AddItem(data);
}
else if (data.type == 1)
{
//ad
this.AddItemAD(data);
}
}
foreach (UIScrollViewGold gold in arrGold)
{
Destroy(gold.gameObject);
}
}
}
public void AddItem(ShopData data)
{
//프리팹 인스턴스 생성, contentTrans 자식으로 부착
var go = Instantiate(this.itemPrefab, this.contentTrans);
UIScrollViewItem item = go.GetComponent<UIScrollViewItem>();
item.Init(data);
item.btnGem.onClick.AddListener(() => {
Debug.LogFormat("purchase chest. id:{0}", item.id);
//사건 발생
EventManager.instance.onPurchaseChest(item.id);
});
}
public void AddItemAD(ShopData data)
{
//프리팹 인스턴스 생성, contentTrans 자식으로 부착
var go = Instantiate(this.itemADPrefab, this.contentTrans);
UIScrollViewItemAD itemAD = go.GetComponent<UIScrollViewItemAD>();
itemAD.Init(data);
itemAD.btnAdd.onClick.AddListener(() => {
Debug.LogFormat("show ad. id:{0}", itemAD.id);
//사건의 발생
EventManager.instance.onBtnAdClick();
});
itemAD.btnGem.onClick.AddListener(() => {
Debug.LogFormat("purchase chest. id:{0}", itemAD.id);
//사건 발생
EventManager.instance.onPurchaseChest(itemAD.id);
});
}
public void AddGold(ShopGoldData data)
{
var go = Instantiate(this.goldPrefab, this.contentTrans);
UIScrollViewGold gold = go.GetComponent<UIScrollViewGold>();
Debug.Log("success");
gold.Init(data);
gold.btnCash.onClick.AddListener(() => {
Debug.LogFormat("purchase gold. id:{0}", gold.id);
EventManager.instance.onPurchaseGold(gold.id);
});
}
}
DataManager
//ShopGold
public ShopGoldData GetShopGoldData(int id)
{
return this.dicShopGoldData[id];
}
public void LoadShopGoldData()
{
TextAsset asset = Resources.Load<TextAsset>("Data/shop_gold_data");
var json = asset.text;
Debug.Log(json);
//역직렬화
ShopGoldData[] arrShopGoldDatas = JsonConvert.DeserializeObject<ShopGoldData[]>(json);
this.dicShopGoldData = arrShopGoldDatas.ToDictionary(x => x.id);
Debug.LogFormat("shop gold data loded : {0}", dicShopGoldData.Count);
}
public List<ShopGoldData> GetShopGoldDatas()
{
return this.dicShopGoldData.Values.ToList();
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShopGoldData
{
public int id;
public string name;
public string sprite_name;
public string price;
public int amount;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class UIScrollViewGold : MonoBehaviour
{
public TMP_Text txtGoldName;
public Image imgGold;
public TMP_Text txtPrice;
public Button btnCash;
public TMP_Text txtGoldAmount;
public int id;
public void Init(ShopGoldData data)
{
Debug.Log("성공");
this.id = data.id;
Debug.Log(data.name);
this.txtGoldName.text = data.name;
var atlas = AtlasManager.instance.GetAtlasByName("UIShopGold");
this.imgGold.sprite = atlas.GetSprite(data.sprite_name);
this.imgGold.SetNativeSize();
this.txtPrice.text = data.price;
this.txtGoldAmount.text = string.Format("{0} Gold", data.amount);
}
}
'Unity > 수업과제' 카테고리의 다른 글
[Unity] AddListener와 for문 (0) | 2023.02.06 |
---|---|
[Unity] Object Pooling 오브젝트 풀링_최적화, Garbage Collector (0) | 2023.02.06 |
[Unity3D] Coroutine 활용 예제 (1) | 2023.02.02 |
[Unity 2D] Mecanim (애니메이션 상태 시스템) 연습 (0) | 2023.02.01 |