合併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文件複製到新文件中,同時添加路徑


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