跳至主要内容

Addressables

Unity Package: https://docs.unity3d.com/Packages/com.unity.addressables@latest/index.html
Install: com.unity.addressables

簡介

官方說明,不多解釋

The Addressables system provides tools and scripts to organize and package content for your application and an API to load and release assets at runtime.
When you make an asset "Addressable," you can use that asset's address to load it from anywhere. Whether that asset resides in the local application or on a content delivery network, the Addressable system locates and returns it.
Adopt the Addressables system to help improve your project in the following areas:

  • Flexibility: Addressables give you the flexibility to adjust where you host your assets. You can install assets with your application or download them on demand. You can change where you access a specific asset at any stage in your project without rewriting any game code.
  • Dependency management: The system automatically loads all dependencies of any assets you load, so that all meshes, shaders, animations, and other assets load before the system returns the content to you.
  • Memory management: The system unloads assets as well as loads them, counting references automatically and providing a robust profiler to help you spot potential memory problems.
  • Content packing: Because the system maps and understands complex dependency chains, it package AssetBundles efficiently, even when you move or rename assets. You can prepare assets for both local and remote deployment, to support downloadable content and reduced application size.

用法

初始化

public override async UniTask<bool> Init() {
AsyncOperationHandle<IResourceLocator> handle = Addressables.InitializeAsync();//先初始化
await handle;
return handle.Status == AsyncOperationStatus.Succeeded;
}

進階修改

自訂BuildScript 以下只有修改放置資料夾

[CreateAssetMenu(fileName = "CustomBuildScript.asset", menuName = "Addressable Assets/Data Builders/Custom Build")]
public class CustomBuildScript : BuildScriptPackedMode
{
public override string Name => "My Custom Build";

protected override string ConstructAssetBundleName(AddressableAssetGroup assetGroup, BundledAssetGroupSchema schema, BundleDetails info, string assetBundleName)
{
return "Bundle/" + base.ConstructAssetBundleName(assetGroup, schema, info, assetBundleName);
}

protected override TResult DoBuild<TResult>(AddressablesDataBuilderInput builderInput, AddressableAssetsBuildContext aaContext)
{
return base.DoBuild<TResult>(builderInput, aaContext);
}
}

Reference

Addressables