Change resource URLs
You can modify the URLs that Addressables uses to load assets at runtime in the following ways:
- Static properties in a Profile variable
- Implement an ID transform method
- Implement a WebRequestOverride method
Static Profile variables
When you define the RemoteLoadPath Profile variable you can use a static property to specify all or part of the URL that your application loads remote content from, including catalogs, catalog hash files, and AssetBundles. Refer to Profile variable syntax for information about specifying a property name in a Profile variable.
The value of the static property must be set before Addressables initializes. Changing the value after initialization has no effect.
ID transform method
You can assign a method to the Addressables.ResourceManager
object's InternalIdTransformFunc
property to individually change the URLs that Addressables loads assets from. You must assign the method before the relevant operation starts, otherwise the default URL is used.
Using TransformInternalId
is useful for remote hosting. Given a single IResourceLocation
, you can transform the ID to point towards a server specified at runtime. This is useful if the server IP address changes or if you use different URLs to give different variants of application assets.
ResourceManager
calls the TransformInternalId
method when it looks up an asset, passing the IResourceLocation
instance for the asset to your method. You can change the InternalId
property of the IResourceLocation
and return the modified object to the ResourceManager
.
The following example illustrates how you can 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", System.StringComparison.Ordinal))
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 method to the Addressable
object's WebRequestOverride
property to individually change the UnityWebRequest
used to download files, such as an AssetBundle or catalog .json file. You must assign the method before the relevant operation starts, otherwise the default UnityWebRequest
is used.
The ResourceManager
calls WebRequestOverride
before UnityWebRequest.SendWebRequest
is called and passes the UnityWebRequest for the download to your method.
The following example shows how you can 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", StringComparison.OrdinalIgnoreCase))
request.url = request.url + "?customQueryTag=customQueryValue";
else if (request.url.EndsWith(".json", StringComparison.OrdinalIgnoreCase) || request.url.EndsWith(".hash", StringComparison.OrdinalIgnoreCase))
request.url = request.url + "?customQueryTag=customQueryValue";
}
}