Notice
Recent Posts
Recent Comments
Link
스토리지
[3.25] json 파일을 싱글턴 패턴으로 읽어오기 본문
using System;
namespace Study11
{
public class SongData
{
public int songId;
public string songName;
public string composer;
public int pastDifficulty;
public int presentDifficulty;
public float futureDifficulty;
public bool haveBeyond;
public float beyondDifficulty;
public int backgroundId;
public override string ToString()
{
//문자열 보간 사용
return String.Format("{0 ,-2} {1, -28} {2, -35} {3} {4, 3} {5, 5} {6, 5} {7, 5} {8, 20}",
this.songId, this.songName, this.composer, this.pastDifficulty,
this.presentDifficulty, this.futureDifficulty, this.haveBeyond,
this.beyondDifficulty, DataManager.GetInstance().dicBackgroundData[this.backgroundId]);
}
}
}
using System;
namespace Study11
{
public class BackgroundData
{
public int backgroundId;
public string backgroundName;
public override string ToString()
{
return String.Format("{0}",this.backgroundName);
}
}
}
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
namespace Study11
{
public class DataManager
{
private static DataManager instance;
public Dictionary<int, SongData> dicSongData;
public Dictionary<int, BackgroundData> dicBackgroundData;
private DataManager()
{
}
public static DataManager GetInstance()
{
if (DataManager.instance == null)
{
DataManager.instance = new DataManager();
}
return DataManager.instance;
}
public void LoadSongData(string path)
{
var json = File.ReadAllText(path);
var arr = JsonConvert.DeserializeObject<SongData[]>(json);
this.dicSongData = arr.ToDictionary(x => x.songId);
}
public void LoadBackgroundData(string path)
{
var json = File.ReadAllText(path);
var arr = JsonConvert.DeserializeObject<BackgroundData[]>(json);
this.dicBackgroundData = arr.ToDictionary(x => x.backgroundId);
}
public void PrintData()
{
Console.WriteLine("Data Num : {0}\n", dicSongData.Count);
foreach(var item in dicSongData)
{
Console.WriteLine(item.Value);
}
Console.WriteLine();
}
}
}
using System;
namespace Study11
{
public class App
{
public App()
{
string path = "./song_data.json";
DataManager.GetInstance().LoadSongData(path);
path = "./background_data.json";
DataManager.GetInstance().LoadBackgroundData(path);
DataManager.GetInstance().PrintData();
}
}
}
'Unity > 수업내용(C#)' 카테고리의 다른 글
[3.29] 스레드 (0) | 2021.03.29 |
---|---|
[3.26] C# json 직렬화 & 역직렬화 복습 1 (0) | 2021.03.26 |
[3.25] 싱글턴 패턴 (0) | 2021.03.25 |
[3.25] C#으로 json파일 불러오기 복습 1 (0) | 2021.03.25 |
[3.24] json 파일을 배열로 받아들이기 (0) | 2021.03.24 |
Comments