MyLocalization Decument
  • v1.3
    • MyLocalization English Document
      • MyLocalization Data Flow Diagram
      • Quick Start
        • Run the Demo Correctly
        • Preparation
        • Localize Text
        • Localize Texture
        • Localization of Other Types of Resources
      • Load Assets
        • Only Load Resources Corresponding to the Current Language
        • Implement IResLoader interface
        • Enable IResLoader Subclass
        • Convert Resource References Associations to Paths
        • Text Localization Items Do Not Need to Be Loaded with Language Type
      • Edit Text for Localization
        • Basic Operation
        • Automatic Translation
        • Find and Replace
        • Speech Binding Text
        • CSV Table Export and Import
        • Essay on the Importance of Text Table (StringTable)
      • Display and Setter Components
        • Correspondence between Setter Components and Display Components
        • RTL (Right To Left) Related Settings
        • Dynamic Splicing of Localized Text
        • Quickly create and modify localized field text using properties of MyString
    • MyLocalization 中文文档
      • MyLocalization 数据流程图
      • 快速开始
        • 正确运行 Demo
        • 准备工作
        • 文本本地化
        • 图片本地化
        • 其他资源的本地化
      • 加载当前语言资源
        • 只加载当前语言对应的资源
        • 实现 IResLoader 接口
        • 启用 IResLoader 子类
        • 将资源引用关联转为路径
        • 文本的本地化项不能随语言类型加载
      • 编辑文本表
        • 基本操作
        • 自动翻译
        • 搜索与替换
        • 语音绑定文字
        • CSV表格导出与导入
        • 使用 MyString 的属性快速创建和修改本地化字段文本
      • 一切准备都为了Setter组件实现本地化
        • Setter 组件与显示组件的对应关系
        • RTL (Right To Left)相关设置
        • 动态拼接本地化文本
Powered by GitBook
On this page
  1. v1.3
  2. MyLocalization English Document
  3. Load Assets

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
PreviousOnly Load Resources Corresponding to the Current LanguageNextEnable IResLoader Subclass

Last updated 2 years ago