mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-04-17 18:56:18 +02:00
42 lines
1.0 KiB
C#
42 lines
1.0 KiB
C#
using System.IO;
|
|
using System.Runtime.Serialization.Json;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class LevelLoader : MonoBehaviour
|
|
{
|
|
public Text levelNameText;
|
|
private Level level;
|
|
|
|
void Start()
|
|
{
|
|
LoadLevel();
|
|
if (level != null)
|
|
{
|
|
levelNameText.text = level.Name;
|
|
}
|
|
else
|
|
{
|
|
levelNameText.text = "Failed to Load Level";
|
|
}
|
|
}
|
|
|
|
void LoadLevel()
|
|
{
|
|
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);
|
|
}
|
|
}
|
|
}
|