특정 ID인 Data객체 검색하기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft;
using Newtonsoft.Json;
public class Test6Main : MonoBehaviour
{
private ShopGemData[] datas;
void Start()
{
var asset = Resources.Load<TextAsset>("shop_gem_data");
var json = asset.text;
//역직렬화
datas = JsonConvert.DeserializeObject<ShopGemData[]>(json);
//var datas = JsonConvert.DeserializeObject<List<ShopGemData>>(json);
//id 가 102번인 Data객체를 검색
ShopGemData foundData = this.GetDataById(102);
Debug.LogFormat("{0}, {1}, {2}, {3}, {4}", foundData.id, foundData.name, foundData.icon, foundData.amount, foundData.price);
}
//private ShopGemData GetDataById(int id)
//{
// for (int i = 0; i < this.datas.Length; i++) {
// ShopGemData data = this.datas[i];
// if (data.id == id) {
// Debug.Log("찾았다");
// return data;
// }
// }
// return null;
//}
private ShopGemData GetDataById(int id)
{
ShopGemData foundData = null;
for (int i = 0; i < this.datas.Length; i++)
{
ShopGemData data = this.datas[i];
if (data.id == id)
{
foundData = data;
break;
}
}
return foundData;
}
}
최소가격, 최대가격 Data객체 검색하기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft;
using Newtonsoft.Json;
public class Test6Main : MonoBehaviour
{
private ShopGemData[] datas;
void Start()
{
var asset = Resources.Load<TextAsset>("shop_gem_data");
var json = asset.text;
//역직렬화
datas = JsonConvert.DeserializeObject<ShopGemData[]>(json);
//var datas = JsonConvert.DeserializeObject<List<ShopGemData>>(json);
// 최소가격 최대가격 Data객체를 검색
ShopGemData minPriceData = this.GetMinPriceData();
Debug.LogFormat("=> {0}", minPriceData.price);
ShopGemData maxPriceData = this.GetMaxPriceData();
Debug.LogFormat("=> {0}", maxPriceData.price);
}
private ShopGemData GetMinPriceData()
{
float minPrice = Mathf.Infinity;
ShopGemData rtnData = null;
for (int i = 0; i < this.datas.Length; i++) {
var data = this.datas[i];
Debug.LogFormat("{0}, {1}", data.price, minPrice);
if (data.price < minPrice) {
rtnData = data;
minPrice = rtnData.price;
}
}
return rtnData;
}
private ShopGemData GetMaxPriceData()
{
float maxPrice = 0;
ShopGemData rtnData = null;
for (int i = 0; i < this.datas.Length; i++)
{
var data = this.datas[i];
Debug.LogFormat("{0}, {1}", data.price, maxPrice);
if (data.price > maxPrice)
{
rtnData = data;
maxPrice = rtnData.price;
}
}
return rtnData;
}
}
'게임개발 > 게임 UI.UX 프로그래밍' 카테고리의 다른 글
데이터 저장하기 1 (0) | 2024.02.14 |
---|---|
DataManager 싱글톤 클래스 만들기 (0) | 2024.02.13 |
데이터 연동 스크롤뷰 (정적) 만들기 / 딕셔너리 (1) | 2024.02.13 |
데이터 연동해서 UI에 보여주기 (0) | 2024.02.13 |
json (0) | 2024.02.12 |