startGameWithURL:options:didGameHandleCreated:completion:
启动游戏(通过远端 URL 配置)
该方法允许开发者通过直接指定游戏包的下载地址(URL)来启动游戏。它是一个全流程集成接口,内部逻辑涵盖了:检查本地缓存 -> 异步下载(若需) -> 文件校验 -> 解压 -> 环境初始化 -> 视图创建 -> 正式启动。
方法声明
objectivec
/**
Start the game using remote URL options.
Automatically handles the download-to-play pipeline.
@param url the URL.
@param options Configuration object containing version, and integrity 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 has finished loading and is ready for interaction.
@return An instance of id<SUDOPGameTask> to track the startup process.
*/
+ (id<SUDOPGameTask>)startGameWithURL:(NSString *)url
options:(SUDOPGamePackageOptions *)options
didGameHandleCreated:(nullable SUDOPDidGameHandleCreated)didGameHandleCreated
completion:(nullable SUDOPGameOperationCompletion)completion;参数说明
| 参数名 | 类型 | 必传 | 描述 |
|---|---|---|---|
| url | NSString * | 是 | 游戏包url |
| options | SUDOPGamePackageOptions * | 是 | 配置对象,需包含 appGameID、version 及 fid(校验值)。 |
| didGameHandleCreated | Block | 否 | 关键回调。当资源就绪且句柄创建后触发。此时应将游戏 View 添加到 UI 层级。 |
| completion | Block | 否 | 最终回调。表示整个下载、解压、加载流程结束,游戏首屏已呈现。 |
运行行为
- 静默流水线:SDK 首先查找本地是否存在匹配
version和fid的缓存。
- 有缓存:直接进入启动逻辑。
- 无缓存:启动网络下载,校验通过后自动解压。
- 句柄交付:在
didGameHandleCreated中交付gameHandle。即便下载耗时较长,该回调也只会在资源完全准备好后触发。 - 渲染激活:与
preStart不同,此方法会直接将游戏引擎切换至活跃渲染状态。
代码示例
objectivec
SUDOPGamePackageOptions *options = [[SUDOPGamePackageOptions alloc] init];
NSString *url = @"https://cdn.example.com/games/pool_master.zip";
options.appGameID = @"pool_001";
options.version = @"1.0.5";
options.fid = @"e99a18c428cb38d5f260853678922e03"; // 校验 MD5
options.fidType = SUDOPFIDTypeMD5;
[SUDOP startGameWithURL:url,
options:options
didGameHandleCreated:^(id<SUDRTGameHandle> gameHandle) {
// 注册业务代理
[SUDOP registerWrappedClientWithGameHandle:gameHandle clientDelegate:self];
// 挂载视图
UIView *gameView = [gameHandle getGameView];
gameView.frame = self.view.bounds;
[self.view addSubview:gameView];
}
completion:^(id<SUDOPGameHandleProvider> _Nullable provider, NSError * _Nullable error) {
if (!error) {
NSLog(@"URL 资源游戏启动成功");
} else {
// 可能的错误:网络连接失败、校验 MD5 不匹配、磁盘空间不足
NSLog(@"启动失败: %@", error.localizedDescription);
}
}];注意事项
- 等待时长:由于该接口可能包含下载过程,
didGameHandleCreated的触发时机取决于文件大小和网络速度。建议在调用此接口前,在 UI 上展示一个加载指示器(Loading)。 - 任务追踪:返回的
id<SUDOPGameTask>可以在用户在下载过程中调用[task destroy]以停止下载并释放未完成的资源。 - ATS 配置:如果
url使用 HTTP 而非 HTTPS,请确保在Info.plist中配置了App Transport Security Settings。