实现 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
Last updated