环境:egretWing(4.1.4),xCode(9.4.1),Egret iOS Support(5.0.12)
1. egret项目打包
点击发布按钮
version填成你喜欢的,选择Runtime,私钥填成空,不填成空的话我这边启动不了,不知道具体的原因。
稍等片刻后会在项目的bin-release/native目录下生成文件。
将native/test目录下的两个文件进行压缩打包成zip包,切记是对game_code_test.zip+resource进行打包,不是对上一级的test目录打包。
这个步骤不是必须的,你也可以把game_code_test.zip和resource文件拷贝到iOS项目中,但是为了方便管理,我选择把文件夹压缩,只管理一个文件,然后运行时unzip。
2. iOS项目工程构建
新建iOS空工程,并关闭bitcode(egret库不支持)。
将下载的egret-ios-support-5.0.12/egret_ios_template/proj.ios/egret_libs文件夹与egret-ios-support-5.0.12/egret_ios_template/proj.ios/HelloEgret/SSZipArchive文件夹拷贝至项目中。
将刚才打包的test.zip也一并拷入。
导入依赖的库。
3. 代码编写
//EgretViewController.h
#import <UIKit/UIKit.h>
@interface EgretViewController : UIViewController
/**
打开egret页面
@param gameVersion 游戏版本号
@param sourceController 来源视图
*/
+ (void)presentEgretVCWithVersion:(NSString *)gameVersion
sourceController:(UIViewController *)sourceController;
@end
//EgretViewController.m
#import "EgretViewController.h"
#import "SSZipArchive.h"
#import "EgretRuntime.h"
@interface EgretViewController ()
//游戏版本号
@property (nonatomic,strong) NSString *gameVersion;
//egret引擎所在的子视图
@property (nonatomic,strong) UIViewController *childViewController;
//egret引擎
@property (nonatomic,strong) EgretRuntime *egret;
@end
@implementation EgretViewController
#pragma mark - API
+ (void)presentEgretVCWithVersion:(NSString *)gameVersion
sourceController:(UIViewController *)sourceController;
{
EgretViewController *vc = [[EgretViewController alloc] init];
vc.gameVersion = gameVersion;
[sourceController presentViewController:vc animated:YES completion:nil];
}
#pragma mark - Cycle
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
//绑定事件
[self bindEvent];
//运行游戏
[self runGame];
}
#pragma mark - Method
//运行游戏
- (void)runGame
{
//存储路径更目录路径
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//游戏解压后的核心zip文件路径
NSString *gamePath= [NSString stringWithFormat:@"%@/%@/game/game_code_%@.zip",
rootPath,
self.gameVersion,
self.gameVersion];
//判断游戏是否已经解压缩
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:gamePath])
{//未解压缩,进行解压缩操作
//游戏压缩包文件路径
NSString *gameZipPath = [[NSBundle mainBundle] pathForResource:self.gameVersion ofType:@"zip"];
//游戏目录结构一定要为{$root}/{$version}/game/xxx.zip
//{$root}/{$version}目录
NSString *firstDir = [NSString stringWithFormat:@"%@/%@",rootPath,self.gameVersion];
BOOL isDir = NO;
BOOL existed = [fileManager fileExistsAtPath:firstDir
isDirectory:&isDir];
if (!(isDir == YES && existed == YES))
{
[fileManager createDirectoryAtPath:firstDir withIntermediateDirectories:YES attributes:nil error:nil];
}
//{$root}/{$version}/game目录
NSString *secondDir = [NSString stringWithFormat:@"%@/%@/game",rootPath,self.gameVersion];
isDir = NO;
existed = [fileManager fileExistsAtPath:secondDir
isDirectory:&isDir];
if (!(isDir == YES && existed == YES))
{
[fileManager createDirectoryAtPath:secondDir withIntermediateDirectories:YES attributes:nil error:nil];
}
//解压文件
dispatch_async(dispatch_get_main_queue(), ^{
[SSZipArchive unzipFileAtPath:gameZipPath
toDestination:secondDir
progressHandler:^(NSString * _Nonnull entry, unz_file_info zipInfo, long entryNumber, long total) {
NSLog(@"解压进度:%.2f",(entryNumber*1.0/total));
}
completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nonnull error) {
if (succeeded)
{
[self runGame];
}
else
{
NSLog(@"游戏资源解压出错");
}
}];
});
}
else
{//已解压缩,调用egret库运行游戏
//运行参数
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
options[@OPTION_EGRET_ROOT] = rootPath;
options[@OPTION_LOADER_URL] = [NSString stringWithFormat:@"game_code_%@.zip",self.gameVersion];
options[@OPTION_GAME_ID] = self.gameVersion;
[self.egret setOptions:options];
[self.egret run];
self.childViewController.view.hidden = NO;
}
}
//绑定事件
- (void)bindEvent
{
//__weak EgretViewController *weakSelf = self;
[self.egret setRuntimeInterface:@"RuntimeInterface" block:^(NSString *value) {
//to do sth.
}];
}
#pragma mark - Getter
- (EgretRuntime *)egret
{
if (_egret == nil)
{
_egret = [EgretRuntime createEgretRuntime];
[_egret initWithViewController:self.childViewController];
}
return _egret;
}
- (UIViewController *)childViewController
{
if (_childViewController == nil)
{
_childViewController = [[UIViewController alloc] init];
[self addChildViewController:_childViewController];
[self.view addSubview:_childViewController.view];
_childViewController.view.hidden = true;
}
return _childViewController;
}
@end
然后在需要调用的地方写下以下代码即可
[EgretViewController presentEgretVCWithVersion:@"test"
sourceController:self];