環(huán)境:egretWing(4.1.4),xCode(9.4.1)拯啦,Egret iOS Support(5.0.12)
1. egret項(xiàng)目打包
點(diǎn)擊發(fā)布按鈕
version填成你喜歡的,選擇Runtime,私鑰填成空劲装,不填成空的話我這邊啟動(dòng)不了,不知道具體的原因荐健。
稍等片刻后會(huì)在項(xiàng)目的bin-release/native目錄下生成文件酱畅。
將native/test目錄下的兩個(gè)文件進(jìn)行壓縮打包成zip包,切記是對(duì)game_code_test.zip+resource進(jìn)行打包江场,不是對(duì)上一級(jí)的test目錄打包纺酸。
這個(gè)步驟不是必須的,你也可以把game_code_test.zip和resource文件拷貝到iOS項(xiàng)目中址否,但是為了方便管理餐蔬,我選擇把文件夾壓縮,只管理一個(gè)文件佑附,然后運(yùn)行時(shí)unzip樊诺。
2. iOS項(xiàng)目工程構(gòu)建
新建iOS空工程,并關(guān)閉bitcode(egret庫(kù)不支持)音同。
將下載的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文件夾拷貝至項(xiàng)目中词爬。
將剛才打包的test.zip也一并拷入。
導(dǎo)入依賴的庫(kù)权均。
3. 代碼編寫
//EgretViewController.h
#import <UIKit/UIKit.h>
@interface EgretViewController : UIViewController
/**
打開egret頁(yè)面
@param gameVersion 游戲版本號(hào)
@param sourceController 來(lái)源視圖
*/
+ (void)presentEgretVCWithVersion:(NSString *)gameVersion
sourceController:(UIViewController *)sourceController;
@end
//EgretViewController.m
#import "EgretViewController.h"
#import "SSZipArchive.h"
#import "EgretRuntime.h"
@interface EgretViewController ()
//游戲版本號(hào)
@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];
//運(yùn)行游戲
[self runGame];
}
#pragma mark - Method
//運(yùn)行游戲
- (void)runGame
{
//存儲(chǔ)路徑更目錄路徑
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//游戲解壓后的核心zip文件路徑
NSString *gamePath= [NSString stringWithFormat:@"%@/%@/game/game_code_%@.zip",
rootPath,
self.gameVersion,
self.gameVersion];
//判斷游戲是否已經(jīng)解壓縮
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:gamePath])
{//未解壓縮顿膨,進(jìn)行解壓縮操作
//游戲壓縮包文件路徑
NSString *gameZipPath = [[NSBundle mainBundle] pathForResource:self.gameVersion ofType:@"zip"];
//游戲目錄結(jié)構(gòu)一定要為{$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(@"解壓進(jìn)度:%.2f",(entryNumber*1.0/total));
}
completionHandler:^(NSString * _Nonnull path, BOOL succeeded, NSError * _Nonnull error) {
if (succeeded)
{
[self runGame];
}
else
{
NSLog(@"游戲資源解壓出錯(cuò)");
}
}];
});
}
else
{//已解壓縮,調(diào)用egret庫(kù)運(yùn)行游戲
//運(yùn)行參數(shù)
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
然后在需要調(diào)用的地方寫下以下代碼即可
[EgretViewController presentEgretVCWithVersion:@"test"
sourceController:self];