Files

45 lines
1.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
public class SongDataLoader : MonoBehaviour
{
private const string SecureCategory = "song_export";
private string jsonDirectory;
public List<SongDataSerializable> loadedSongs = new List<SongDataSerializable>();
[SerializeField] private int _yieldEveryNFiles = 5;
void Start()
{
jsonDirectory = Path.Combine(Application.persistentDataPath, "SongDataJson");
StartCoroutine(LoadAllSongsFromJsonRoutine());
}
IEnumerator LoadAllSongsFromJsonRoutine()
{
List<string> loadedJsonList = SecureSaveVault.LoadAllRawJson(SecureCategory, jsonDirectory);
if (loadedJsonList == null || loadedJsonList.Count == 0)
{
Debug.LogError("JSON 持久化数据不存在");
yield break;
}
loadedSongs.Clear();
int counter = 0;
for (int i = 0; i < loadedJsonList.Count; i++)
{
string json = loadedJsonList[i];
SongDataSerializable jsonData = JsonUtility.FromJson<SongDataSerializable>(json);
loadedSongs.Add(jsonData);
Debug.Log("Loaded persistent song data from secure vault.");
counter++;
if (_yieldEveryNFiles > 0 && counter % _yieldEveryNFiles == 0)
{
yield return null;
}
}
}
}