Implement IResLoader interface

Users need to create a static class, loaders and call his own resources, to implement all the methods in the IResLoader interface, and then at the right time to create the non-static class instance assigned to LocalizationSetting. Loader field.

public interface IResLoader {
    /// <summary>
    /// 同步加载资源。
    /// Load assets synchronously.
    /// </summary>
    /// <param name="resPath">资源加载所需要提供的路径 ( The path required for asset loading )</param>
    /// <typeparam name="T">资源类型 (asset type)</typeparam>
    /// <returns>返回加载后得到的资源 (Return the assets obtained after loading)</returns>
    T Load<T>(string resPath) where T : Object;
    /// <summary>
    /// 根据资源加载所需要提供的路径来加载Assets目录下的原始资源.
    /// Load the original Assets in the assets directory according to the path required to load the assets.
    /// </summary>
    /// <param name="resPath">资源加载所需要提供的路径 ( The path required for asset loading )</param>
    /// <returns></returns>
    T LoadAsset<T>(string resPath) where T : Object;
    /// <summary>
    /// 异步加载资源
    /// Load assets asynchronously
    /// </summary>
    /// <param name="resPath">资源加载所需要提供的路径 ( The path required for asset loading )</param>
    /// <typeparam name="T">资源类型 (asset type)</typeparam>
    /// <returns>返回加载后得到的资源 (Return the assets obtained after loading)</returns>
    Task<T> LoadAsync<T>(string resPath) where T : Object;
    /// <summary>
    /// 将 Asset 路径转换为加载所需的资源路径。
    /// 例如加载器要加载 Resources 路径下的资源,那么返回的路径就应该是 Resources 下的文件路径,不包括扩展名。
    /// 如果加载器是要加载 AssetBundle,那么资源所在的路径可能是 StreamingAssets 也可能是一个自定义的路径,那么此时就由用户根据实际情况需要生成自己所需要的路径了。
    /// 当遇到 Unity 内置的资源时, 如果无法正确获取资源加载路径的话, 请让此方法返回空. 返回空代表获取失败, 程序会以此来判断是否会取消资源关联.
    ///
    /// Convert the Asset path to the asset path required for loading.
    /// For example, if the loader wants to load resources under the Resources path, then the returned path should be the file path under Resources, excluding the extension.
    /// If the loader is to load AssetBundle, then the path where the resource is located may be StreamingAssets or a custom path, then it is up to the user to generate the path he needs according to the actual situation.
    /// Allow this method to return null when encountering a resource built into Unity if the resource load path cannot be obtained correctly. Returning a null indicates that the acquisition failed, and the program will use this to determine whether to cancel the resource association.
    ///
    /// </summary>
    /// <param name="assetPath">资源路径,由AssetDatabase获得的路径,以 Assets 开始的路径。
    /// Asset path, the path obtained from AssetDatabase, the path starting with Assets.</param>
    /// <returns>加载所需的资源路径,可提供给 Load 或 LoadAsync 方法使用的路径。
    /// 若返回空, 则代表无法获取资源加载路径.
    /// The asset path required for loading, which can be provided to the path used by the Load or LoadAsync method.
    /// If null is returned, the resource load path cannot be obtained.
    /// </returns>
    string Convert2LoadPath(string assetPath);
}

Note: The Load and LoadAsync methods load resources that can be instances of the resource, whereas the LoadAsset method loads the required resource itself, not the instance.

You can implement the IResLoader interface by referring to the ResourceLoader class in MyLocalization's Demo.

When users achieved his IResLoader subclasses, you can turn off ResourceLoader class to LocalizationSetting. Automatic loader field assignment code, Simply remove or change the Using_ResourceLoader field in the Scripting Define Symbols list in ProjectSetting.Player, and the following code will become invalid.

#if UNITY_EDITOR && Using_ResourceLoader
    [UnityEditor.Callbacks.DidReloadScripts(200)]
    public static void AutoAssign() {
        LocalizationSetting.loader = new ResourceLoader();
    }
#endif

Last updated