diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs new file mode 100644 index 00000000..40ab6987 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs @@ -0,0 +1,48 @@ +using System.Collections; +using System.Collections.Generic; +using System.IO; +using UnityEngine; + +namespace TON +{ + public class JSONLoader : MonoBehaviour + { + /// Resources 폴더에서 JSON 파일을 읽어 특정 데이터 타입으로 변환하는 함수 + public static T LoadFromResources(string fileName) + { + TextAsset jsonFile = Resources.Load(fileName); + if (jsonFile != null) + { + return JsonUtility.FromJson(jsonFile.text); + } + else + { + Debug.LogError($"JSON 파일을 찾을 수 없습니다: {fileName}"); + return default; // 기본값 반환 + } + } + + /// Application.persistentDataPath에서 JSON 파일을 읽어 특정 데이터 타입으로 변환하는 함수 + public static T LoadFromFile(string filePath) + { + 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 filePath) + { + string json = JsonUtility.ToJson(data, true); + File.WriteAllText(filePath, json); + Debug.Log($"데이터 저장 완료: {filePath}"); + } + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs.meta new file mode 100644 index 00000000..0962ef31 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eb99017c9a8f63448be15b27253643f8 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: