refactor: level resource loader

This commit is contained in:
2025-02-09 19:52:35 +01:00
parent 707dee5063
commit 50945f807d
9 changed files with 33 additions and 63 deletions

View File

@@ -1,5 +1,4 @@
using System.IO;
using System.Runtime.Serialization.Json;
using UnityEngine;
public class LevelsLoader : MonoBehaviour
@@ -10,22 +9,7 @@ public class LevelsLoader : MonoBehaviour
{
DontDestroyOnLoad(gameObject);
// var jsonTextFile = Resources.Load<TextAsset>("Text/jsonFile01");
//Then use JsonUtility.FromJson<T>() to deserialize jsonTextFile into an object
string path = Path.Combine(Application.dataPath, "Levels", "back-on-track.json");
if (File.Exists(path))
{
string json = File.ReadAllText(path);
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Level));
level = (Level)serializer.ReadObject(stream);
}
}
else
{
Debug.LogError("Level file not found: " + path);
}
TextAsset jsonTextFile = Resources.Load<TextAsset>(Path.Combine("Levels", "BackOnTrack"));
level = JsonUtility.FromJson<Level>(jsonTextFile.text);
}
}