using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Newtonsoft.Json;
public class DataManager
{
public static readonly DataManager Instance = new DataManager();
public Dictionary<T, T> dicDatas = new Dictionary<T, T>();
public Dictionary<T,T> dicInfoDatas = new Dictionary<T, T>();
private DataManager() { }
public void LoadData()
{
var asset = Resources.Load<TextAsset>("path");
var json = asset.text;
var datas = JsonConvert.DeserializeObject<T[]>(json);
foreach (var data in datas)
{
if (!this.dicDatas.ContainsKey(data.id)) // 키 중복 검사
{
this.dicDatas.Add(data.id, data);
}
}
Debug.LogFormat("data Load Complete! => Count : {0}", this.dicDatas.Count);
}
public void LoadInfoData()
{
string filePath = Path.Combine(Application.persistentDataPath, "data_info.json");
if (File.Exists(filePath))
{
var json = File.ReadAllText(filePath);
var datas = JsonConvert.DeserializeObject<T[]>(json);
foreach (var data in datas)
{
if (!this.dicDatas.ContainsKey(data.id)) // 키 중복 검사
{
this.dicDatas.Add(data.id, data);
}
}
Debug.LogFormat("Info Load Complete! => Count : {0}", this.dicInfoDatas.Count);
}
else
{
Debug.LogFormat("Cannot find file: " + filePath + "\n" + "new data.json created!");
foreach (var Data in this.dicDatas.Values)
{
// 새 MissionInfo 객체를 생성하고, 필요한 데이터로 초기화합니다.
T newInfo = new T
{
//기본 데이터 설정
};
// 딕셔너리에 추가
this.dicInfoDatas.Add(T.id, newInfo);
}
SaveData();
}
}
public void SaveData()
{
var Datas = this.dicDatas.Values.ToArray();
var json = JsonConvert.SerializeObject(Datas);
string path = Path.Combine(Application.persistentDataPath, "data.json");
File.WriteAllText(path, json);
Debug.Log("data Save Complete!");
}
}
+++
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameManager : MonoBehaviour {
public static GameManager instance;
void Awake() {
if (instance == null)
{
instance = this;
}
else
{
Debug.LogWarning("씬에 두개 이상의 게임 매니저가 존재합니다!");
Destroy(gameObject);
}
}
}
반응형
'메모장' 카테고리의 다른 글
이벤트(Event), 이벤트 핸들러(EventHandler), 그리고 이벤트 기반 프로그래밍(Event-driven programming) (0) | 2024.02.26 |
---|---|
System.Text.RegularExpressions.Regex.IsMatch - 문자열 매칭 메서드 (0) | 2024.02.23 |
[BigNumberConverter] 재화 단위 시스템으로 숫자 변환 (0) | 2024.02.20 |
Application.dataPath 및 파일 저장 경로 메모 (0) | 2024.02.13 |
com.unity.nuget.newtonsoft-json (0) | 2024.02.08 |