美文网首页
Xcode 10.3 ,10.1集成 Unity 2018.2.

Xcode 10.3 ,10.1集成 Unity 2018.2.

作者: dunlunjinfen | 来源:发表于2019-08-27 18:15 被阅读0次

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

    player setting关键设置

    工程集成和设置

    步骤一

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

    步骤二

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

    步骤三

    将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如下修改

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

    {

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

        returndelegate.unityVC;

        //return _UnityAppController;

    }

    步骤八

    对AppDelegate进行如下修改:

    #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中同样步骤集成进来,连接真机报错如下:

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

    相关文章

      网友评论

          本文标题:Xcode 10.3 ,10.1集成 Unity 2018.2.

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