Transforming resource URLs
Addressables provides the following ways to modify the URLs it uses to load assets at runtime:
- Static properties in a Profile variable
- Implementing an ID transform function
- Implementing a WebRequestOverride method
Static Profile variables
You can use a static property when defining the RemoteLoadPath Profile variable to specify all or part of the URL from which your application loads remote content, including catalogs, catalog hash files, and AssetBundles. See Profile variable syntax for information about specifying a property name in a Profile variable. The value of your static property must be set before Addressables initializes. Changing the value after initialization has no effect.
ID transform function
You can assign a function to the Addressables.ResourceManager object's InternalIdTransformFunc property to individually change the URLs from which Addressables loads assets. You must assign the function before the relevant operation starts, otherwise the default URL is used.
Using TransformInternalId grants a fair amount of flexibility, especially in regards to remote hosting. Given a single IResourceLocation, you can transform the ID to point towards a server specified at runtime. This is particularly useful if your server IP address changes or if you use different URLS to provide different variants of your application assets.
The ResourceManager calls your TransformInternalId function when it looks up an asset, passing the IResourceLocation instance for the asset to your function. You can change the InternalId property of this IResourceLocation and return the modified object to the ResourceManager.
The following example illustrates how you could append a query string to all URLs for AssetBundles:
using UnityEngine.ResourceManagement.ResourceLocations;
using UnityEngine.ResourceManagement.ResourceProviders;
using UnityEngine.AddressableAssets;
static class IDTransformer
{
//Implement a method to transform the internal ids of locations
static string MyCustomTransform(IResourceLocation location)
{
if (location.ResourceType == typeof(IAssetBundleResource)
&& location.InternalId.StartsWith("http"))
return location.InternalId + "?customQueryTag=customQueryValue";
return location.InternalId;
}
//Override the Addressables transform method with your custom method.
//This can be set to null to revert to default behavior.
[RuntimeInitializeOnLoadMethod]
static void SetInternalIdTransform()
{
Addressables.InternalIdTransformFunc = MyCustomTransform;
}
}
WebRequest override
You can assign a function to the Addressables object's WebRequestOverride property to individually modify the UnityWebRequest from which is used to download files, such as an AssetBundle or catalog json file. You must assign the function before the relevant operation starts, otherwise the default UnityWebRequest is used.
The ResourceManager calls your WebRequestOverride function before UnityWebRequest.SendWebRequest is called. Passing the UnityWebRequest for the download to your function.
The following example illustrates how you could append a query string to all URLs for AssetBundles and catalogs:
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.AddressableAssets;
internal class WebRequestOverride : MonoBehaviour
{
//Register to override WebRequests Addressables creates to download
private void Start()
{
Addressables.WebRequestOverride = EditWebRequestURL;
}
//Override the url of the WebRequest, the request passed to the method is what would be used as standard by Addressables.
private void EditWebRequestURL(UnityWebRequest request)
{
if (request.url.EndsWith(".bundle"))
request.url = request.url + "?customQueryTag=customQueryValue";
else if (request.url.EndsWith(".json") || request.url.EndsWith(".hash"))
request.url = request.url + "?customQueryTag=customQueryValue";
}
}