From 1c1d8e5d42511bee3cc685384af90b81bf1028bb Mon Sep 17 00:00:00 2001 From: "aube.lee" Date: Fri, 31 Jan 2025 14:31:14 +0900 Subject: [PATCH] =?UTF-8?q?JSON=20=EB=A1=9C=EB=8D=94=20=EC=8A=A4=ED=81=AC?= =?UTF-8?q?=EB=A6=BD=ED=8A=B8=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 | 48 +++++++++++++++++++ .../Gameton/Scripts/Common/JSONLoader.cs.meta | 11 +++++ 2 files changed, 59 insertions(+) create mode 100644 Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs create mode 100644 Gameton-06/Assets/Gameton/Scripts/Common/JSONLoader.cs.meta 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: