![]() The earlier section on AssetBundle Dependencies discussed how, in order to load an Asset from an AssetBundle, you must first load any AssetBundles that the AssetBundle depends on. 加载清单本身的操作方法与 AssetBundle 中的任何其他资源完全相同: AssetBundle assetBundle = AssetBundle.LoadFromFile(manifestFilePath) ĪssetBundleManifest manifest = assetBundle.LoadAsset("AssetBundleManifest") 以及 AssetBundleRequest request = loadedAssetBundle.LoadAllAssetsAsync() To load an asset asynchronously: AssetBundleRequest request = loadedAssetBundleObject.LoadAssetAsync(assetName) You’ll need to wait for this operation to complete before accessing the asset. Now, whereas the previously shown methods return either the type of object you’re loading or an array of objects, the asynchronous methods return an AssetBundleRequest. This returns an array with all the root Object of each Asset. 加载所有资源: Unity.Object objectArray = loadedAssetBundle.LoadAllAssets() To load a single Asset (for example the root GameObject of a Prefab): GameObject gameObject = loadedAssetBundle.LoadAsset(assetName) 通用代码片段: T objectFromBundle = bundleObject.LoadAsset(assetName) Now that you’ve successfully loaded your AssetBundle, it’s time to finally load in some Assets. See AssetBundle compression and caching for details of the Cache and the associated considerations about compression. The hash acts as version identifier for the precise build of the AssetBundle that you are requesting. The hash is available from the AssetBundle Manifest, which is described later in this section. This is done by passing the AssetBundle hash when calling UnityWebRequestAssetBundle.GetAssetBundle. Note: In order to avoid downloading the entire AssetBundle content each time this code is called you could enable caching for your AssetBundle. See AssetBundle Download Integrity and Security. But in practice the expected CRC values for your AssetBundles would be downloaded separately, or retrieved from a file, prior to downloading the AssetBundles. Var loadAsset = bundle.LoadAssetAsync("Assets/Players/MainPlayer.prefab") įor simplicity the example shows a CRC value that is hardcoded in the code. UnityWebRequest request = UnityWebRequestAssetBundle.GetAssetBundle(uri, crc) ĪssetBundle bundle = DownloadHandlerAssetBundle.GetContent(request) String uri = " uint crc = 1098980 // Expected content CRC Public class DownloadExample : MonoBehaviour This request can be passed into DownloadHandlerAssetBundle.GetContent(UnityWebRequestAssetBundle) to get the AssetBundle object for the loaded AssetBundle. It could also be ‘file://’ to access local data on platforms that do not support direct file system access. Typically the URL would be a ‘ address pointing to the file as exposed by a web service. ![]() The UnityWebRequestAssetBundle API can be called to create a special web request for downloading, caching and loading AssetBundles. You can use AssetBundle.LoadFromMemoryAsync. There are several other methods available to load an AssetBundle if it is not actually stored in a local file. Rather than blocking your app during the load process you can also use the asynchronous version, AssetBundle.LoadFromFileAsync. Var prefab = myLoadedAssetBundle.LoadAsset("MyObject") Var myLoadedAssetBundle = AssetBundle.LoadFromFile(Path.Combine(Application.streamingAssetsPath, "myassetBundle")) ĭebug.Log("Failed to load AssetBundle!") Public class LoadFromFileExample : MonoBehaviour 如何使用 LoadFromFile 的一个示例: using System.IO Loading a file that is compressed with full file compression (LZMA) using this method is less efficient because it requires that the file be fully decompressed into memory first. This API is highly-efficient when loading uncompressed and chunk compressed bundles (LZ4) from local storage because it can read content of the file incrementally, directly from disk. UnityWebRequestAssetBundle.GetAssetBundle. The UnityWebRequest support for AssetBundles, e.g.The static Load methods on the AssetBundle object, for example AssetBundle.LoadFromFile.There are two different APIs that you can use to load AssetBundles.
0 Comments
Leave a Reply. |