IEnumerator OnLoadAsset <T>(string abname, string assetName, Action <T> func) where T : UnityEngine.Object { // Load asset string abName = abname.ToLower() + "." + Global.BundleExtName; AssetBundleOperation request = this.LoadAssetAsync(abName, assetName, typeof(T)); if (request == null) { yield break; } yield return(StartCoroutine(request)); // Get asset T prefab = request.GetAsset <T>(); if (func != null) { func(prefab); } else { DebugConsole.LogError("assetbundle load failed:" + "bundle[" + abname + "]" + " asset[" + assetName + "]"); } }
/// <summary> /// アセットバンドルの状態に応じた処理 /// </summary> private void UpdateAssetBundleOperation(AssetBundleOperation data) { var status = data.GetStatus(); switch (status) { //ダウンロードが必要 case AssetBundleOperation.Status.isNeedDownload: { //ダウンロード開始 data.DownloadAssetBundle(this, this.serverAssetBundleDirectoryUrl, (bytes) => { //ダウンロードしたアセットバンドルをローカルに保存しない場合 if (this.dontCacheAssetBundle) { //メモリからアセットバンドルを読み込む data.LoadAssetBundleFromMemory(bytes, () => { this.UpdateAssetBundleOperation(data); }); } //ダウンロードしたアセットバンドルをローカルに保存する場合 else { //保存先ディレクトリを作成 Directory.CreateDirectory(Path.GetDirectoryName(data.path)); //アセットバンドルを保存 File.WriteAllBytes(data.path, bytes); //ダウンロードしたのでCRC値を更新 data.UpdateCRC(); //更新内容を保存 this.SaveResourceList(); //次の処理へ this.UpdateAssetBundleOperation(data); } }); } break; //ダウンロード済み case AssetBundleOperation.Status.isDownloaded: { //ローカルファイルからアセットバンドルを読み込む data.LoadAssetBundleFromFile(() => { this.UpdateAssetBundleOperation(data); }); } break; //読み込み済み case AssetBundleOperation.Status.isLoaded: { data.LoadAsset(); } break; } }
public AssetBundleOperation LoadAssetAsync(string assetBundleName, string assetName, System.Type type) { AssetBundleOperation operation = null; DownLoadAssetBundle(assetBundleName); operation = new AssetBundleOperation(assetBundleName, assetName, type); _bundleOperations.Add(operation); //添加进处理中列表,等Update处理 return(operation); }
/// <summary> /// リソースリストのダウンロード /// </summary> protected IEnumerator DownloadResourceList(Action <int> onFinished = null) { //CSVダウンロード using (var www = new WWW(this.serverResourceListUrl)) { //ダウンロード完了を待つ yield return(www.WaitOrTimeout()); //タイムアウト if (!www.isDone) { Debug.LogError("タイムアウト"); } //エラー else if (!string.IsNullOrEmpty(www.error)) { Debug.LogWarning("リソースリストのダウンロードに失敗 : " + www.error); onFinished.SafetyInvoke(-1); } else { //CSV読み込み using (var stream = new MemoryStream(www.bytes)) using (var reader = new StreamReader(stream)) { string line = null; while ((line = reader.ReadLine()) != null) { string[] lineSplit = line.Split(','); string name = lineSplit[0]; if (this.resourceList.ContainsKey(name)) { this.resourceList[name].UpdateFromCsv(lineSplit); } else { var data = new AssetBundleOperation(lineSplit); this.resourceList.Add(name, data); } } } if (!this.dontCacheAssetBundle) { //更新内容を保存 this.SaveResourceList(); } //コールバック実行 onFinished.SafetyInvoke(1); } } }
/// <summary> /// リソースリスト読み込み /// </summary> protected void LoadResourceList() { //ファイル存在チェック if (File.Exists(this.localResourceListPath)) { //ファイル読み込み using (var stream = new MemoryStream(File.ReadAllBytes(this.localResourceListPath))) using (var reader = new BinaryReader(stream)) { while (!reader.IsEnd()) { var data = new AssetBundleOperation(reader); this.resourceList.Add(data.name, data); } } } }
protected IEnumerator OnLoadLevel(string abname, string levelName, bool isAdditive, Action <GameObject> callback = null) { // Load asset from assetBundle. string abName = abname.ToLower() + ".unity3d"; // Load level from assetBundle. AssetBundleOperation request = ResourceManager.LoadLevelAsync(abName, levelName, isAdditive); if (request == null) { yield break; } yield return(StartCoroutine(request)); UnloadAssetBundle(abName); // This log will only be output when loading level additively. Debug.Log("Finish loading scene " + levelName + " at frame " + Time.frameCount); }
/// <summary> /// シーンアセットバンドルの読み込み /// </summary> /// <param name="assetBundleName">アセットバンドル名</param> /// <param name="onLoad">読み込み完了時コールバック</param> public void LoadScenePaths(string assetBundleName, Action <string[]> onLoad = null) { if (!this.CheckAssetBundleExists(assetBundleName)) { onLoad.SafetyInvoke(null); return; } AssetBundleOperation data = this.resourceList[assetBundleName]; var assetOperation = data.FindAssetOperation <SceneAssetOperation>(); //初めての読み込み if (assetOperation == null) { //アセット管理データさくせい assetOperation = new SceneAssetOperation(onLoad); data.AddAssetOperation(assetOperation); //読み込み開始 this.UpdateAssetBundleOperation(data); } //ロード済み else if (assetOperation.GetStatus() == AssetOperationBase.Status.isLoaded) { //1フレーム後にコールバック実行 StartCoroutine(CoroutineUtility.WaitForFrameAction(1, () => { onLoad.SafetyInvoke(assetOperation.GetAllScenePaths()); })); } //ロード中 else { //コールバック追加 assetOperation.AddCallBack(onLoad); } }