From fd7627cd3feaddc030d925d4a09ff2289eab149c Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Tue, 11 Feb 2025 17:12:39 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20Resource.Load=20=EC=9D=98=20=EB=8D=B0?= =?UTF-8?q?=EC=9D=B4=ED=84=B0=20=EB=8F=99=EC=A0=81=20=EB=B3=80=EA=B2=BD=20?= =?UTF-8?q?=EB=B6=88=EA=B0=80=ED=95=9C=20=EC=9D=B4=EC=8A=88=EB=A5=BC=20?= =?UTF-8?q?=ED=95=B4=EA=B2=B0=ED=95=98=EA=B8=B0=20=EC=9C=84=ED=95=9C=20per?= =?UTF-8?q?sistentDataPath=20=EC=A0=81=EC=9A=A9=20=EB=A9=94=EC=86=8C?= =?UTF-8?q?=EB=93=9C=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Gameton/Scripts/Common/JSONLoader.cs | 112 +++++++++++++++--- 1 file changed, 95 insertions(+), 17 deletions(-) diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs index df79ac9e..6c673452 100644 --- a/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs +++ b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs @@ -64,23 +64,6 @@ namespace TON } } - /// Application.persistentDataPath에서 JSON 파일을 읽어 특정 데이터 타입으로 변환하는 함수 - public static T LoadFromFile(string fileName) - { - string filePath = Application.persistentDataPath + "/" + fileName; - - if (File.Exists(filePath)) - { - string json = File.ReadAllText(filePath); - return JsonUtility.FromJson(json); - } - else - { - Debug.LogError($"파일을 찾을 수 없습니다: {filePath}"); - return default; - } - } - /// 특정 데이터를 JSON 형식으로 저장하는 함수 public static void SaveToFile(T data, string fileName) { @@ -105,5 +88,100 @@ namespace TON Debug.Log($"파일 저장 성공 ::: {fileName}.json"); } + /// Application.persistentDataPath 내의 파일 경로 생성성 + private static string GetPersistentPath(string fileName) + { + return Path.Combine(Application.persistentDataPath, fileName + ".json"); + } + + /// Resources.Load 로 읽어온 파일을 persistentDataPath 경로에 저장 + public static bool SaveJsonToPersistentData(string fileName) + { + if (fileName.EndsWith(".json")) + { + fileName = fileName.Replace(".json", ""); // 확장자 제거 + } + + string persistentPath = GetPersistentPath(fileName); + + // 📌 Step 1: persistentDataPath에 파일이 있는지 체크 + if (File.Exists(persistentPath)) + { + Debug.Log($"⚠ {fileName}.json 파일이 이미 존재합니다. 덮어쓰지 않습니다. ({persistentPath})"); + return false; + } + + // 📌 Step 2: Resources에서 JSON 불러오기 + string path = "GameData/" + fileName; // Resources 폴더 내 경로 + TextAsset jsonFile = Resources.Load(path); + + if (jsonFile != null) + { + File.WriteAllText(persistentPath, jsonFile.text); + Debug.Log($"✅ JSON 저장 완료 (처음 저장됨): {persistentPath}"); + return true; + } + else + { + Debug.LogError($"❌ Resources에서 JSON 파일을 찾을 수 없음: {path}"); + return false; + } + } + + + /// persistentDataPath 경로의 파일 읽어오기 + public static T LoadJsonFromPersistentData(string fileName) + { + string path = GetPersistentPath(fileName); + + if (!File.Exists(path)) + { + Debug.LogError($"⚠ 파일을 찾을 수 없습니다: {path}"); + return default; + } + + string jsonText = File.ReadAllText(path); + Debug.Log($"📂 JSON 로드: {jsonText}"); + + // 리스트(JSON 배열)인지 확인 + if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>)) + { + if (jsonText.StartsWith("[")) + { + return JsonUtility.FromJson>(WrapArray(jsonText)).items; + } + else + { + Wrapper wrapperInstance = JsonUtility.FromJson>(jsonText); + return wrapperInstance.items; + } + } + + // 일반 객체 변환 + return JsonUtility.FromJson(jsonText); + } + + /// persistentDataPath 경로의 파일 데이터 업데이트 + public static void SaveUpdatedJsonToPersistentData(T updatedData, string fileName) + { + string path = GetPersistentPath(fileName); + string json; + + // 리스트인지 확인 후 JSON 변환 + if (typeof(T).IsGenericType && typeof(T).GetGenericTypeDefinition() == typeof(List<>)) + { + Wrapper wrapper = new Wrapper { items = updatedData }; + json = JsonUtility.ToJson(wrapper, true); + } + else + { + json = JsonUtility.ToJson(updatedData, true); + } + + File.WriteAllText(path, json); + Debug.Log($"✅ JSON 데이터 업데이트 완료: {path}"); + } + + } }