Xcode9原生工程接入unity2017导出工程

1、unity导出工程设置如下

2、把导出工程的以下几个文件拷到原生的工程下面。

 

3、原生目录如下

4、把拷贝的unity文件拉到面板,除了data文件夹reference选项,其他都是createGroup,都要选copy items if needed,拉完如下

5、库跟着xcode工程添加

6、

7、这里切记,不要添加$(inherited),而且顺序不能倒,不然编译会找不到CoreMotion文件,什么原理我也不知道,知道的可以留言告诉我谢谢。

 

 

 

 

8、这两个路径参考unity工程,前提是你拷过来的文件摆放相对路径得跟unity导出的工程一样。

 

 

 9、other C flags只添加这一行,可以到unity工程拷贝

 

 

 

10、

 

11、Classes里面的预编译文件路径添加到这来,预编译文件加上下面一句。如果源工程已经有pch文件,就拷到一起只留一个吧。

    #import <Foundation/Foundation.h>

    #import <UIKit/UIKit.h>

    #import "UnityAppController.h"

12、可从unity工程拷过来

 

13、加RunScripts

 

 14、Classes里面的main.mm内容考到原生的main.m里面,并把原生的后缀改成.mm,然后把Classes里面的main删啦。直接move to trash。

15、修改main.mm:

//const char* AppControllerClassName = "UnityAppController";

const char* AppControllerClassName = "AppDelegate"

 16、修改UnityAppController.h

//inline UnityAppController*  GetAppController()

//{

//    return (UnityAppController*)[UIApplication sharedApplication].delegate;

//}

#import "AppDelegate.h"

inline UnityAppController*  GetAppController()

{

    AppDelegate *delegate=(AppDelegate *)[UIApplication sharedApplication].delegate;

    return delegate.unityController;

}

 

17、AppDelegate.h修改:

 

#import <UIKit/UIKit.h>

@class UnityAppController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate>

 

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UIWindow *unityWindow;

 

@property (strong, nonatomic) UnityAppController *unityController;

 

- (void)showUnityWindow;

- (void)hideUnityWindow;

 18、AppDelegate.m修改:

 

#import "AppDelegate.h"

 

@interface AppDelegate ()

 

@end

 

@implementation AppDelegate

 

 

-(UIWindow *)unityWindow{

    return  UnityGetMainWindow();

}

 

-(void)showUnityWindow{

    [self.unityWindow makeKeyAndVisible];

}

 

-(void)hideUnityWindow{

    [self.window makeKeyAndVisible];

}

 

 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.

    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];

    self.window.backgroundColor = [UIColor whiteColor];

    

    self.unityController = [[UnityAppController alloc]init];

    [self.unityController application:application didFinishLaunchingWithOptions:launchOptions];

    

    [self showUnityWindow];

    return YES;

}

 

 

 

- (void)applicationWillResignActive:(UIApplication *)application {

    [self.unityController applicationWillResignActive:application];

    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

}

 

 

- (void)applicationDidEnterBackground:(UIApplication *)application {

    [self.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 {

    [self.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 {

    [self.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 {

    [self.unityController applicationWillTerminate:application];

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end

 

 

 

posted @ 2018-04-20 13:58  joson_zeng  阅读(1649)  评论(0编辑  收藏  举报