Addressables.LoadAsset(s)Async
原文地址:Addressables.LoadAsset(s)Async | Addressables | 1.16.19 (unity3d.com)
Addressables.LoadAssetAsync
static AsyncOperationHandle<TObject> LoadAssetAsync<TObject>(object key)
static AsyncOperationHandle<TObject> LoadAssetAsync<TObject>(IResourceLocation location)
- 代码示例:
-
IEnumerator LoadGameObjectAndMaterial() { //Load a GameObject AsyncOperationHandle<GameObject> goHandle = Addressables.LoadAssetAsync<GameObject>("gameObjectKey"); yield return goHandle; if(goHandle.Status == AsyncOperationStatus.Succeeded) { GameObject obj = goHandle.Result; //etc... } //Load a Material AsyncOperationHandle<IList<IResourceLocation>> locationHandle = Addressables.LoadResourceLocationsAsync("materialKey"); yield return locationHandle; AsyncOperationHandle<Material> matHandle = Addressables.LoadAssetAsync<Material>(locationHandle.Result[0]); yield return matHandle; if (matHandle.Status == AsyncOperationStatus.Succeeded) { Material mat = matHandle.Result; //etc... } //Use this only when the objects are no longer needed Addressables.Release(goHandle); Addressables.Release(matHandle); }
Addressables.LoadAssetsAsync
static AsyncOperationHandle<IList<TObject>> LoadAssetsAsync<TObject>(object key, Action<TObject> callback)
static AsyncOperationHandle<IList<TObject>> LoadAssetsAsync<TObject>(object key, Action<TObject> callback, bool releaseDependenciesOnFailure)
static AsyncOperationHandle<IList<TObject>> LoadAssetsAsync<TObject>(IEnumerable keys, Action<TObject> callback, MergeMode mode)
static AsyncOperationHandle<IList<TObject>> LoadAssetsAsync<TObject>(IEnumerable keys, Action<TObject> callback, MergeMode mode, bool releaseDependenciesOnFailure)
static AsyncOperationHandle<IList<TObject>> LoadAssetsAsync<TObject>((IList<IResourceLocation> locations, Action<TObject> callback))
static AsyncOperationHandle<IList<TObject>> LoadAssetsAsync<TObject>((IList<IResourceLocation> locations, Action<TObject> callback, bool releaseDependenciesOnFailure))
代码示例:
-
IEnumerator LoadAllLocations(List<IResourceLocation> locations) { //Will load all assets for the provided IResourceLocations AsyncOperationHandle<IList<GameObject>> loadWithIResourceLocations = Addressables.LoadAssetsAsync<GameObject>(locations, obj => { //Gets called for every loaded asset Debug.Log(obj.name); }); yield return loadWithIResourceLocations; IList<GameObject> loadWithLocationsResult = loadWithIResourceLocations.Result; //Will load all assets for the provided IResourceLocations //With false passed in as the last parameter the Result will be populated with //objects that could be successfully loaded, even if others could not. AsyncOperationHandle<IList<GameObject>> doNotReleaseOnFailWithIResourceLocations = Addressables.LoadAssetsAsync<GameObject>(locations, obj => { //Gets called for every loaded asset Debug.Log(obj.name); }, false); yield return doNotReleaseOnFailWithIResourceLocations; IList<GameObject> multipleKeyResult = doNotReleaseOnFailWithIResourceLocations.Result; //Use this only when the objects are no longer needed Addressables.Release(loadWithIResourceLocations); Addressables.Release(doNotReleaseOnFailWithIResourceLocations); } IEnumerator LoadAllAssetsByKey() { //Will load all objects that match the given key. //If this key is an Addressable label, it will load all assets marked with that label AsyncOperationHandle<IList<GameObject>> loadWithSingleKeyHandle = Addressables.LoadAssetsAsync<GameObject>("objectKey", obj => { //Gets called for every loaded asset Debug.Log(obj.name); }); yield return loadWithSingleKeyHandle; IList<GameObject> singleKeyResult = loadWithSingleKeyHandle.Result; //Loads all assets that match the list of keys. //With no MergeMode parameter specified, the Result will be that of the first key. AsyncOperationHandle<IList<GameObject>> loadWithMultipleKeys = Addressables.LoadAssetsAsync<GameObject>(new string[] { "key1", "key2" }, obj => { //Gets called for every loaded asset Debug.Log(obj.name); }); yield return loadWithMultipleKeys; IList<GameObject> multipleKeyResult1 = loadWithMultipleKeys.Result; //This will load the assets that match the given keys and populate the Result //with only objects that match both of the provided keys AsyncOperationHandle<IList<GameObject>> intersectionWithMultipleKeys = Addressables.LoadAssetsAsync<GameObject>(new string[] { "key1", "key2" }, obj => { //Gets called for every loaded asset Debug.Log(obj.name); }, Addressables.MergeMode.Intersection); yield return intersectionWithMultipleKeys; IList<GameObject> multipleKeyResult2 = intersectionWithMultipleKeys.Result; //This will load all objects that match either of the provided keys since we passed in //MergeMode.Union. It will also populate any successfully loaded objects into the //Result property even if others fail because of the final parameter being false. AsyncOperationHandle<IList<GameObject>> unionWithMultipleKeysDoNotRelease = Addressables.LoadAssetsAsync<GameObject>(new string[] { "key1", "key2" }, obj => { //Gets called for every loaded asset Debug.Log(obj.name); }, Addressables.MergeMode.Union, false); yield return unionWithMultipleKeysDoNotRelease; IList<GameObject> multipleKeyResult3 = unionWithMultipleKeysDoNotRelease.Result; //Use this only when the objects are no longer needed Addressables.Release(loadWithSingleKeyHandle); Addressables.Release(loadWithMultipleKeys); Addressables.Release(intersectionWithMultipleKeys); Addressables.Release(unionWithMultipleKeysDoNotRelease); }