docs.unity3d.com
    目次を表示する/隠す

    リソース URL の変換

    Addressables では、以下の方法により、ランタイム時にアセットのロードに使用する URL を変更できます。

    • プロファイル変数の静的プロパティ
    • ID 変換関数の実装
    • WebRequestOverride メソッドの実装

    静的プロファイル変数

    [RemoteLoadPath プロファイル変数] を定義するときに静的プロパティを使用すると、カタログ、カタログハッシュファイル、AssetBundle などのリモートコンテンツをアプリケーションでロードする際のロード元となる URL の全体または一部を指定できます。プロファイル変数でのプロパティ名の指定については、[プロファイル変数の構文] を参照してください。静的プロパティの値は、Addressables が初期化される前に設定する必要があります。この値を初期化後に変更しても効果はありません。

    ID 変換関数

    Addressables.ResourceManager オブジェクトの InternalIdTransformFunc プロパティに関数を割り当てると、Addressables がアセットをロードする際のロード元となる URL を個別に変更できます。この関数は、関連する操作が開始される前に割り当てる必要があります。それ以外の場合は、デフォルトの URL が使用されます。

    TransformInternalId を使用すると、特にリモートホスティングに関して大幅に柔軟性が向上します。提供される単一の IResourceLocation を通じて、ランタイム時に指定したサーバーを指すように ID を変換できます。これは、サーバーの IP アドレスが変わる場合や、異なる URL を使用してアプリケーションアセットのさまざまなバリアントを提供する場合に特に便利です。

    ResourceManager は、アセットの検索時に TransformInternalId 関数を呼び出して、アセットの IResourceLocation インスタンスをこの関数に渡します。この IResourceLocation の InternalId プロパティを変更し、変更したオブジェクトを ResourceManager に返すことができます。

    以下の例は、AssetBundle のすべての URL にクエリ文字列を付加する方法を示しています。

    
    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 のオーバーライド

    Addressables オブジェクトの WebRequestOverride プロパティに関数を割り当てると、AssetBundle やカタログ json ファイルなどのファイルのダウロードに使用される UnityWebRequest を個別に変更できます。この関数は、関連する操作が開始される前に割り当てる必要があります。それ以外の場合は、デフォルトの UnityWebRequest が使用されます。

    ResourceManager は、UnityWebRequest.SendWebRequest が呼び出される前に WebRequestOverride 関数を呼び出します。このとき、ダウンロード用の UnityWebRequest が関数に渡されます。

    以下の例は、AssetBundle とカタログのすべての URL にクエリ文字列を付加する方法を示しています。

    
    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";
        }
    }
    
    
    トップに戻る
    Copyright © 2023 Unity Technologies — 商標と利用規約
    • 法律関連
    • プライバシーポリシー
    • クッキー
    • 私の個人情報を販売または共有しない
    • Your Privacy Choices (Cookie Settings)