/// <summary> /// 参照ユーザーの追加 /// </summary> public void AddReferenceUser(AssetBundleLoadHandle user) { if (!this.referenceUsers.Contains(user)) { this.referenceUsers.Add(user); } }
/// <summary> /// 破棄 /// </summary> public void Unload(AssetBundleLoadHandle user) { //参照ユーザーの解除 this.referenceUsers.Remove(user); //参照ユーザーがいなくなったら if (this.referenceUsers.Count == 0) { //アセットバンドルがロード済みならアセットバンドルを破棄 if (this.request != null && this.request.isDone) { this.request.assetBundle.Unload(true); this.request = null; } //自身をリストから除去 assetBundles.Remove(this); } }
/// <summary> /// ロード /// </summary> public static AssetLoadHandle Load <T>(string path, Action <T> onLoaded = null) where T : UnityEngine.Object { //既に読み込みがかかっているか検索 AssetLoadHandle handle = FindHandle <T>(path); //ハンドルが既存だった場合 if (handle != null) { //参照カウンタ増加 handle.referenceCount++; //コールバックを積む callbacks.Add(new Tuple <AssetLoadHandle, Action>(handle, () => { if (onLoaded != null) { onLoaded((T)handle.asset); } })); //ハンドルがロード済みなら積まれているコールバックを順に消化 if (!handle.keepWaiting) { GetInstance().StartCoroutine(InvokeCallbackNextFrame());//1フレ後に処理したい } } //ハンドルが存在しなかったら else { bool isSubAsset = false; var info = FindAssetBundleInfo(path, out isSubAsset); //AssetBundleの場合 if (info != null) { //ハンドル作成 → ロード完了したら積まれているコールバックを順に消化 handle = new AssetBundleLoadHandle(path, info, isSubAsset, typeof(T), InvokeCallback, OnError); } //Resourcesの場合 else { //ハンドル作成 → ロード完了したら積まれているコールバックを順に消化 handle = new ResourcesLoadHandle(path, typeof(T), InvokeCallback, OnError); } //参照カウンタ増加 handle.referenceCount++; handles.Add(handle); //コールバックを積む callbacks.Add(new Tuple <AssetLoadHandle, Action>(handle, () => { if (onLoaded != null) { onLoaded((T)handle.asset); } })); //ロード開始 LoadStartIfCan(); } return(handle); }