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);
        }
    }


}

 

 

반응형

+ Recent posts