0427 NGUI Scroll View+(데이터 연동)
2021. 4. 27. 17:33ㆍunity
using UnityEngine;
public class NGUITestScrollView : MonoBehaviour
{
public GameObject prefab;
public UIGrid grid;
// Start is called before the first frame update
void Start()
{
for (int i = 0; i < 10; i++)
{
var go = Instantiate<GameObject>(this.prefab, this.grid.transform);
}
grid.Reposition();
}
데이터 연동
using UnityEngine;
public class UIListItem : MonoBehaviour
{
public UIButton btnPurchase;
public GameObject popular;
public GameObject best;
public UILabel labelPrice;
public UILabel labelName;
private ShopData data;
public UISprite icon;
public virtual void Init(ShopData data)
{
this.data = data;
this.labelName.text = this.data.name;
this.labelPrice.text = this.data.price.ToString();
this.icon.spriteName = this.data.sprite_name;
this.icon.width = this.data.width;
this.icon.height = this.data.height;
Debug.Log(this.data.height);
if (this.data.is_popular)
{
this.popular.SetActive(true);
this.best.SetActive(false);
}
if (this.data.is_best)
{
this.popular.SetActive(false);
this.best.SetActive(true);
}
}
}
public class ShopData
{
public int id;
public string name;
public bool is_popular;
public bool is_best;
public int price;
public string sprite_name;
public int width;
public int height;
}
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
using System.Linq;
public class NGUITestScrollView : MonoBehaviour
{
public GameObject prefab;
public UIGrid grid;
private Dictionary<int, ShopData> dicShopDatas;
// Start is called before the first frame update
void Start()
{
var ta = Resources.Load<TextAsset>("listitem_data");
var json = ta.text;
this.dicShopDatas = JsonConvert.DeserializeObject<ShopData[]>(json).ToDictionary(x => x.id);
foreach (var data in this.dicShopDatas.Values)
{
var go = Instantiate<GameObject>(this.prefab, this.grid.transform);
var listItem = go.GetComponent<UIListItem>();
listItem.Init(data);
listItem.btnPurchase.onClick.Add(new EventDelegate(() =>
{
Debug.LogFormat("{0} {1}", data.id, data.price);
}));
this.grid.Reposition();
}
}
// Update is called once per frame
void Update()
{
}
}
[
{
"id": "100",
"name": "Wooden Chest",
"is_popular": "TRUE",
"is_best": "FALSE",
"price": "100",
"sprite_name": "shop_img_chest_0",
"width": "222",
"height": "205"
},
{
"id": "101",
"name": "Silver Chest",
"is_popular": "FALSE",
"is_best": "TRUE",
"price": "600",
"sprite_name": "shop_img_chest_1",
"width": "236",
"height": "218"
},
{
"id": "102",
"name": "Golden Chest",
"is_popular": "FALSE",
"is_best": "FALSE",
"price": "1000",
"sprite_name": "shop_img_chest_2",
"width": "250",
"height": "231"
},
{
"id": "103",
"name": "Epic Chest",
"is_popular": "FALSE",
"is_best": "FALSE",
"price": "1500",
"sprite_name": "shop_img_chest_3",
"width": "238",
"height": "246"
},
{
"id": "104",
"name": "Legendary Chest",
"is_popular": "FALSE",
"is_best": "FALSE",
"price": "6500",
"sprite_name": "shop_img_chest_4",
"width": "296",
"height": "276"
}
]
버튼 클릭 시 연동된 데이터의 id와 가격이 출력되도록 했다.
'unity' 카테고리의 다른 글
0429 데이터 연동으로 해당 프리팹 불러오기 (0) | 2021.04.29 |
---|---|
0428 월드 좌표를 로컬 좌표로 변환하기(+ DOTween) (0) | 2021.04.28 |
0426 NGUI 버튼, 슬라이더, 스프라이트, 텍스트, input Field (0) | 2021.04.26 |
0423 ugui scroll view 3D+데이터 테이블 연동2 (RayCast Target) (0) | 2021.04.23 |
0423 ui scroll view+데이터 테이블 연동 (0) | 2021.04.23 |