using Cute; using System; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; public class ResourcesManager : MonoBehaviour { private static ResourcesManager _instance; public static ResourcesManager instance { get { return _instance; } } private bool loadSemaphore; private Dictionary bundleDictionary = new Dictionary(); private Dictionary objectDictionary = new Dictionary(); private Dictionary audioclipDictionary = new Dictionary(); private Dictionary shaderDictionary = new Dictionary(); //読み込みの終了を待つ List parallelCorutines = new List(2); void Awake() { if (_instance != null) { UnityEngine.Object.Destroy(this); } else { _instance = this; } } private void OnDestroy() { initResource(); print("Destory Resource"); _instance = null; } private void Start() { } public void Destroy() { UnityEngine.Object.Destroy(base.gameObject); } public void initResource() { foreach (var tmp in bundleDictionary) { AssetBundle bundle = tmp.Value; bundle.Unload(true); } bundleDictionary.Clear(); objectDictionary.Clear(); shaderDictionary.Clear(); audioclipDictionary.Clear(); } /// /// HashSetを経由することで重複したアセットを削除 /// private List FilterAssetList(List assetList) { if (assetList.Count < 2) { return assetList; } HashSet collection = new HashSet(assetList); List list = new List(collection); if (list.Count != assetList.Count) { } return list; } /// /// 並列でアセットを読み込み /// /// /// 同時処理数 /// アクション private IEnumerator ParallelAssetListExec(List assetList) { assetList = FilterAssetList(assetList); int totalJobs = assetList.Count; int finishedJobs = 0; while (true) { int currentJobs = assetList.Count; for (int i = 0; i < currentJobs; i++) { StartCoroutine(LoadAsset(assetList[i], delegate { finishedJobs++; })); } while (finishedJobs < totalJobs) { yield return (object)0; } yield break; } } private IEnumerator ParallelMusicListExec(List assetList) { assetList = FilterAssetList(assetList); int totalJobs = assetList.Count; int finishedJobs = 0; while (true) { int currentJobs = assetList.Count; for (int i = 0; i < currentJobs; i++) { StartCoroutine(CacheMusic(assetList[i], delegate { finishedJobs++; })); } while (finishedJobs < totalJobs) { yield return (object)0; } yield break; } } /// /// アセットリストを読み込む /// public IEnumerator LoadAssetGroup(List assetList) { yield return LoadAssetGroupAsync(assetList, null); yield return WaitLoadResource(); } /// /// アセットリストを非同期で読み込み /// /// /// /// public IEnumerator LoadAssetGroupAsync(List assetList, Action callback) { while (loadSemaphore) { yield return (object)null; } SetLoadProcessLock(true); List soundAssetList = null; List otherAssetList = null; for (int i = 0; i < assetList.Count; i++) { string item = assetList[i]; if (item.EndsWith(".acb")) { if (soundAssetList == null) { soundAssetList = new List(); } soundAssetList.Add(item); } else { if (otherAssetList == null) { otherAssetList = new List(); } otherAssetList.Add(item); } } if (soundAssetList != null) { //parallelCorutines.Add(StartCoroutine(ParallelMusicListExec(soundAssetList))); //非同期で… //StartCoroutine(ParallelMusicListExec(soundAssetList)); } if (otherAssetList != null) { parallelCorutines.Add(StartCoroutine(ParallelAssetListExec(otherAssetList))); } yield return (object)new WaitForFixedUpdate(); SetLoadProcessLock(false); if (callback != null) { callback(); } } public IEnumerator WaitLoadResource() { for (int i = 0; i < parallelCorutines.Count; i++) { yield return parallelCorutines[i]; } parallelCorutines.Clear(); } public bool ExistsAssetBundleManifest(string assetName) { if (AssetManager.instance.CheckExistFileInManifest(assetName)) { return true; } return false; } /// /// Objectを取得する /// Objectが格納されたAssetBundleはあらかじめLoadAssetしておく必要がある /// public UnityEngine.Object LoadObject(string objectName) { return LoadObject(objectName); } /// /// Objectを特定の型で取得する /// Objectが格納されたAssetBundleはあらかじめLoadAssetしておく必要がある /// public T LoadObject(string objectName) where T : UnityEngine.Object { object obj = LoadObject(objectName, typeof(T)); if (obj != null) { return (T)obj; } return null; } private object LoadObject(string objectName, Type type) { if (string.IsNullOrEmpty(objectName)) { return null; } string searchKey = objectName.ToLower(); foreach (KeyValuePair item in objectDictionary) { AssetBundleObject value = item.Value; for (int i = 0; i < value.objectArray.Count; i++) { if (value.objectArray[i].basePath.Contains(searchKey) && value.objectArray[i].baseObject != null && (type == typeof(UnityEngine.Object) || value.objectArray[i].baseObject.GetType() == type)) { return value.objectArray[i].baseObject; } } } Debug.Log("Object Missing!: " + objectName); return null; } /// /// オブジェクトが読み込み可能かを確認する /// public bool CheckLoadObject(string objectName, Type type = null) { if (string.IsNullOrEmpty(objectName)) { return false; } string searchKey = objectName.ToLower(); foreach (KeyValuePair item in objectDictionary) { AssetBundleObject value = item.Value; for (int i = 0; i < value.objectArray.Count; i++) { if (value.objectArray[i].basePath.Contains(searchKey) && value.objectArray[i].baseObject != null) { if (type != null) { if (value.objectArray[i].baseObject.GetType() == type) { return true; } } return true; } } } return false; } private void SetLoadProcessLock(bool enable_lock) { loadSemaphore = enable_lock; } public IEnumerator GameInitialize() { while (!AssetManager.instance.isManifestLoad) yield return null; while (!MasterDBManager.instance.isLoadDB) yield return null; } public Shader GetShader(string shadername) { Shader shader = null; if (shaderDictionary.TryGetValue(shadername.ToLower(), out shader)) { return shader; } print("ShaderNotfound:" + shadername); return null; } public bool CheckAudioClip(string audioname) { return audioclipDictionary.ContainsKey(audioname.ToLower()); } public AudioClip GetAudioClip(string audioname) { AudioClip audioClip = null; if (audioclipDictionary.TryGetValue(audioname.ToLower(), out audioClip)) { return audioClip; } print("AudioNotfound:" + audioname); return null; } /// /// Assetファイルをロードして辞書へ登録する /// 複数ファイル読み込む場合はLoadAssetGroupAsyncを使用する /// public IEnumerator LoadAsset(string assetName, Action callback) { if (!bundleDictionary.ContainsKey(assetName)) { //マニフェストにないなら終了 if (!AssetManager.instance.CheckExistFileInManifest(assetName)) { if (callback != null) { callback(); } yield break; } float timeOut = 0f; //Localのファイル確認 while (!AssetManager.instance.CheckFileFromFilename(assetName)) { //タイムアウト10秒 timeOut += Time.deltaTime; if (timeOut > 10f) { if (callback != null) { callback(); } yield break; } yield return null; } //非同期で読み込み AssetBundleCreateRequest req = AssetManager.instance.LoadAssetFromNameAsync(assetName); if (req == null) { if (callback != null) { callback(); } yield break; } //待ち yield return req; AssetBundle bundle = req.assetBundle; List registlist = new List(); string[] pathlist; pathlist = bundle.GetAllAssetNames(); foreach (string AssetNames in pathlist) { UnityEngine.Object obj = bundle.LoadAsset(AssetNames); if(obj != null) { registlist.Add(new AssetObject(AssetNames, obj)); //shaderリストに追加 if (obj is Shader) { if (obj.name != "") { string str = Path.GetFileNameWithoutExtension(obj.name.ToLower()); if (!shaderDictionary.ContainsKey(str)) { shaderDictionary.Add(str, obj as Shader); } } } } } SetAssetDic(assetName, bundle, registlist); bundleDictionary.Add(assetName, bundle); } if (callback != null) { callback(); } } /// /// 音楽をロードする /// private IEnumerator CacheMusic(string musicName, Action callback) { if (!audioclipDictionary.ContainsKey(musicName)) { //マニフェストにないなら終了 if (!AssetManager.instance.CheckExistFileInManifest(musicName)) { if (callback != null) { callback(); } yield break; } float timeOut = 0f; //Localのファイル確認 while (!AssetManager.instance.CheckFileFromFilename(musicName)) { //タイムアウト20秒 timeOut += Time.deltaTime; if (timeOut > 20f) { if (callback != null) { callback(); } yield break; } yield return null; } //キャッシュファイル作成およびパスの取得 string musicfile = PathHandler.instance.GetCachePath(musicName); while (!File.Exists(musicfile)) { yield return null; } musicfile = @"file:///" + musicfile; //print(musicfile); yield return AssetManager.instance.WaitACBDecodeProgress(); using (WWW www = new WWW(musicfile)) { // ロード完了まで待機 yield return www; AudioClip audioclip; audioclip = www.GetAudioClip(); // ロード開始まで待機 while (audioclip.loadState == AudioDataLoadState.Unloaded) { yield return null; } audioclipDictionary.Add(musicName, audioclip); } } if (callback != null) { callback(); } } private void SetAssetDic(string filename, AssetBundle assetbundle, List objectList) { AssetBundleObject value; if (objectDictionary.TryGetValue(filename, out value)) { for (int i = 0; i < value.objectArray.Count; i++) { value.objectArray[i].DestroyImmediate(); } value.objectArray.Clear(); value.Unload(true); value.SetAssetBundle(assetbundle); value.objectArray = objectList; } else { value = new AssetBundleObject(); value.SetAssetBundle(assetbundle); value.objectArray = objectList; objectDictionary.Add(filename, value); } } }