美文网首页
Unity集成到iOS

Unity集成到iOS

作者: 米开朗骑騾 | 来源:发表于2019-06-10 21:29 被阅读0次

    网上试了很多 综合两个之后完成了
    参考1参考2

    步骤大致两部分

    一.工程配置

    1.添加framework:
    注意:libiconv.2.dylib 这个的添加方法
    Add other 然后全局搜索 command+shift+G 然后输入/usr/lib 查找就可以了
    导出的工程有的都要添加,个别的需选择Optional


    image.png

    2.添加Header Search Paths
    和导出的工程一致即可

    • $(inherited)
    • "$(SRCROOT)"
    • "$(SRCROOT)/Classes"
    • "$(SRCROOT)/Classes/Native"
    • "$(SRCROOT)/Libraries"
    • "$(SRCROOT)/Libraries/libil2cpp/include"
    • "$(SRCROOT)/Libraries/bdwgc/include"
    image.png

    3.Library Search Paths
    这个本身不用修改 拖入文件后会自动添加$(PROJECT_DIR)/Libraries

    4.Other C Fiags,Other C++ Flags设置成-DINIT_SCRIPTING_BACKEND=1


    image.png

    $(inherited) 、-DINIT_SCRIPTING_BACKEND=1 、-fno-strict-overflow 、-DNET_4_0 -DRUNTIME_IL2CPP=1

    image.png

    $(inherited)

    $(OTHER_CFLAGS)
    -DINIT_SCRIPTING_BACKEND=1

    5.设置User-Defined
    设置key值:GCC_THUMB_SUPPORT,设置Value值:NO
    设置key值:GCC_USE_INDIRECT_FUNCTION_CALLS,设置Value值:NO
    设置key值:UNITY_RUNTIME_VERSION,设置Value值:2018.4.0f1 (这里与你的unity项目的版本一致)
    设置key值:UNITY_SCRIPTING_BACKEND,设置Value值: il2cpp

    image.png

    6.将Unity/Classes/Prefix.pch 的代码复制到项目里的pch文件
    Build Setting 设置Precompile Prefix Header: YES

    image.png

    7.在Build Phases,点+号,添加Run Script
    "$PROJECT_DIR/MapFileParser.sh"

    image.png

    8.设置 C Language Dialekt:C99[-std=c99]
    设置 C++ Language Dialekt : C++ 11[-std=C++11]
    设置 C++ Standard Library : libc++ (LLVM C++ standard Library with C++11 support)

    9.在Other Linker Flags添加
    $(inherited) 、 -weak_framework 、CoreMotion 、-weak-lSystem (顺序要对,这里吃过亏!)

    image.png

    10.Enable Bitcode 设置成NO

    二.工程文件代码相关

    1.拖入Unity导出的Xcode工程里的四个文件
    Data文件拖入时需选择 Create folder references

    image.png
    image.png

    2.将Classes里的Prefix.pch的代码拷贝到自己生成的pch文件里并彻底删除
    拷贝后添加#import "UnityAppController.h"

    image.png

    3.将Classes里的main.mm代码拷贝到自己的main.m里并彻底删除
    引入头文件#import "AppDelegate.h"并修改main.m代码
    将main.m 改成main.mm

    return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
    
    image.png

    4.删除Classes里DynamicLibEngineAPI-functions.h、DynamicLibEngineAPI.mm的引用(选择Remove References)

    在Classes里的Unity里的DeviceSettings.mm文件里添加
    (#endif下没东西会报错 error: control may reach end of non-void function)

    #elif PLATFORM_TVOS
        if (!strncmp(model, "AppleTV5,", 9))
            return deviceAppleTV1Gen;
        else if (!strncmp(model, "AppleTV6,", 9))
            return deviceAppleTV2Gen;
        else
            return deviceUnknown;
    #endif
        return deviceUnknown;
    }
    

    5.修改UnityAppController.h文件

    #import "AppDelegate.h"
    
    extern UnityAppController* _UnityAppController;
    inline UnityAppController* GetAppController()
    {
        //    return _UnityAppController;
        return (UnityAppController*)[[UIApplication sharedApplication] valueForKeyPath:@"delegate.unityController"];
    }
    或者写成
    inline UnityAppController* GetAppController()
    {
        //    return _UnityAppController;
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        return delegate.unityController;
    }
    

    6.加载Unity的Window
    AppDelegate.h

    #import <UIKit/UIKit.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
    

    AppDelegate.m

    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    @implementation AppDelegate{
        UIApplication *_application;
        NSDictionary *_launchOptions;
    }
    
    -(UIWindow *)unityWindow{
        return  UnityGetMainWindow();
    }
    
    -(void)showUnityWindow{
        
        if (!self.unityController) {
            self.unityController = [[UnityAppController alloc]init];
            [self.unityController application:_application didFinishLaunchingWithOptions:_launchOptions];
            
            [self applicationDidBecomeActive:_application];
        }
        
        
        [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.rootViewController = [[ViewController alloc] init];
        
        _application = application;
        _launchOptions = launchOptions;
        
    //这里放开的话 有些游戏会立刻被加载 因此做了改善 在上面显示加载
    //    self.unityController = [[UnityAppController alloc]init];
    //    [self.unityController application:application didFinishLaunchingWithOptions:launchOptions];
        
        [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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
        
        [_unityController 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.
        
        [_unityController applicationDidEnterBackground:application];
    }
    
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
        
        [_unityController 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.
        
        [_unityController applicationDidBecomeActive:application];
    }
    
    - (void)applicationWillTerminate:(UIApplication *)application {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        
        [_unityController applicationWillTerminate:application];
    }
    @end
    

    7.调用显示

       [btn addTarget:self action:@selector(aaa) forControlEvents:UIControlEventTouchUpInside];
    
    - (void)aaa{
       
       [(AppDelegate*)[UIApplication sharedApplication].delegate showUnityWindow];
       
       NSLog(@"aaaa");
    }
    

    相关文章

      网友评论

          本文标题:Unity集成到iOS

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