177 lines
5.1 KiB
C#
177 lines
5.1 KiB
C#
using UnityEngine;
|
|
using System.Collections.Generic;
|
|
using System.Collections;
|
|
|
|
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;
|
|
|
|
[Header("Song List")]
|
|
[SerializeField] SongList songList;
|
|
[SerializeField] bool useSongListIfAvailable = true;
|
|
|
|
[Header("Async Load Settings")]
|
|
[SerializeField] int yieldEvery = 16;
|
|
[SerializeField] bool logLoadProgress = false;
|
|
|
|
public bool IsLoaded { get; private set; }
|
|
public bool IsLoading { get; private set; }
|
|
|
|
private void Awake()
|
|
{
|
|
if (Instance == null)
|
|
{
|
|
Instance = this;
|
|
DontDestroyOnLoad(gameObject);
|
|
StartCoroutine(LoadSongDataCoroutine());
|
|
}
|
|
else
|
|
{
|
|
Destroy(gameObject);
|
|
}
|
|
}
|
|
|
|
private IEnumerator LoadSongDataCoroutine()
|
|
{
|
|
if (IsLoading || IsLoaded)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
IsLoading = true;
|
|
songDataDictionary.Clear();
|
|
|
|
// Allow one frame to render before heavy load begins
|
|
yield return null;
|
|
|
|
// prefer SongList if available to avoid heavy Resources.LoadAll
|
|
if (useSongListIfAvailable)
|
|
{
|
|
if (songList == null)
|
|
{
|
|
SongSelectUI ui = Object.FindAnyObjectByType<SongSelectUI>();
|
|
if (ui != null)
|
|
{
|
|
songList = ui.songList;
|
|
}
|
|
}
|
|
if (songList != null && songList.songs != null && songList.songs.Count > 0)
|
|
{
|
|
yield return LoadFromList(songList.songs, "songList");
|
|
IsLoaded = true;
|
|
IsLoading = false;
|
|
yield break;
|
|
}
|
|
}
|
|
|
|
yield return LoadFromPath("song_songIndex", "root");
|
|
|
|
if (subfolderNames != null)
|
|
{
|
|
for (int i = 0; i < subfolderNames.Length; i++)
|
|
{
|
|
string subfolder = subfolderNames[i];
|
|
if (string.IsNullOrEmpty(subfolder))
|
|
{
|
|
continue;
|
|
}
|
|
yield return LoadFromPath("song_songIndex/" + subfolder, subfolder);
|
|
// yield a frame between folders to reduce hitch
|
|
yield return null;
|
|
}
|
|
}
|
|
|
|
IsLoaded = true;
|
|
IsLoading = false;
|
|
}
|
|
|
|
private IEnumerator LoadFromPath(string path, string label)
|
|
{
|
|
SongData[] assets = Resources.LoadAll<SongData>(path);
|
|
if (assets == null || assets.Length == 0)
|
|
{
|
|
if (logLoadProgress)
|
|
{
|
|
Debug.Log("SongDataLibrary: no assets found at '" + path + "'");
|
|
}
|
|
yield break;
|
|
}
|
|
|
|
int yielded = 0;
|
|
foreach (SongData data in assets)
|
|
{
|
|
if (data == null)
|
|
{
|
|
continue;
|
|
}
|
|
if (!songDataDictionary.ContainsKey(data.songID))
|
|
{
|
|
songDataDictionary.Add(data.songID, data);
|
|
if (logLoadProgress)
|
|
{
|
|
Debug.Log("Loaded SongData [" + label + "] songID: " + data.songID + ", name: " + data.songName);
|
|
}
|
|
}
|
|
else if (logLoadProgress)
|
|
{
|
|
Debug.LogWarning("Duplicate songID: " + data.songID);
|
|
}
|
|
|
|
yielded++;
|
|
if (yieldEvery > 0 && (yielded % yieldEvery) == 0)
|
|
{
|
|
// yield to spread work across frames
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
private IEnumerator LoadFromList(List<SongData> list, string label)
|
|
{
|
|
if (list == null || list.Count == 0)
|
|
{
|
|
yield break;
|
|
}
|
|
|
|
int yielded = 0;
|
|
for (int i = 0; i < list.Count; i++)
|
|
{
|
|
SongData data = list[i];
|
|
if (data == null) continue;
|
|
if (!songDataDictionary.ContainsKey(data.songID))
|
|
{
|
|
songDataDictionary.Add(data.songID, data);
|
|
if (logLoadProgress)
|
|
{
|
|
Debug.Log("Loaded SongData [" + label + "] songID: " + data.songID + ", name: " + data.songName);
|
|
}
|
|
}
|
|
else if (logLoadProgress)
|
|
{
|
|
Debug.LogWarning("Duplicate songID: " + data.songID);
|
|
}
|
|
|
|
yielded++;
|
|
if (yieldEvery > 0 && (yielded % yieldEvery) == 0)
|
|
{
|
|
yield return null;
|
|
}
|
|
}
|
|
}
|
|
public SongData GetSongDataByID(int songID)
|
|
{
|
|
if (songDataDictionary.ContainsKey(songID))
|
|
{
|
|
return songDataDictionary[songID];
|
|
}
|
|
Debug.LogError("δ�ҵ� songID: " + songID);
|
|
return null;
|
|
}
|
|
}
|