技能主要更新,修复卡顿并加入动画,以及各种其他更新。
This commit is contained in:
@@ -3,56 +3,67 @@ using UnityEngine;
|
||||
|
||||
public class SongDataManager : MonoBehaviour
|
||||
{
|
||||
// 保存 JSON 文件的目录路径
|
||||
// ���� JSON �ļ���Ŀ¼·��
|
||||
private string jsonDirectory;
|
||||
|
||||
[Header("Settings")]
|
||||
public bool autoSaveOnStart = false; // 默认关闭,仅在需要同步数据时手动开启
|
||||
|
||||
void Start()
|
||||
{
|
||||
// 获取持久化数据路径,并在其下创建 "SongDataJson" 文件夹
|
||||
// 获取持久化存储路径,并在该目录下创建 "SongDataJson" 文件夹
|
||||
jsonDirectory = Path.Combine(Application.persistentDataPath, "SongDataJson");
|
||||
|
||||
// 如果目录不存在,则创建目录
|
||||
// 如果目录不存在,则创建目录
|
||||
if (!Directory.Exists(jsonDirectory))
|
||||
{
|
||||
Directory.CreateDirectory(jsonDirectory);
|
||||
Debug.Log("创建 JSON 存储目录: " + jsonDirectory);
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.Log("JSON 存储目录已存在: " + jsonDirectory);
|
||||
Debug.Log("创建 JSON 存储目录: " + jsonDirectory);
|
||||
}
|
||||
|
||||
// 调用方法,将所有 SongData 资源转换为 JSON 并保存
|
||||
SaveAllSongDataToJson();
|
||||
// 只有开启了 autoSaveOnStart 且在编辑器模式下才自动执行
|
||||
if (autoSaveOnStart)
|
||||
{
|
||||
StartCoroutine(SaveAllSongDataToJsonRoutine());
|
||||
}
|
||||
}
|
||||
|
||||
void SaveAllSongDataToJson()
|
||||
public void TriggerSaveAllSongs()
|
||||
{
|
||||
// 从 Resources 文件夹下的 "SongData" 子目录加载所有 SongData 资源
|
||||
StartCoroutine(SaveAllSongDataToJsonRoutine());
|
||||
}
|
||||
|
||||
System.Collections.IEnumerator SaveAllSongDataToJsonRoutine()
|
||||
{
|
||||
// 从 Resources 文件夹下的 "song_songIndex" 子目录加载所有 SongData 资源
|
||||
SongData[] allSongData = Resources.LoadAll<SongData>("song_songIndex");
|
||||
|
||||
|
||||
// 如果没有加载到任何 SongData,输出警告信息
|
||||
if (allSongData.Length == 0)
|
||||
{
|
||||
Debug.LogWarning("未在 Resources/SongData 中找到任何 SongData 资源!");
|
||||
Debug.LogWarning("未在 Resources/song_songIndex 找到任何 SongData 资源。");
|
||||
yield break;
|
||||
}
|
||||
|
||||
// 遍历每个 SongData 资源
|
||||
foreach (SongData song in allSongData)
|
||||
Debug.Log($"开始异步保存 {allSongData.Length} 个歌曲数据...");
|
||||
|
||||
// 遍历每个 SongData 资源
|
||||
for (int i = 0; i < allSongData.Length; i++)
|
||||
{
|
||||
// 使用 SongDataSerializable 将 SongData 转换为可序列化对象
|
||||
SongData song = allSongData[i];
|
||||
// 使用 SongDataSerializable 将 SongData 转换为可序列化对象
|
||||
SongDataSerializable jsonData = new SongDataSerializable(song);
|
||||
// 将对象转换为 JSON 格式字符串,true 表示格式化输出(便于阅读)
|
||||
string json = JsonUtility.ToJson(jsonData, true);
|
||||
|
||||
// 构造 JSON 文件的完整路径,文件名使用 songID 命名,例如 "123.json"
|
||||
string filePath = Path.Combine(jsonDirectory, song.songID + ".json");
|
||||
|
||||
// 将 JSON 内容写入文件
|
||||
File.WriteAllText(filePath, json);
|
||||
// 输出调试信息,显示已保存的 JSON 文件路径
|
||||
Debug.Log("已保存 JSON 文件: " + filePath);
|
||||
|
||||
// 每处理 5 个文件等待一帧,防止主线程卡死
|
||||
if (i % 5 == 0)
|
||||
{
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
Debug.Log("所有 JSON 文件保存完毕。");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user