unity Addressable API学习(一)

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);
    }
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值