/// <summary> /// 読込 /// </summary> /// <param name="assetPath"></param> public static AssetLoadHandle Load <T>(ref string assetPath) { AssetLoader loader = GetAssetLoader(ref assetPath); if (loader == null) { // ロードされていない loader = new AssetLoader(ref assetPath, typeof(T)); Instance.loadList.Add(loader); return(loader); } else { // ロードされている AssetLoadHandle handle = GetAssetLoadHandle(ref assetPath); if (handle == null) { // まだハンドルがなければ、ローダーを渡す loader.AddRefCount(); return(loader); } else { // ハンドルがあれば、それを渡す loader = handle as AssetLoader; loader.AddRefCount(); return(handle); } } }
/// <summary> /// OnGUI /// </summary> private void OnGUI() { if (this.errorHandle != null) { //エラーしたハンドルのパス GUILayout.Label(this.errorHandle.path); //エラー内容 GUILayout.Label(this.errorHandle.errorStatus.ToString()); //エラー解消出来たならリトライさせることも出来る(エラーの種類にもよる) if (GUILayout.Button("Retry")) { this.errorHandle = null; AssetManager.Retry(); } //エラー解消出来なかったり、何度リトライしてもダメだったらタイトルに戻らせる if (GUILayout.Button("BackToTitle")) { this.errorHandle = null; AssetManager.UnloadAll(true); AssetManager.Clear(); } } }
/// <summary> /// クリア(タイトル戻るときとか) /// </summary> public static void Clear() { //アセットバンドル情報一覧のクリア if (assetBundleInfoList != null) { assetBundleInfoList.Clear(); assetBundleInfoList = null; } //エラーハンドル解放 errorHandle = null; }
/// <summary> /// アセットを取得 /// </summary> /// <param name="assetPath"></param> /// <returns></returns> public static AssetLoadHandle GetAssetLoadHandle(ref string assetPath) { int hash = assetPath.GetHashCode(); if (Instance.assets.ContainsKey(hash)) { AssetLoadHandle handle = Instance.assets[hash]; return(handle); } return(null); }
/// <summary> /// 破棄 /// </summary> public static void Unload(ref AssetLoadHandle handle) { AssetLoader loader = handle as AssetLoader; if (handle.RefCount > 0) { loader.RemoveRefCount(); return; } loader.Unload(); Instance.assets.Remove(handle.UniqueID); handle = null; }
/// <summary> /// ハンドルエラー時 /// </summary> private static void OnError(AssetLoadHandle handle) { if (errorHandle == null) { errorHandle = handle; for (int i = 0; i < handles.Count; i++) { //ロード中のハンドルを一時停止させる handles[i].Stop(ErrorNotificationIfCan); } //可能ならエラー通知 ErrorNotificationIfCan(); } }
/// <summary> /// アンロード /// </summary> public static bool Unload(AssetLoadHandle handle) { if (handles.Contains(handle)) { //参照カウンタ減少 handle.referenceCount--; if (handle.IsUnloadable()) { handle.Unload(); handles.Remove(handle); callbacks.RemoveAll(x => x.Item1 == handle); return(true); } //Debug.LogWarningFormat("破棄不可:{0}, status={1}, isDontDestroy={2}, referenceCount={3}", handle.path, handle.status, handle.isDontDestroy, handle.referenceCount); } return(false); }
private void Start() { //testQuad1とtestQuad2は同じマテリアル「Materials/test.mat」を参照している。 //testQuad1, testQuad2, test.matは3つバラバラのアセットバンドルになっているため、 //testQuad1とtestQuad2からtest.matに対して依存関係が発生している。 //test.matをロードしなければtestQuad1とtestQuad2は正しく表示されないが、 //依存関係を自動でロードするので、わざわざtest.matをロードしなくても大丈夫。 this.handle1 = AssetManager.Load <GameObject>("Prefabs/testQuad1", (asset) => { Instantiate(asset); }); this.handle2 = AssetManager.Load <GameObject>("Prefabs/testQuad2", (asset) => { Instantiate(asset); }); }
/// <summary> /// リトライ /// </summary> public static void Retry() { if (errorHandle != null) { //エラーしたハンドルのリトライ var tmpErrorHandle = errorHandle; errorHandle = null; tmpErrorHandle.Restart(); //Restartしたハンドルがエラーした場合、OnErrorが呼ばれてerrorHandleに値が入るので //errorHandleがnullじゃなくなった時点でRestart処理を中止する for (int i = 0; i < handles.Count && errorHandle == null; i++) { handles[i].Restart(); } //スレッドに余裕があるなら未処理ハンドルのロードを開始 for (int i = 0; i < THREAD_MAX && errorHandle == null; i++) { LoadStartIfCan(); } } }
/// <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); }
/// <summary> /// キャンセル /// </summary> /// <para> /// キャンセルとは名ばかりで、コールバックは呼ばれなくなるがロードは完了まで行われる。 /// そのため、キャンセルしたハンドルもちゃんとUnloadをしてやらないとメモリに残り続けてしまう。 /// シーン遷移時とかにAssetManagerのロードが走っていないことを確認しつつUnloadAllで全消しする処理が必要。 /// </para> public static void CancelHandle(AssetLoadHandle handle) { callbacks.RemoveAll(x => x.Item1 == handle); }
/// <summary> /// ロード /// </summary> public AssetLoadHandle Load(Action onLoaded = null) { return(this.handle = AssetManager.Load <T>(path, (_) => onLoaded?.Invoke())); }
/// <summary> /// 必要リソースの読み込み /// </summary> private void Load() { //WAVEデータ読み込み this.waveDataHandle = AssetManager.Load<FishWaveData>(SharkDefine.GetFishWaveDataPath(this.stageData.key), (asset) => { //WAVE準備 this.waveDataController = new FishWaveDataController(new Fish.ID(), asset, 0f); this.waveDataController.onFinished = this.OnAllWaveFinished; //背景リソース読み込み this.loader.Add<Sprite>(SharkDefine.GetBattleBgSpritePath(this.worldData.key)); //WAVE必要リソース読み込み this.loader.AddRange(this.waveDataController.loader); //バトルアイテムアイコンリソース読み込み this.battleItemIconManager.Set( userItemDatas: new UserItemData[]{ new UserItemData{ itemType = ItemType.BattleItem, itemId = this.stageData.itemId1, stockCount = this.stageData.amount1 }, new UserItemData{ itemType = ItemType.BattleItem, itemId = this.stageData.itemId2, stockCount = this.stageData.amount2 }, }, onClick: this.OnClickItemIcon ); this.battleItemIconManager.LoadIfNeed(); //BGM読み込み this.loader.Add<BgmClipData>(SharkDefine.GetBgmClipPath(this.worldData.bgmName)); //SE読み込み this.loader.Add<AudioClip>(SharkDefine.GetSeClipPath(SeName.CAPTURE_SINGLE)); this.loader.Add<AudioClip>(SharkDefine.GetSeClipPath(SeName.FVATTACK_OK)); this.loader.Add<AudioClip>(SharkDefine.GetSeClipPath(SeName.FVATTACK_START)); //砲台リソース読み込み this.loader.AddRange(this.turret.loader); //ロード this.loader.Load(); //ロード完了待ち StartCoroutine(new WaitWhile(AssetManager.IsLoading).AddCallback(() => { //ローディング表示消す SharedUI.Instance.HideSceneChangeAnimation(); //BGM再生 this.bgmTrack = SoundManager.Instance.PlayBgm(this.worldData.bgmName); //背景 this.bgImage.sprite = this.loader[SharkDefine.GetBattleBgSpritePath(this.worldData.key)].handle.asset as Sprite; //バトルWAVEセットアップ this.waveDataController.Setup(); //アイテムアイコンセットアップ this.battleItemIconManager.Setup(); //砲台セットアップ this.turret.Setup(); //FVアタックゲージセットアップ this.uiFvAttackGauge.Setup(); //魚ゲージセットアップ this.uiFishGauge.Setup(this.waveDataController.master); //準備完了したらバトル開始 this.stateManager.ChangeState<BattleStartState>(); })); }); }
public override void Release(AssetLoadHandle handle) { _loader.Release(handle); }
/// <summary> /// アセットロードエラー時 /// </summary> private void OnAssetLoadError(AssetLoadHandle errorHandle) { this.errorHandle = errorHandle; }