Skip to content

preLoadGameWithDirectoryPath:options:completion:

预加载游戏(通过本地目录路径)

该方法允许开发者通过指定一个已经解压完成的游戏资源目录(Directory Path)来执行预加载。与文件路径加载不同,该接口适用于游戏资源已经以解压后的文件夹形式存在于本地文件系统(如 App Bundle 内或沙盒的 Library 目录)的场景。SDK 将直接映射该目录下的资源,跳过解压过程,实现最快的环境就绪。


方法声明

objc
/**
 Pre-load game using a local directory path.
 @param directoryPath the local directory path.
 @param options Configuration object containing  and metadata.
 @param completion Called when the pre-load operation finishes.
 @return An instance of id<SUDOPGameTask> to track the pre-load task.
 */
+ (id<SUDOPGameTask>)preLoadGameWithDirectoryPath:(NSString *)directoryPath
                                          options:(SUDOPGamePackageOptions *)options
                                       completion:(nullable SUDOPGameOperationCompletion)completion;

参数说明

参数名类型必传描述
directoryPathNSString *包含目录绝对路径 path
optionsSUDOPGamePackageOptions *版本 version 以及游戏标识等信息的配置对象。
completionSUDOPGameOperationCompletion异步预加载完成后的回调。成功时返回可获取句柄的 provider

返回值

返回一个遵循 id<SUDOPGameTask> 协议的对象,用于监控或取消预加载任务。


运行行为

  1. 目录结构校验:SDK 会检查 directoryPath 目录下的核心入口文件(如 index.jsonmain.js)是否存在,以确认目录的完整性。
  2. 直接加载:由于资源已经是解压状态,SDK 将直接初始化脚本虚拟机并关联该目录,从而极大缩短预加载所需的时间。
  3. 持久化关联:在此模式下,SDK 默认开发者会负责该目录的生命周期,建议在游戏运行期间不要修改目录内容。

代码示例

objectivec
SUDOPGamePackageOptions *options = [[SUDOPGamePackageOptions alloc] init];
// 假设资源位于 App Main Bundle 的内置文件夹中
NSString *resourcePath = [[NSBundle mainBundle] pathForResource:@"GameRes" ofType:nil];

options.appGameID = @"game_builtin_001";
options.version = @"1.0.0";

[SUDOP preLoadGameWithDirectoryPath:resourcePath options:options completion:nil];

注意事项

  • 路径性质directoryPath 必须指向一个文件夹而非压缩包。如果指向的是压缩文件,请使用 preLoadGameWithFilePathOptions:
  • 只读限制:如果目录位于 App Bundle 中(只读),请确保游戏逻辑本身不依赖于向资源目录写入数据。
  • 版本匹配:虽然是直接加载,但建议 options.version 与目录内的配置保持一致,以便 SDK 进行正确的版本管理和缓存决策。