startGameWithDerectoryPath:options:onCreated:completion:
启动游戏(通过本地目录路径)
该方法允许开发者通过指定一个已经解压完成的游戏资源目录路径正式启动游戏。SDK 会直接映射该目录下的资源并初始化运行时环境。省略了下载和解压步骤,并直接启动游戏逻辑。
方法声明
objectivec
/**
Start the game using a local directory path.
@param directoryPath the directory path.
@param options Configuration object containing the directory path and metadata.
@param didGameHandleCreated Called when the game handle is created. Use this to mount the view and register delegates.
@param completion Called when the game process has fully started.
@return An instance of id<SUDOPGameTask> to track the startup process.
*/
+ (id<SUDOPGameTask>)startGameWithDerectoryPath:(NSString *)directoryPath
options:(SUDOPGamePackageOptions *)options
didGameHandleCreated:(nullable SUDOPDidGameHandleCreated)didGameHandleCreated
completion:(nullable SUDOPGameOperationCompletion)completion;参数说明
| 参数名 | 类型 | 必传 | 描述 |
|---|---|---|---|
| directoryPath | NSString * | 是 | directoryPath (目录绝对路径) |
| options | SUDOPGamePackageOptions * | 是 | 配置对象。需包含 appGameID 和 version。 |
| didGameHandleCreated | Block | 否 | 核心回调。游戏实例创建后触发。此时应通过 gameHandle 获取游戏视图并添加到 UI 窗口。 |
| completion | Block | 否 | 最终回调。表示游戏已完成首屏初始化,用户可开始操作。 |
运行行为
- 目录映射:SDK 验证
directoryPath指向的是否为文件夹,并检查是否存在必要的入口配置文件(如index.json)。 - 即时初始化:直接启动 JavaScript 运行时,无需等待解压过程,资源读取速度取决于磁盘 IO。
- 视图交付:在
didGameHandleCreated回调中,SDK 已准备好渲染上下文,开发者可以获取UIView并进行布局。
代码示例
objectivec
SUDOPGamePackageOptions *options = [[SUDOPGamePackageOptions alloc] init];
// 指向已存在的文件夹路径
NSString * directoryPath = @"/var/mobile/Containers/Data/Application/.../Library/Caches/Game_001_v2/";
options.appGameID = @"game_001";
options.version = @"2.0.0";
[SUDOP startGameWithDerectoryPath:directoryPath Options:options
didGameHandleCreated:^(id<SUDRTGameHandle> gameHandle) {
// 1. 注册代理以接收游戏事件
[SUDOP registerWrappedClientWithGameHandle:gameHandle clientDelegate:self];
// 2. 将游戏画面挂载到当前页面
UIView *gameView = [gameHandle getGameView];
gameView.frame = self.view.bounds;
[self.view addSubview:gameView];
}
completion:^(id<SUDOPGameHandleProvider> _Nullable provider, NSError * _Nullable error) {
if (!error) {
NSLog(@"目录资源启动成功");
} else {
NSLog(@"启动失败,错误信息: %@", error);
}
}];注意事项
- 路径有效性:
directoryPath必须是一个有效的文件夹绝对路径。如果路径指向的是一个文件(如 .zip),请改用 startGameWithPkgPath:options:didGameHandleCreated:completion。 - 资源独占性:在游戏运行期间,请勿对该目录进行重命名、删除或修改文件内容的操作,否则会导致游戏运行异常。
- Bundle 资源:如果资源放在
Main Bundle中,路径通常是只读的。SDK 完美支持只读路径加载,但需确保游戏逻辑本身不尝试向该目录写入持久化数据。