73 lines
2.2 KiB
C#
73 lines
2.2 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
|
|
public class SongDataLibrary : MonoBehaviour
|
|
{
|
|
public static SongDataLibrary Instance;
|
|
private Dictionary<int, SongData> songDataDictionary = new Dictionary<int, SongData>();
|
|
|
|
// 指定存放在 "song_songIndex" 目录下的各个子文件夹名称
|
|
// 例如:如果你有 Assets/Resources/song_songIndex/FolderA 和 FolderB,则在 Inspector 中填写 { "FolderA", "FolderB" }
|
|
public string[] subfolderNames;
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
LoadSongData();
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private void LoadSongData()
|
|
{
|
|
// 先加载直接放在 "song_songIndex" 目录下的 SongData
|
|
SongData[] assets = Resources.LoadAll<SongData>("song_songIndex");
|
|
foreach (SongData data in assets)
|
|
{
|
|
if (!songDataDictionary.ContainsKey(data.songID))
|
|
{
|
|
songDataDictionary.Add(data.songID, data);
|
|
Debug.Log("加载到 SongData, songID: " + data.songID + ", 名称: " + data.songName);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("重复的 songID: " + data.songID);
|
|
}
|
|
}
|
|
|
|
// 再遍历每个子文件夹,加载其中的 SongData
|
|
foreach (string subfolder in subfolderNames)
|
|
{
|
|
SongData[] subAssets = Resources.LoadAll<SongData>("song_songIndex/" + subfolder);
|
|
foreach (SongData data in subAssets)
|
|
{
|
|
if (!songDataDictionary.ContainsKey(data.songID))
|
|
{
|
|
songDataDictionary.Add(data.songID, data);
|
|
Debug.Log("从 " + subfolder + " 加载到 SongData, songID: " + data.songID + ", 名称: " + data.songName);
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning("重复的 songID: " + data.songID);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public SongData GetSongDataByID(int songID)
|
|
{
|
|
if (songDataDictionary.ContainsKey(songID))
|
|
{
|
|
return songDataDictionary[songID];
|
|
}
|
|
Debug.LogError("未找到 songID: " + songID);
|
|
return null;
|
|
}
|
|
}
|