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 中文文档
  3. 加载当前语言资源

实现 IResLoader 接口

用户需要新建一个非静态类, 并调用自己的资源加载器, 来实现 IResLoader 接口中的所有方法, 然后在合适的时机创建这个非静态类的实例赋值给 LocalizationSetting.loader 字段.

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);

}

注意: Load 和 LoadAsync 方法加载到的资源, 可以是资源的实例, 而 LoadAsset 方法加载到的必须资源本身, 而非实例.

用户可以参考一下 MyLocalization 的Demo中的 ResourceLoader 类的实现方式去实现 IResLoader 接口.

当用户实现了自己的 IResLoader 子类后, 可以关掉 ResourceLoader 类给 LocalizationSetting.loader 字段自动赋值的代码, 只需删除或改掉ProjectSetting.Player 中的Scripting Define Symbols 列表中的 Using_ResourceLoader 字段, 下面的代码就会失效.

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

Previous只加载当前语言对应的资源Next启用 IResLoader 子类

Last updated 2 years ago