mirror of
https://github.com/boudji-ludwig-pett/cnam-geometry-dash.git
synced 2025-06-10 22:20:40 +02:00
feat: import JSON level (#48)
This commit is contained in:
@ -63,6 +63,5 @@ public class ShipGameMode : IGameMode
|
||||
|
||||
public void OnCollisionExit(Player player, Collision2D collision)
|
||||
{
|
||||
// rien pour l'instant
|
||||
}
|
||||
}
|
||||
|
128
Assets/Scripts/JSONImporter.cs
Normal file
128
Assets/Scripts/JSONImporter.cs
Normal file
@ -0,0 +1,128 @@
|
||||
using UnityEngine;
|
||||
using System.IO;
|
||||
using System.Collections;
|
||||
using SimpleFileBrowser;
|
||||
using UnityEngine.SceneManagement;
|
||||
using TMPro;
|
||||
|
||||
public class JSONImporter : MonoBehaviour
|
||||
{
|
||||
public TMP_Text statusText;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (statusText == null)
|
||||
{
|
||||
GameObject statusObj = GameObject.Find("StatusText");
|
||||
if (statusObj != null)
|
||||
{
|
||||
statusText = statusObj.GetComponent<TMP_Text>();
|
||||
if (statusText != null)
|
||||
{
|
||||
Debug.Log("✅ StatusText found and assigned automatically!");
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("⚠️ 'StatusText' was found but does not have a TMP_Text component!");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("⚠️ No GameObject named 'StatusText' found in the scene. Please create a TextMeshPro element and name it 'StatusText'.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void Start()
|
||||
{
|
||||
if (statusText != null)
|
||||
{
|
||||
statusText.text = "Ready to import...";
|
||||
statusText.color = Color.white;
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("statusText is not assigned!");
|
||||
}
|
||||
}
|
||||
|
||||
public void ImportJSON()
|
||||
{
|
||||
Debug.Log("Button clicked, starting import...");
|
||||
if (statusText != null)
|
||||
{
|
||||
statusText.text = "Importing...";
|
||||
statusText.color = Color.yellow;
|
||||
}
|
||||
StartCoroutine(ShowFileBrowser());
|
||||
}
|
||||
|
||||
private IEnumerator ShowFileBrowser()
|
||||
{
|
||||
yield return FileBrowser.WaitForLoadDialog(FileBrowser.PickMode.Files, false, null, null, "Select JSON File", "Load");
|
||||
|
||||
if (FileBrowser.Success)
|
||||
{
|
||||
string sourcePath = FileBrowser.Result[0];
|
||||
|
||||
if (Path.GetExtension(sourcePath).ToLower() != ".json")
|
||||
{
|
||||
UpdateStatus("Invalid file. Please select a JSON file.", Color.red);
|
||||
yield break;
|
||||
}
|
||||
|
||||
string fileName = Path.GetFileName(sourcePath);
|
||||
string destinationPath = Path.Combine(Application.dataPath, "Resources/Levels", fileName);
|
||||
|
||||
bool success = false;
|
||||
try
|
||||
{
|
||||
File.Copy(sourcePath, destinationPath, true);
|
||||
success = true;
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
Debug.LogError("Error copying file: " + e.Message);
|
||||
}
|
||||
|
||||
if (success)
|
||||
{
|
||||
UpdateStatus("Import successful!", Color.green);
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateStatus("Import error.", Color.red);
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
UnityEditor.AssetDatabase.Refresh();
|
||||
#endif
|
||||
LevelsLoader loader = Object.FindAnyObjectByType<LevelsLoader>();
|
||||
if (loader != null)
|
||||
{
|
||||
loader.RefreshLevels();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
UpdateStatus("No file selected.", Color.red);
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
|
||||
private void UpdateStatus(string message, Color color)
|
||||
{
|
||||
if (statusText != null)
|
||||
{
|
||||
statusText.text = message;
|
||||
statusText.color = color;
|
||||
statusText.gameObject.SetActive(false);
|
||||
statusText.gameObject.SetActive(true);
|
||||
Canvas.ForceUpdateCanvases();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("statusText is NULL!");
|
||||
}
|
||||
}
|
||||
}
|
2
Assets/Scripts/JSONImporter.cs.meta
Normal file
2
Assets/Scripts/JSONImporter.cs.meta
Normal file
@ -0,0 +1,2 @@
|
||||
fileFormatVersion: 2
|
||||
guid: baf97ea8555b6214299a38be9fe1724f
|
@ -111,4 +111,12 @@ public class LevelsLoader : MonoBehaviour
|
||||
|
||||
return clampedPercentage;
|
||||
}
|
||||
|
||||
public void RefreshLevels()
|
||||
{
|
||||
levels.Clear();
|
||||
LoadAllLevels();
|
||||
if (levels.Count > 0)
|
||||
levelCurrent = levels[0];
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,11 @@ public class MainMenu : MonoBehaviour
|
||||
SceneManager.LoadSceneAsync("SelectLevelScene");
|
||||
}
|
||||
|
||||
public void OpenImport()
|
||||
{
|
||||
SceneManager.LoadSceneAsync("ImportScene");
|
||||
}
|
||||
|
||||
public void OpenSettings()
|
||||
{
|
||||
// SceneManager.LoadSceneAsync(?);
|
||||
|
Reference in New Issue
Block a user