iOS集成UnityFramework插件

590 阅读1分钟

1.先准备好原生工程【KW_OC】和导出的unity工程【UnityProject】

WeChataf65bafcf900557204c558f501fa5fe7.png

2.打开UnityProject,检查Data文件是否勾选UnityFramework

WeChat8d2ed13ee1322d4c59efc96c6864829b.png

3.选择真机设备,Command+B 进行编译,编译成功后会看到 UnityFramework文件变色了

截屏2023-04-11 13.49.25.png

4.点击Show in Finder ,打开文件目录

截屏2023-04-11 13.52.32.png

5.将Unity项目嵌入原生项目

  • 打开原生工程,左下角 + 按钮 截屏2023-04-11 13.55.42.png

  • 选中xcodeproj文件,右下角add 截屏2023-04-11 13.57.46.png

  • 现在原生工程文件目录就变成这样了 截屏2023-04-11 13.58.02.png

  • 编译UnityFramework 【Command + B】

WeChatcda74c547711f235341f142253aeba9d.png

  • 配置Framework

WeChat9142880dd44d2c94167ea42c051a231f.png

    • Frameworks,Librarys,and... 展开后点击+ , AddFile 截屏2023-04-11 14.12.13.png
    • 把刚才的framework文件拖进来,点击open WeChat128ed1c6c63feeba59a0bfee0f32a6ef.png

6.嵌入完成 进入代码部分

  • appdelegate.h 文件
#include <UnityFramework/UnityFramework.h>
#include <UnityFramework/NativeCallProxy.h>

@property UnityFramework* ufw;
  • appdelegate.m 文件
#import "AppDelegate.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]) {
        [ufw setExecuteHeader: &_mh_execute_header];
    }
    return ufw;
}

  


@interface AppDelegate ()
@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [self initUnityWithLaunchOptions:launchOptions]
}

//初始化unity
- (void)initUnityWithLaunchOptions:(NSDictionary *)options {
/*
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString *receiveUnityMsg = [user objectForKey:@"receiveUnityMsg"];
    NSString *unityType = [user objectForKey:@"Unity_Type"];
    if (![receiveUnityMsg isEqualToString:unityType]) {
        [[self ufw] unregisterFrameworkListener: self];
        [self setUfw: nil];
    }
    _shouldChangeOrientation = YES;

    if([self unityIsInitialized]) {
        [[self ufw] showUnityWindow];
        [[self ufw]pause:false];
    }
*/
  

    [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.unity3d.framework"];
    [[self ufw] registerFrameworkListener: self];
    [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
    
    [[self ufw] runEmbeddedWithArgc: gArgc argv: gArgv appLaunchOpts: appLaunchOpts];


}

//其他设置

- (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