/
GeekNerd
/
ServerModelingProject
Обзор
Документация
Войти
/
GeekNerd
/
ServerModelingProject
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
Assets/Scripts/LoadServices/JSONLoader.cs
70 строк
2 KB
Gorney-Alex
Init
30 май 2026, 19:05
30 май 2026, 19:05
3cdcc49
Код
Авторство
О чём код?
using System.IO; using UnityEngine; namespace LoadServices { public static class JSONLoader { public static T LoadJsonFromResources<T>(string path) { TextAsset textAsset = Resources.Load<TextAsset>(path); if (textAsset == null) { Debug.LogError($"Couldn't find JSON file at {path}"); return default; } return JsonUtility.FromJson<T>(textAsset.text); } public static void SaveJson<T>(T data, string fileName) { string path = GetApplicationPath(fileName); string json = JsonUtility.ToJson(data, true); File.WriteAllText(path, json); Debug.Log($"Saved JSON to: {path}"); } public static T LoadJsonFromFile<T>(string fileName) { string path = GetApplicationPath(fileName); if (!File.Exists(path)) { Debug.LogWarning($"File not found at {path}"); return default; } string json = File.ReadAllText(path); return JsonUtility.FromJson<T>(json); } public static void SaveJsonToPath<T>(T data, string fullPath) { string json = JsonUtility.ToJson(data, true); File.WriteAllText(fullPath, json); Debug.Log($"Saved JSON to: {fullPath}"); } public static T LoadJsonFromPath<T>(string fullPath) { if (!File.Exists(fullPath)) { Debug.LogWarning($"File not found at {fullPath}"); return default; } string json = File.ReadAllText(fullPath); return JsonUtility.FromJson<T>(json); } private static string GetApplicationPath(string fileName) { return Path.Combine(Application.persistentDataPath, fileName + ".json"); } } }