【Unity】Addressables Resource Management Notes

【Unity】Addressables Resource Management Notes

Addressables is a system for managing resources. Allows a flexible way to load, unload and manage assets, whether scenes, prefabs, materials, textures, audio clips, etc.

1. Quick implementation

  1. Install the Addressables plugin

  2. Add object to group

    1) Open the AddressablesGroups panel

    window→AssetManagement→Addressables→Grounps

    2) Create environment prefab into group

    After packaging the prefab, drag it directly to the default group (Default Local Group)

    Here you can see that the prefab automatically adds an addressing path. Once you get this path, you can dynamically load the resource.

  3. Load object

    Simple resource loading

        void Update()
        {
            if (Input.GetKeyDown(KeyCode.C))
            {
                AddAsset();
            }
        }
        void AddAsset()
        {
             Addressables.LoadAssetAsync<GameObject>("Assets/Prefab/Environment_Prefab.prefab").Completed +=
                (asyncOperationHandle) =>
                {
                    if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
                    {
                        Instantiate(asyncOperationHandle.Result);
                    }
                    else
                    {
                        Debug.Log("加载失败");
                    }
                };
        }
    

2. Loading method

  1. Load according to path

     void AddAsset()
        {
          
          
            asyncOperationHandle = Addressables.LoadAssetAsync<GameObject>("Assets/Prefab/Environment_Prefab.prefab");
            asyncOperationHandle.Completed += AsyncOperationHandle_Completed;
        }
        void AsyncOperationHandle_Completed(AsyncOperationHandle<GameObject> asyncOperationHandle)
        {
          
          
            if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
            {
          
          
                Instantiate(asyncOperationHandle.Result);
            }
            else
            {
          
          
              Debug.Log("加载失败");
            }
        }
    
  2. Load according to tag

        [SerializeField] AssetLabelReference labelReference;
        void AddAssetForAssetLabelReference()
        {
            Addressables.LoadAssetAsync<GameObject>(labelReference).Completed +=
                (asyncOperationHandle) =>
                {
                    if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
                    {
                        Instantiate(asyncOperationHandle.Result);
                    }
                    else
                    {
                        Debug.Log("加载失败");
                    }
                };
        }
    
  3. Specify prefab loading

     [SerializeField] AssetReference assetReference;
     void AddAssetForAssetReference()
        {
            assetReference.LoadAssetAsync<GameObject>().Completed+=
                    (asyncOperationHandle) =>
                    {
                        if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
                        {
                            game= asyncOperationHandle.Result;
                            Instantiate(game);
                        }
                        else
                        {
                            Debug.Log("加载失败");
                        }
                    };
          
        }
    
  4. Load multiple resources

        void AddAssetForAssetLabelReferences()
        {
            Addressables.LoadAssetsAsync<GameObject>(labelReference, (addAesst) => { Instantiate(addAesst); })
                .Completed +=
                (asyncOperationHandle) =>
                {
                    if (asyncOperationHandle.Status == AsyncOperationStatus.Succeeded)
                    {
                        Debug.Log("加载成功");
                    }
                    else
                    {
                        Debug.Log("加载失败");
                    }
                };
        }
    
  5. Load creation and unloading

       
    GameObject game;
        void AddAssetForAssetInstantiateAsync()
        {
              assetReference.InstantiateAsync().Completed += (asyncOperation) => game = asyncOperation.Result;
        }
        void ClearInstantiateAsset()
        {
            //该方法只适用于InstantiateAsync创建的资源
            assetReference.ReleaseInstance(game);
            GC.Collect();
        }
    
  6. Remote loading

    1) Turn on remote

    window→AssetManagement→Addressables→Setting

    Check BuildRemoteCatalog

    Build&Load Paths Select Remote

Select the resource groupAddressables Asset Groupp

Build&Load Paths Select Remote

2) Modify configuration file

window→AssetManagement→Addressables→Profiles

Remote.BuildPath is the creation path

Remote.LoadPath is the remote address

[BuildTarget] is the release type folder, such as webgl and StandaloneWindows64

Multiple configuration files can be created here

3) Publish

After publishing, you can find the resource files in the corresponding folder.

When publishing files online, you need to copy the entire folder to the address

This can be seen from the configuration file

The configuration file address is usually under StreamingAssets→aa

Includes: settings.json and catalog.json

Finally, it is recommended to use AssetBundle and write the configuration and dependencies yourself.

Guess you like

Origin blog.csdn.net/dxs1990/article/details/134157526