Files
bansonic_beta_main/Assets/songsDatabase/SongDataManager.cs
T

61 lines
1.6 KiB
C#
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.IO;
using UnityEngine;
public class SongDataManager : MonoBehaviour
{
private string jsonDirectory;
[Header("Settings")]
public bool autoSaveOnStart = false;
void Start()
{
jsonDirectory = Path.Combine(Application.persistentDataPath, "song_json_export");
if (!Directory.Exists(jsonDirectory))
{
Directory.CreateDirectory(jsonDirectory);
Debug.Log("Created song JSON directory: " + jsonDirectory);
}
if (autoSaveOnStart)
{
StartCoroutine(SaveAllSongDataToJsonRoutine());
}
}
public void TriggerSaveAllSongs()
{
StartCoroutine(SaveAllSongDataToJsonRoutine());
}
System.Collections.IEnumerator SaveAllSongDataToJsonRoutine()
{
SongData[] allSongData = Resources.LoadAll<SongData>("song_songIndex");
if (allSongData.Length == 0)
{
Debug.LogWarning("No SongData assets found in Resources/song_songIndex.");
yield break;
}
Debug.Log($"Start exporting {allSongData.Length} songs to JSON...");
for (int i = 0; i < allSongData.Length; i++)
{
SongData song = allSongData[i];
if (song == null) continue;
SongDataSerializable jsonData = new SongDataSerializable(song);
string json = JsonUtility.ToJson(jsonData, true);
string filePath = Path.Combine(jsonDirectory, song.songID + ".json");
File.WriteAllText(filePath, json);
yield return null;
}
Debug.Log("All song JSON files exported.");
}
}