Xcode 10.3 ,10.1集成 Unity 2018.2.21f1

过程很简单,因为之前对player setting的不清晰理解,导致折腾了两天几近才完成编译!具体看导出ios工程一文。

player setting关键设置

5777390-dcb2f4f402680ac3.png
5777390-24ea95161f56cafc.png
5777390-1d19ee31538546bb.png

工程集成和设置

步骤一

5777390-ec6e291ff87766c2.png


下面的一定要注意添加的方式是不同的

5777390-d786ab0a47c4dff0.png
5777390-47b5da1ec6d72998.png

步骤二

在项目根目录中新建PCH文件,然后将Classes下的Prefix.pch文件中的全部代码复制到刚才创建的PCH文件中,并在代码中添加#import "UnityAppController.h"

5777390-d593be90dd505541.png

步骤三

将PCH文件的路径添加到Target->Build Setting->Prefix Header中 (直接拖拽就可以自动加载路径了)

步骤四

在Target->Build Setting中进行以下配置:

a.设置Enable Bitcode:NO;

b.Other Linker Flags添加(注意:一定要按顺序)-weak_framework,CoreMotion,-weak-lSystem,-licucore

c.对Classes和Libraries的路径进行配置;

在Header Search Paths中添加:

$(SRCROOT)/Classes

$(SRCROOT)/Classes/Native

$(SRCROOT)/Libraries/libil2cpp/include

在Library Search Paths中添加:

$(PROJECT_DIR)/Libraries(如有,则不需要重复添加)

$(PROJECT_DIR)/Libraries/Plugins/iOS (这个因为我用不到,因此看不到plugin文件夹)

d.Other C Flags添加 -DINIT_SCRIPTING_BACKEND=1 和 -DRUNTIME_IL2CPP=1

e.设置Enable C++ Runtime Types:NO

f.设置Overriding Deprecated Objective-C Methods:Yes

g.设置Unintentional Root Class:Yes

h.点击如下图的+号,添加4个Add User-Defined Setting

GCC_THUMB_SUPPORT : NO

GCC_USE_INDIRECT_FUNCTION_CALLS : NO

UNITY_RUNTIME_VERSION : 2018.2.21f1(以自己Unity实际版本号为准)

UNITY_SCRIPTING_BACKEND : il2cpp

步骤五

在Target->Build Phases中导入以下库(建议以自己Unity导出的Xcode项目对应添加): 注意如果原来unity是optional选项的库,也同样设置为optional的。而且发现没有libiconv.2.dylib,就没有管,暂时也没有问题

步骤六

Classes文件夹下的main.mm中全部代码复制到Xcode生成的main.m中;将main.mm代码中的"UnityAppController"修改成"AppDelegate";

将main.m修改为main.mm然后删除classes下的原来的main文件,不要remove to trash

步骤七

把Classes文件夹下的UnityAppController.h如下修改

5777390-af0aa0603326f7d1.png
5777390-63c5bdaf7235aeeb.png

#import "AppDelegate.h"
inlineUnityAppController* GetAppController()

{

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

    returndelegate.unityVC;

    //return _UnityAppController;

}

步骤八

对AppDelegate进行如下修改:

5777390-e1cb37274d36f9c0.png


5777390-4ee195e371393d4e.png
5777390-5d0d13f6c3bd5f11.png
5777390-228d39be1a5a7173.png

#import "AppDelegate.h"

#import "UnityAppController.h"

#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (UIWindow*)unityWindow {

    return UnityGetMainWindow();

}

- (void)showUnityWindow {

    [self.unityWindow makeKeyAndVisible];

    UIButton *back = [UIButton buttonWithType:UIButtonTypeSystem];

    back.frame=CGRectMake(80, 200, 80, 30);

    [self.unityWindow addSubview:back];

    [backsetTitle:@"goToiOS" forState:UIControlStateNormal];

    [backaddTarget:self action:@selector(hideUnityWindow) forControlEvents:UIControlEventTouchUpInside];

}

- (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.redColor;

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

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

    ViewController *vc = [[ViewController alloc] init];

    self.window.rootViewController = vc;

    [self.window makeKeyAndVisible];

    return YES;

}

- (void)applicationWillResignActive:(UIApplication*)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.

    [self.unityVC applicationWillResignActive:application];

}

- (void)applicationDidEnterBackground:(UIApplication*)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.

    [self.unityVC applicationDidEnterBackground:application];

}

- (void)applicationWillEnterForeground:(UIApplication*)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.

    [self.unityVC applicationWillEnterForeground:application];

}

- (void)applicationDidBecomeActive:(UIApplication*)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.

    [self.unityVC applicationDidBecomeActive:application];

}

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

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

    // Saves changes in the application's managed object context before the application terminates.

    [self saveContext];

    [self.unityVC applicationWillTerminate:application];

}

#pragma mark - Core Data stack

@synthesizepersistentContainer =_persistentContainer;

- (NSPersistentContainer*)persistentContainer {

    // The persistent container for the application. This implementation creates and returns a container, having loaded the store for the application to it.

    @synchronized (self) {

        if (_persistentContainer == nil) {

            _persistentContainer = [[NSPersistentContainer alloc] initWithName:@"xcode10_3orignal"];

            [_persistentContainer loadPersistentStoresWithCompletionHandler:^(NSPersistentStoreDescription *storeDescription, NSError *error) {

                if(error !=nil) {

                    // Replace this implementation with code to handle the error appropriately.

                    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.


                    /*

                     Typical reasons for an error here include:

                     * The parent directory does not exist, cannot be created, or disallows writing.

                     * The persistent store is not accessible, due to permissions or data protection when the device is locked.

                     * The device is out of space.

                     * The store could not be migrated to the current model version.

                     Check the error message to determine what the actual problem was.

                    */

                    NSLog(@"Unresolved error %@, %@", error, error.userInfo);

                    abort();

                }

            }];

        }

    }


    return _persistentContainer;

}

#pragma mark - Core Data Saving support

- (void)saveContext {

    NSManagedObjectContext *context = self.persistentContainer.viewContext;

    NSError*error =nil;

    if([contexthasChanges] && ![contextsave:&error]) {

        // Replace this implementation with code to handle the error appropriately.

        // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

        NSLog(@"Unresolved error %@, %@", error, error.userInfo);

        abort();

    }

}

@end

头文件

@class UnityAppController;

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic , strong)UIWindow *unityWindow;

@property (nonatomic , strong)UnityAppController *unityVC;

@property(readonly,strong)NSPersistentContainer*persistentContainer;

- (void)saveContext;

- (void)showUnityWindow;

- (void)hideUnityWindow;

步骤九

真机编译成功,但是crash,屏蔽InitCrashHandling(); ok了

步骤十

xcode10.1中同样步骤集成进来,连接真机报错如下:

5777390-17d40dd0d35d7188.png

修改方式为在endif下面return一个设备就可以了,比如return device.iPhonex

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值