Version: 2023.1

AssetBundle.LoadFromFileAsync

切换到手册
public static AssetBundleCreateRequest LoadFromFileAsync (string path);
public static AssetBundleCreateRequest LoadFromFileAsync (string path, uint crc);
public static AssetBundleCreateRequest LoadFromFileAsync (string path, uint crc, ulong offset);

参数

path 文件在磁盘上的路径。
crc 未压缩内容的 CRC-32 校验和(可选)。如果该参数不为零,则加载前将内容与校验和进行比较,如果不匹配则给出错误。
offset 字节偏移(可选)。该值指定读取 AssetBundle 的起始位置。

返回

AssetBundleCreateRequest Asynchronous load request for an AssetBundle. Use assetBundle property to get an AssetBundle once it is loaded.

描述

从磁盘上的文件异步加载 AssetBundle。

The function supports bundles of any compression type. In case of LZMA compression, the data will be decompressed to the memory. See AssetBundles compression for more details.

这是加载 AssetBundle 的最快方法。

using UnityEngine;
using System.Collections;
using System.IO;

public class LoadFromFileAsyncExample : MonoBehaviour { IEnumerator Start() { var bundleLoadRequest = AssetBundle.LoadFromFileAsync(Path.Combine(Application.streamingAssetsPath, "myassetBundle")); yield return bundleLoadRequest;

var myLoadedAssetBundle = bundleLoadRequest.assetBundle; if (myLoadedAssetBundle == null) { Debug.Log("Failed to load AssetBundle!"); yield break; }

var assetLoadRequest = myLoadedAssetBundle.LoadAssetAsync<GameObject>("MyObject"); yield return assetLoadRequest;

GameObject prefab = assetLoadRequest.asset as GameObject; Instantiate(prefab);

myLoadedAssetBundle.Unload(false); } }