合并unity和iOS(将unity嵌入到iOS中,进行二次开发)

1 :在U3D中生成iOS文件,注意:


bundle identifier :用户名,要与后面的对接上
Version:版本号,后面要用到
TargetSDK:设备或模拟器

2 :生成一个X-code程序,然后新建一个oc项目
将U3D生成的项目内的文件:
将Classes,Libraries,MapFileParser.sh拖入到项目(选中Copy items if needed, 选中Create groups)
将Data拖入到项目(选中Copy items if needed, 选中Create folder references)

3 :修改bit code为no
Build Settings - > Enable Bitcode

4 : 添加frameworks



5 :添加Header Search Paths 和 Library Search Paths

6 :other C Flags - >  -DINIT_SCRIPTING_BACKEND=1


7 : 添加User-Defind(UNITY_RUNTIME_VERSION)版本号与工程版本一致


Add User-Defined Setting添加以上信息

8 :删除main.Storyboard,同时改变plist文件中的Main storyboard file base name,将Main删除,在AppDelegate.m初始化window进行使用
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

9 :将main.mm文件内容复制到main.m中,同时删除.mm文件,将.m文件修改为.mm
#import <UIKit/UIKit.h>
#import "AppDelegate.h"

#include "RegisterMonoModules.h"
#include "RegisterFeatures.h"
#include <csignal>

// Hack to work around iOS SDK 4.3 linker problem
// we need at least one __TEXT, __const section entry in main application .o files
// to get this section emitted at right time and so avoid LC_ENCRYPTION_INFO size miscalculation
static const int constsection = 0;

void UnityInitTrampoline();

// WARNING: this MUST be c decl (NSString ctor will be called after +load, so we cant really change its value)
//const char* AppControllerClassName = "UnityAppController";
const char* AppControllerClassName = "AppDelegate";

int main(int argc, char* argv[])
{
    @autoreleasepool
    {
        UnityInitTrampoline();
        UnityParseCommandLine(argc, argv);
        
        RegisterMonoModules();
        NSLog(@"-> registered mono modules %p\n", &constsection);
        RegisterFeatures();
        
        // iOS terminates open sockets when an application enters background mode.
        // The next write to any of such socket causes SIGPIPE signal being raised,
        // even if the request has been done from scripting side. This disables the
        // signal and allows Mono to throw a proper C# exception.
        std::signal(SIGPIPE, SIG_IGN);
        
//        UIApplicationMain(argc, argv, nil, [NSString stringWithUTF8String:AppControllerClassName]);
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));

    }
    
//    return 0;
}

#if TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

#include <pthread.h>

extern "C" int pthread_cond_init$UNIX2003(pthread_cond_t *cond, const pthread_condattr_t *attr)
{ return pthread_cond_init(cond, attr); }
extern "C" int pthread_cond_destroy$UNIX2003(pthread_cond_t *cond)
{ return pthread_cond_destroy(cond); }
extern "C" int pthread_cond_wait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex)
{ return pthread_cond_wait(cond, mutex); }
extern "C" int pthread_cond_timedwait$UNIX2003(pthread_cond_t *cond, pthread_mutex_t *mutex,
                                               const struct timespec *abstime)
{ return pthread_cond_timedwait(cond, mutex, abstime); }

#endif // TARGET_IPHONE_SIMULATOR && TARGET_TVOS_SIMULATOR

10 : 添加Run Script,点➕添加(注意文件路径)


11 :修改UnityAppController.h文件
#import "AppDelegate.h"
inline UnityAppController*    GetAppController()
{
//    return (UnityAppController*)[UIApplication sharedApplication].delegate;
    return (UnityAppController*)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];
//    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    return delegate.unityController;
}
12 :修改AppDelegate.h文件中
#import <UIKit/UIKit.h>
#import "UnityAppController.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) UIWindow *unityWindow;

@property (strong, nonatomic) UnityAppController *unityController;

- (void)showUnityWindow;
- (void)hideUnityWindow;

@end

13 : 修改AppDelegate.m文件

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

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

- (void)showUnityWindow {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(0, 0, 155, 85);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"back" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];
    [self.unityWindow addSubview:button];
    
    [[UIApplication sharedApplication] setStatusBarHidden:YES];
    
    [self.unityWindow makeKeyAndVisible];
}

- (void)hideUnityWindow {
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
    [self.window makeKeyAndVisible];
}

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

- (void)applicationWillResignActive:(UIApplication *)application {
    [self.unityController applicationWillResignActive:application];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    [self.unityController applicationDidEnterBackground:application];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [self.unityController applicationWillEnterForeground:application];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    [self.unityController applicationDidBecomeActive:application];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    [self.unityController applicationWillTerminate:application];
}
14 :修改pch文件,将原pch文件复制到新文件中,同时添加路径


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章