記錄一下ios接入unity工程的坑

經(jīng)過(guò)各種google總共發(fā)現(xiàn)了兩個(gè)方案:
1,直接嵌入工程:嘗試各種報(bào)文件找不到。。。放棄
2,編譯產(chǎn)物接入:成功
產(chǎn)物有兩個(gè):1UnityFramework.framework。2,Data資源。文件拖入即可
代碼步驟:
1,使用單例保存main函數(shù)參數(shù)

[ConfigObj shareInstance].gArgc = argc;
[ConfigObj shareInstance].gArgv = argv;

2,AppDelegate 添加協(xié)議UnityFrameworkListener,和相應(yīng)的property

#import <UIKit/UIKit.h>
#include <UnityFramework/UnityFramework.h>
#import "ConfigObj.h"

@interface AppDelegate : UIResponder <UIApplicationDelegate,UnityFrameworkListener>

@property (strong, nonatomic) UIWindow *window;

@property (nonatomic, strong) UnityFramework *ufw;

@end

3,加載framework,添加監(jiān)聽(tīng)等操作

#import "AppDelegate.h"
#import "UnityFramework.framework/Headers/UnityAppController.h"


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;
}

@interface AppDelegate ()


@end

@implementation AppDelegate

- (bool)unityIsInitialized {
    return [self ufw] && [[self ufw] appController];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    //
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self initUnityWithOptions:launchOptions];
        });

    return YES;
}

- (void)initUnityWithOptions:(NSDictionary *)launchOptions
{

    
    [self setUfw: UnityFrameworkLoad()];
    // Set UnityFramework target for Unity-iPhone/Data folder to make Data part of a UnityFramework.framework and uncomment call to setDataBundleId
    // ODR is not supported in this case, ( if you need embedded and ODR you need to copy data )
    [[self ufw] setDataBundleId: "com.UnityIOS.I12"];
    [[self ufw] registerFrameworkListener: self];
//    [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
    
    [[self ufw] runEmbeddedWithArgc: [ConfigObj shareInstance].gArgc argv: [ConfigObj shareInstance].gArgv appLaunchOpts: launchOptions];
    
    UIView *view = [[[self ufw] appController] rootView];
    [ConfigObj shareInstance].unityView = view;
}

- (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]; }

@end

問(wèn)題記錄:
bundleId必須和framework的bundle名字一樣否則報(bào)錯(cuò)
[[self ufw] setDataBundleId: "com.unity3d.framework"];
工程的inplist文件配置copy一下,可能有重要信息

參考
https://blog.csdn.net/baidu_25743639/article/details/73250667

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容