美文网首页
iOS 原生项目通过 workspace 来集成 unity 导

iOS 原生项目通过 workspace 来集成 unity 导

作者: predecessor | 来源:发表于2020-08-20 15:44 被阅读0次

    最近公司打算将 unity 项目集成的 iOS 原生项目中,网上找了很多文章,大都集成复杂,关键是还不能成功集成。
    许久,找到了一篇靠谱的文章。和小伙伴摸索了好久终于集成成功。遂写此文以供后人日后之用。

    1.unity 项目导出iOS 项目

    将你自己的 unity 项目或者参考文章中的unity 项目UnityProject按照下面的步骤来导出 iOS 项目。

    注意:unity 项目导出 iOS 项目可以有多种方式,这里我们使用的是以 framework 库的方式导出的,不清楚的可以询问 unity 开发的小伙伴。切记!我们摸索的时候就出现过类似坑点!

    1.1

    首先在Unity编辑器打开UnityProject项目,选择Menu -> Window -> Package Manager,因为2.0.8版本不兼容使用Unity作为库,所以要移除Ads资源包,或更新Ads资源包到v 3.*版本。

    1.2

    选择Menu -> Edit -> Player Settings -> Player -> iOS设置标签页 -> Identification Section,设置有效的Bundle Identification和Signing Team ID,以避免后续步骤出现Xcode签名问题。

    1.3

    打开Menu -> File -> Builds Settings,在此选择并切换平台为iOS。将Unity项目导出 iOS 项目到一个文件夹。导出成功后该文件目录如下:


    unity 导出的 iOS 项目文件目录

    2.创建 workspace并添加xcodeproj

    workspace允许同时处理多个项目,并结合它们的结果。

    2.1

    我们打开 Xcode,选择File -> New -> Workspace,创建工作空间integration.xcworkspace,把它保存在integrationProject目录下。并按照下图设置integrationProject目录下的内容,其中NativeiOSAPP为新建的空白的iOS 原生项目,当然你也可以用已有的项目。UnityProject为 unity 导出的 iOS 项目,即 1.3 中导出的那个文件夹。


    1.png
    2.2

    打开integration.xcworkspace,选择File -> Add Files to “integration”,把NativeiOSAPP.xcodeproj和Unity-iPhone.xcodeproj添加到workspace 中。


    2.png

    3.添加UnityFramework.framework

    这一步看似简单,实则是一个坑点。通过图 3 图 4直接添加UnityFramework.framework。如果添加后如图 5一样,UnityFramework.framework不是浅白色的就说明添加成功了。


    3.png
    4.png
    5.png

    如果是浅白色的话可以尝试接下来的添加方法(我就是通过这种方法添加的)
    先如下图找到UnityFramework.framework的位置


    6.png
    7.png

    然后将UnityFramework.framework文件夹拖到NativeiOSAPP的目录下

    8.png

    成功后如下图


    9.png

    4.设置NativeCallProxy.h

    首先找到并选择Unity-iPhone / Libraries / Plugins / iOS / NativeCallProxy.h。

    然后在Target Membership中勾选UnityFramework,通过UnityFramework右侧的下拉图标,把标头可见性设为Public。


    10.png

    5.设置Data文件夹

    默认情况下,Data文件夹是Unity-iPhone目标的一部分,我们会通过修改它,使所有内容封装到单个框架文件中。

    我们把Data文件夹的Target Membership改为UnityFramework。


    11.png

    改动之后Unity-iPhone项目不能正常运行,为了使Unity-iPhone项目能够正常运行,我们需要在Unity-iPhone/MainApp/main.mm中通过下面代码指定Data文件夹所处的新位置。

    [ufw setDataBundleId: "com.unity3d.framework"];//com.unity3d.framework是UnityFramework的 bundleID。
    
    12.png

    至此,配置完毕!UnityFramework 也已经集成到 iOS 原生项目中了。此时iOS 原生项目和 unity 导出的 iOS 项目应该都能正常运行了。

    6.原生 iOS 项目中调用 unity

    这里我们把 main.m改为 main.mm,然后在Main.storyboard中 ViewController 上添加一个按钮,点击跳转到 unity 工程。话不多说,直接上代码。

    main.mm

    #import <UIKit/UIKit.h>
    #import "AppDelegate.h"
    
    int main(int argc, char * argv[]) {
        char ** aa = argv;
        [[NSUserDefaults standardUserDefaults] setValue:@(argc) forKey:@"argc"];
        [[NSUserDefaults standardUserDefaults] setValue:[NSString stringWithFormat:@"%p",aa] forKey:@"argv"];
        NSString * appDelegateClassName;
        @autoreleasepool {
            // Setup code that might create autoreleased objects goes here.
            appDelegateClassName = NSStringFromClass([AppDelegate class]);
        }
        return UIApplicationMain(argc, argv, nil, appDelegateClassName);
    }
    

    AppDelegate.h

    #import <UIKit/UIKit.h>
    #include <UnityFramework/UnityFramework.h>
    #include <UnityFramework/NativeCallProxy.h>
    
    @interface AppDelegate : UIResponder <UIApplicationDelegate,UnityFrameworkListener, NativeCallsProtocol>
    
    @property (nonatomic, strong) UIWindow * window;
    @property (nonatomic, strong) UnityFramework *ufw;
    @end
    

    AppDelegate.h

    #import "AppDelegate.h"
    
    @interface AppDelegate ()
    
    @end
    
    UnityFramework* UnityFrameworkLoad()
    {
        NSString* bundlePath = nil;
        bundlePath = [[NSBundle mainBundle] bundlePath];
        bundlePath = [bundlePath stringByAppendingString: @"/Frameworks/UnityFramework.framework"];
        
        NSBundle* bundle = [NSBundle bundleWithPath: bundlePath];
        if ([bundle isLoaded] == false) [bundle load];
        
        UnityFramework* ufw = [bundle.principalClass getInstance];
        if (![ufw appController])
        {
            // unity is not initialized
            [ufw setExecuteHeader: &_mh_execute_header];
        }
        return ufw;
    }
    
    @implementation AppDelegate
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        // Override point for customization after application launch.
        [[NSUserDefaults standardUserDefaults] setValue:launchOptions forKey:@"launchOptions"];
        self.ufw = UnityFrameworkLoad();
        [self.ufw setDataBundleId:"com.unity3d.framework"];
        [self.ufw registerFrameworkListener:self];
        [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
    
        return YES;
    }
    
    - (void)applicationWillResignActive:(UIApplication *)application {
        [[[self ufw] appController] applicationWillResignActive: application];
    }
    - (void)applicationDidEnterBackground:(UIApplication *)application {
        [[[self ufw] appController] applicationDidEnterBackground: application];
    }
    - (void)applicationWillEnterForeground:(UIApplication *)application {
        [[[self ufw] appController] applicationWillEnterForeground: application];
    }
    - (void)applicationDidBecomeActive:(UIApplication *)application {
        [[[self ufw] appController] applicationDidBecomeActive: application];
    }
    - (void)applicationWillTerminate:(UIApplication *)application {
        [[[self ufw] appController] applicationWillTerminate: application];
    }
    - (void)showHostMainWindow:(NSString *)color
    {}
    @end
    

    ViewController.m

    #import "ViewController.h"
    #include <UnityFramework/UnityFramework.h>
    #import "AppDelegate.h"
    
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view.
    }
    - (IBAction)pushToUnity:(id)sender {
        NSString *argv = [[NSUserDefaults standardUserDefaults] valueForKey:@"argv"];
        char ** aa;
        sscanf([argv cStringUsingEncoding:NSUTF8StringEncoding], "%p",&aa);
        int bb = [[[NSUserDefaults standardUserDefaults] valueForKey:@"argc"] intValue];
        AppDelegate *dd = (AppDelegate *)([UIApplication sharedApplication].delegate);
        [dd.ufw runEmbeddedWithArgc:bb argv:aa appLaunchOpts:[[NSUserDefaults standardUserDefaults] valueForKey:@"launchOptions"]];
    }
    @end
    

    这样集成就完成了!运行iOS 原生项目就可以跳转至 unity 了。至于iOS 和 unity 之间的通信还在摸索之中。期待我神功大成吧。

    相关文章

      网友评论

          本文标题:iOS 原生项目通过 workspace 来集成 unity 导

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