0423 ui scroll view+데이터 테이블 연동
2021. 4. 23. 11:02ㆍunity
mission_data
id |
name |
goal |
reward_id |
reward_amount |
sprite_name |
int |
string |
int |
int |
int |
string |
1000 |
Collect {0} Golds |
10000 |
100 |
5 |
mission_img_collect |
1001 |
Play {0} times |
500 |
101 |
10 |
mission_img_goal |
1002 |
Kill {0} monsters |
300 |
102 |
300 |
mission_img_kill |
1003 |
New Episode open |
1 |
101 |
5 |
mission_img_key |
1004 |
Reash Level {0} |
10 |
101 |
10 |
mission_img_crown |
reward_data
id |
name |
int |
string |
100 |
하트 |
101 |
다이아몬드 |
102 |
골드 |
using UnityEngine;
using UnityEngine.U2D;
public class ScrollViewTest : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
DataManager.GetInstance().LoadDatas();
var data = DataManager.GetInstance().dicMissionData[1000];
var missionName = string.Format(data.name, data.goal);
Debug.LogFormat(missionName);
}
using Newtonsoft.Json;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DataManager
{
private static DataManager instance;
public Dictionary<int, MissionData> dicMissionData;
public Dictionary<int, RewardData> dicRewardData;
private DataManager()
{
}
public static DataManager GetInstance()
{
if (DataManager.instance == null)
{
DataManager.instance = new DataManager();
}
return DataManager.instance;
}
public void LoadDatas()
{
var ta = Resources.Load<TextAsset>("mission_data");
var json = ta.text;
this.dicMissionData = JsonConvert.DeserializeObject<MissionData[]>(json).ToDictionary(x => x.id);
ta = Resources.Load<TextAsset>("reward_data");
json = ta.text;
this.dicRewardData = JsonConvert.DeserializeObject<RewardData[]>(json).ToDictionary(x => x.id);
}
}
프리팹 불러오기
using UnityEngine;
using UnityEngine.U2D;
public class ScrollViewTest : MonoBehaviour
{
public GameObject listItemPrefab;
public Transform contents;
// Start is called before the first frame update
void Start()
{
DataManager.GetInstance().LoadDatas();
for (int i = 0; i < DataManager.GetInstance().dicMissionData.Count; i++)
{
var go = Instantiate<GameObject>(this.listItemPrefab,contents);
}
}
using UnityEngine;
using UnityEngine.U2D;
public class ScrollViewTest : MonoBehaviour
{
public GameObject listItemPrefab;
public Transform contents;
// Start is called before the first frame update
void Start()
{
DataManager.GetInstance().LoadDatas();
foreach (var pair in DataManager.GetInstance().dicMissionData)
{
var go = Instantiate<GameObject>(this.listItemPrefab, contents);
var listItem = go.GetComponent<UIMissionItem>();
var data = pair.Value;
var missionName = string.Format(data.name, data.goal);
listItem.Init(missionName);
}
}
using UnityEngine;
using UnityEngine.UI;
public class UIMissionItem : MonoBehaviour
{
public Text txtName;
public void Init(string missionName)
{
this.txtName.text = missionName;
}
}
using UnityEngine;
using UnityEngine.UI;
public class UIMissionItem : MonoBehaviour
{
public Text txtName;
public Image icon;
public void Init(string missionName,Sprite icon)
{
this.txtName.text = missionName;
this.icon.sprite = icon;
}
}
using UnityEngine;
using UnityEngine.U2D;
public class ScrollViewTest : MonoBehaviour
{
public GameObject listItemPrefab;
public Transform contents;
public SpriteAtlas atlas;
// Start is called before the first frame update
void Start()
{
DataManager.GetInstance().LoadDatas();
foreach (var pair in DataManager.GetInstance().dicMissionData)
{
var go = Instantiate<GameObject>(this.listItemPrefab, contents);
var listItem = go.GetComponent<UIMissionItem>();
var data = pair.Value;
var missionName = string.Format(data.name, data.goal);
var icon = this.atlas.GetSprite(data.sprite_name);
listItem.Init(missionName,icon);
var sp = this.atlas.GetSprite(data.sprite_name);
}
}
}
'unity' 카테고리의 다른 글
0426 NGUI 버튼, 슬라이더, 스프라이트, 텍스트, input Field (0) | 2021.04.26 |
---|---|
0423 ugui scroll view 3D+데이터 테이블 연동2 (RayCast Target) (0) | 2021.04.23 |
0421 데이터 저장 및 로딩 (0) | 2021.04.21 |
0421 UI Stage (0) | 2021.04.21 |
0420 Item Pick up+인벤토리 연동 (0) | 2021.04.20 |