docs.unity3d.com
Search Results for

    Show / Hide Table of Contents

    Method AddResourceLocator

    AddResourceLocator(IResourceLocator, string, IResourceLocation)

    Adds a ResourceLocator to the Addressables runtime.

    Declaration
    public static void AddResourceLocator(IResourceLocator locator, string localCatalogHash = null, IResourceLocation remoteCatalogLocation = null)
    Parameters
    Type Name Description
    IResourceLocator locator

    The locator object.

    string localCatalogHash

    The hash of the local catalog. This can be null if the catalog cannot be updated.

    IResourceLocation remoteCatalogLocation

    The location of the remote catalog. This can be null if the catalog cannot be updated.

    Remarks

    Adds a ResourceLocator to the Addressables runtime.

    After adding the resource locator to Addressables it can then be used to locate Locations via Addressables loading APIs.

    Adding new resource locators can be used to add locations and manage asset files outside of the Addressables build system.

    In the following example we have a folder in the root folder called "dataFiles" containing some json files.

    These json files are then loaded using TextDataProvider, which is a ResourceProvider used to load text files.

    Examples

    This example code creates ResourceLocationBase and adds it to the locator for each file.

    private string m_SourceFolder = "dataFiles";
    

    public void AddFileLocatorToAddressables() { if (!Directory.Exists(m_SourceFolder)) return;

     ResourceLocationMap locator = new ResourceLocationMap(m_SourceFolder + "_FilesLocator", 12);
     string providerId = typeof(TextDataProvider).ToString();
    
     string[] files = Directory.GetFiles(m_SourceFolder);
     foreach (string filePath in files)
     {
          if (!filePath.EndsWith(".json"))
               continue;
          string keyForLoading = Path.GetFileNameWithoutExtension(filePath);
          locator.Add(keyForLoading, new ResourceLocationBase(keyForLoading, filePath, providerId, typeof(string)));
     }
     Addressables.AddResourceLocator(locator);
    

    }

    Using Addressables API to load "dataFiles/settings.json" after adding the locator:

    private string m_DataFileName = "settings";
    

    public IEnumerator LoadDataUsingAddedLocator() { var loadingHandle = Addressables.LoadAssetAsync<string>(m_DataFileName); yield return loadingHandle; Debug.Log("Load completed " + loadingHandle.Status + (loadingHandle.Status == AsyncOperationStatus.Succeeded ? ", with result " + loadingHandle.Result : "")); }

    In This Article
    Back to top
    Copyright © 2025 Unity Technologies — Trademarks and terms of use
    • Legal
    • Privacy Policy
    • Cookie Policy
    • Do Not Sell or Share My Personal Information
    • Your Privacy Choices (Cookie Settings)