using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.U2D;
public class UIGemScrollView : MonoBehaviour
{
[SerializeField] private GemCell[] gemCells;
[SerializeField] private GameObject GemCellPrefab;
[SerializeField] private GameObject contents;
[SerializeField] private SpriteAtlas gemAtlas;
void Start()
{
gemCells = new GemCell[ShopDataManager.Instance.dicGemDatas.Count];
for (int i = 0; i < ShopDataManager.Instance.dicGemDatas.Count; i++)
{
GameObject gemCellGO = Instantiate(GemCellPrefab, contents.transform);
gemCells[i] = gemCellGO.GetComponent<GemCell>();
}
int idx = 100;
for (int i = 0; i < gemCells.Length; i++)
{
gemCells[i].gemTitle.text = ShopDataManager.Instance.dicGemDatas[idx].gemName;
gemCells[i].gemImage.sprite = gemAtlas.GetSprite(ShopDataManager.Instance.dicGemDatas[idx].imageName);
gemCells[i].gemImage.SetNativeSize();
gemCells[i].gemImage.rectTransform.anchoredPosition = new Vector2(ShopDataManager.Instance.dicGemDatas[idx].posX, ShopDataManager.Instance.dicGemDatas[idx].posY);
gemCells[i].gemValue.text = Convert.ToString(ShopDataManager.Instance.dicGemDatas[idx].value);
gemCells[i].gemPrice.text = "US $" + Convert.ToString(ShopDataManager.Instance.dicGemDatas[idx].price);
gemCells[i].gemParticles[i].gameObject.SetActive(true);
for(int j = 0; j< gemCells.Length; j++)
{
gemCells[i].gemParticles[j].gameObject.SetActive(false);
}
gemCells[i].gemParticles[i].gameObject.SetActive(true);
if (gemCells[i].gemTitle.text == "Tiny of Gem")
{
gemCells[i].topGlows[0].gameObject.SetActive(true); //topGlows[0] : Red
gemCells[i].flames[1].gameObject.SetActive(true); //frames[1] : Red
gemCells[i].labels[1].gameObject.SetActive(true); //labels[1] : Red
}
else if (gemCells[i].gemTitle.text == "Pouch of Gem")
{
gemCells[i].topGlows[1].gameObject.SetActive(true); //topGlows[1] : Blue
gemCells[i].flames[2].gameObject.SetActive(true); //frames[2] : Blue
gemCells[i].labels[0].gameObject.SetActive(true); //labels[0] : Blue
}
else
{
gemCells[i].flames[0].gameObject.SetActive(true); //frames[0] : Normal
}
idx++;
}
}
}
5. UIGemScrollView.cs
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShopDataManager
{
public static readonly ShopDataManager Instance = new ShopDataManager();
public Dictionary<int, GemData> dicGemDatas = new Dictionary<int, GemData>();
private ShopDataManager() { }
public void LoadGemData()
{
var asset = Resources.Load<TextAsset>("Gem_data");
var json = asset.text;
var gemDatas = JsonConvert.DeserializeObject<GemData[]>(json);
foreach (var gemData in gemDatas)
{
this.dicGemDatas.Add(gemData.id, gemData);
}
Debug.LogFormat("gem_data Load Complete! => Count : {0}", this.dicGemDatas.Count);
}
}
6. ShopDataManager.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GemData : MonoBehaviour
{
public int id;
public string gemName;
public string imageName;
public int value;
public float price;
public float posX;
public float posY;
}
7. GemData.cs
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
public class GemCell : MonoBehaviour
{
public Image[] topGlows; //0: Red , 1: Blue
public Image[] flames; //0: Normal, 1: Red, 2: Blue
public Image[] labels; //0: Blue, 1: Red
public Image gemImage;
public GameObject[] gemParticles; // 0 ~ 5
public TMP_Text gemTitle;
public TMP_Text gemValue;
public TMP_Text gemPrice;
}
8. GemCell.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class UIShopMain : MonoBehaviour
{
[SerializeField] private UIGemScrollView gemScrollView;
private void Awake()
{
ShopDataManager.Instance.LoadGemData();
}
}
9. UIShopMain.cs
'산대특 > 게임 UIUX프로그래밍' 카테고리의 다른 글
[LearnUGUI] 인벤토리 만들기 (2) - 데이터 연동으로 동적 인벤토리 만들기 (저장 및 불러오기) (0) | 2024.02.21 |
---|---|
[LearnUGUI] 인벤토리 만들기 (1) - Grid Scroll VIew 만들기 (0) | 2024.02.18 |
[LearnUGUI] Start 버튼 클릭 시 씬 전환 + 캐릭터 이동 (0) | 2024.02.10 |
[LearnUGUI] Home 화면에서 광고 버튼 누르면 Popup 보여주기 (0) | 2024.02.10 |
[LearnUGUI] Settings 화면 만들기 (0) | 2024.02.10 |