美文网首页
ios原生工程集成Unity导出的Xcode工程

ios原生工程集成Unity导出的Xcode工程

作者: 测试开发雨辰 | 来源:发表于2019-04-06 12:50 被阅读0次
1、将Unity项目下面的五个文件复制到iOS项目的根目录
C86C701F-2F34-4EE2-AA55-6E9ED801F9F4.png

复制到iOS的项目后的目录如下:

F60D404C-494E-4AED-A225-21E737AC4FBE.png
2、在自己iOS项目中引用这几个文件,但是引用跟我们平时引用的不一样,右键Add File to...分别选择ClassesLibrariesMapFileParser.sh,在Options里面勾选Create groups,不要选 Copy items if needed
1ED93EDD-EABF-472A-928B-55D2C30F900F.png
2.1剩下的Data文件,右键Add Files to...,在Options里面勾选Create folder references,不要选Copy items if needed
563F0D23-9089-4F80-AA64-F6EE23223883.png

完成之后的文件夹目录如下:


523249BB-C56A-4CEA-91F6-415A8CA8027C.png
2.2,删除Library->libil2cpp这个文件的引用
C68EDE7C-191E-4D36-BD77-3D18DBFD44BC.png
3,对ios工程环境的配置,这里的配置都是以Unity的配置为参考
3.1,添加应用库
EF90FD74-7EB1-4A8C-8992-71899CE7E3D7.png

这里添加应用库文件,最好参照Unity项目工程里面的来~

3.2,添加Run Script
93C1BBA7-D0A3-4624-9111-D60BC5687FDB.png
3.2,添加头文件和库的搜索路径
59E26554-D9DD-478C-A463-78778C138710.png
3.3, 其他一些配置
D8181506-DADA-4606-84B2-3C99D01DB29D.png 99663DDE-865B-47FB-8541-70FFACC276A1.png C93CECC9-0FFC-49AD-BA89-2720B6E6DCFE.png ACFBB4BE-359E-45A9-B958-7582B9E48866.png 07882B20-D3FA-4FC8-9B1A-9029D19721A8.png

上面注意:如果自己的iOS项目有pch文件,那就把Prefix.pch里面的文件拷贝到自己的pch文件中,反之也行(注意pch路径),并添加#import"UnityAppController.h"

B2523A5E-6A78-46F3-B262-FC628B35BF1E.png
FC8772AB-DE80-478C-BAE7-D221ACD8B34B.png
4、修改main.m文件,把Classes/文件夹里面的main.mm里面的代码,拷贝到Supporting Files/下的main.m文件中,并把后缀改成.mm,修改如下图
image.png
然后删除Classes目录下单main.mm文件。注意:一样是删除引用,到了这里基本集成完毕了~~~
5、修改AppDelegate文件

AppDelegate.h文件

BF7F35875A6CD79848750E57013E776D.png
#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (UIWindow *)unityWindow {
    return UnityGetMainWindow();
}

- (void)showUnityWindow {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"返回iOS界面" forState:UIControlStateNormal];
    [button setFrame:CGRectMake(100, 100, 200, 100)];
    button.backgroundColor = [UIColor redColor];
    [button addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
    [self.unityWindow addSubview:button];
    
    [self.unityWindow makeKeyAndVisible];
    
}

- (void)hideUnityWindow {
    [self.window makeKeyAndVisible];
}


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor whiteColor];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:[[ViewController alloc] init]];
    self.window.rootViewController = nav;
    
    self.unityController = [[UnityAppController alloc] init];
    [self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
    
    [self.window makeKeyAndVisible];
    
    return YES;
}


- (void)applicationWillResignActive:(UIApplication *)application {
    [_unityController applicationWillTerminate:application];
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    [_unityController applicationDidEnterBackground:application];
    
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    [_unityController applicationWillEnterForeground:application];
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    [_unityController applicationDidBecomeActive:application];
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    [_unityController applicationWillTerminate:application];
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end

6、修改UnityAppController.h文件
#import "AppDelegate.h"

inline UnityAppController* GetAppController()
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication  sharedApplication].delegate;
    return delegate.unityController;
//    return _UnityAppController;
}

7、启动Unity界面
进入Unity界面

 [(AppDelegate *) [UIApplication sharedApplication].delegate showUnityWindow];
    UnityPause(false);

跳出Unity界面

[(AppDelegate *) [UIApplication sharedApplication].delegate hideUnityWindow];
    UnityPause(true);

相关文章

网友评论

      本文标题:ios原生工程集成Unity导出的Xcode工程

      本文链接:https://www.haomeiwen.com/subject/realiqtx.html