IT圈老男孩1 发表于 2022-11-6 15:52

Unity3D内嵌iOS

前言


前段时间需要用Unity3D做AR开发展示嵌入的iOS和android中,在过程中碰到了一些问题,现在分享给大家。
准备工作


Unity版本用的是

:Users:sunyujian:Library:Application Support:typora-user-images:image-20221101163609242.png


版本升高之后,在app调试时会crash,无法进行调试,不知道具体的原因是什么,但app release之后是可以使用的。
导入依赖文件


在Unity文件中导入NativeCallProxy的依赖文件。
// NativeCallProxy.h#import <Foundation/Foundation.h>@protocol NativeCallsProtocol@required- (void)iOSLog:(NSString *)value;- (void)backToIOS;@end__attribute__ ((visibility("default")))@interface FrameworkLibAPI: NSObject+(void)registerAPIforNativeCalls:(id<NativeCallsProtocol>) aApi;@end
NativeCallProxy.mm文件
// NativeCallProxy.mm#import <Foundation/Foundation.h>#import "NativeCallProxy.h"@implementation FrameworkLibAPIid<NativeCallsProtocol> api = NULL;+(void)registerAPIforNativeCalls: (id<NativeCallsProtocol>) aApi{ api = aApi;}@endextern "C" { void iOSLog(const char * value);}void iOSLog(const char * value) { return ];}extern "C" { void backToIOS(const char * value);}void backToIOS(const char* value) { return ;}
这两个文件导入到Unity3D原文件中,如图所示,放入iOS文件夹下。

image-20221101165709308.png

Unity3D的脚本文件代码如下:
using System.Collections;using System.Collections.Generic;using UnityEngine;using System.Runtime.InteropServices;public class Jiaoben : MonoBehaviour { private static extern void iOSLog(string name); private static extern void backToIOS(); // Unity调用iOS的两个方法,一个是打印log另一个是返回按钮 void Start() {   Debug.Log("脚本开始运行"); } // Update is called once per frame void Update() { } // iOS调用Unity的方法。 public void sendArMessage(string msg) {   Debug.Log("从iOS接收到的消息:", msg);   }}导入iOS工程


将导出的文件拖入到iOS工程中。

image-20221101163857009.png

选中Unity-iPhone中的Data文件,并选中UnityFramework如下图

image-20221101164127269.png

然后选中UnityFramework目标,并点击build按钮进行build。更新UnityFramework文件,如下图。


image-20221101164314307.png

build完成后,选中你的主项目,找到General中的Frameworks,Libraries,and Embedded Content。

image-20221101165048556.png

点击+,在弹出的搜索框中选中UnityFramework.framework,Embed的状态无需改变

:Users:sunyujian:Library:Application Support:typora-user-images:image-20221101165127123.png

最终状态


image-20221101165149961.png

iOS端开发


接下来我们对iOS端的AppDelegate文件进行修改
// AppDelegate.h#import <UIKit/UIKit.h>#include <UnityFramework/UnityFramework.h>#import <UnityFramework/NativeCallProxy.h>@interface AppDelegate : UIResponder <UIApplicationDelegate,UnityFrameworkListener, NativeCallsProtocol>@property (strong, nonatomic) UIWindow * window;@property (strong, nonatomic) UnityFramework *ufw;- (void)showUnityView;- (void)showNativeView;- (void)sendMessageWithName:(const char*)goName functionName:(const char*) functionName message:(const char*)msg;@end
AppDelegate.m
#import "AppDelegate.h"#import "QHJSBaseWebLoader.h"/* UnityFrameworkLoad */UIKIT_STATIC_INLINE UnityFramework* UnityFrameworkLoad(){ NSString* bundlePath = nil; bundlePath = [ bundlePath]; bundlePath = ; NSBundle* bundle = ; if ( == false) ; UnityFramework* ufw = ; if (!) { // unity is not initialized ; } ;/////////add this code return ufw;}@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. ; self.window = [ initWithFrame:.bounds]; QHJSBaseWebLoader *rootVc = [init]; UINavigationController *rootNav = [initWithRootViewController:rootVc]; ; ; ; return YES;}// 打印方法-(void)iOSLog:(NSString *)value{ NSLog(@"%@", );}// 返回按钮-(void)backToIOS { ;}- (UIViewController *) rootViewController { return [ appController].rootViewController;}#pragma mark - Unity- (BOOL)unityIsInitialized{ return && [ appController];}- (void)initUnity{ /* 判断Unity 是否已经初始化 */ if () return; /* 初始化Unity */ self.ufw = UnityFrameworkLoad(); ; ; ; NSString *argvStr = [ valueForKey:@"argv"]; char **argv; sscanf(, "%p",&argv); int argc = [[ valueForKey:@"argc"] intValue]; NSDictionary *launchOptions = [ valueForKey:@"launchOptions"]; ;}- (void)showUnityView { if (!){ NSLog(@"Unity 还未初始化"); } ;}- (void)showNativeView { ;}- (void)sendMessageWithName:(const char*)goName functionName:(const char*) functionName message:(const char*)msg{ ;}#pragma mark - UnityFrameworkListener- (void)unityDidUnload:(NSNotification *)notification{ NSLog(@"========== %s ============",__func__); ; [ unregisterFrameworkListener: self]; ;}- (void)unityDidQuit:(NSNotification *)notification{ NSLog(@"========== %s ============",__func__);}- (void)applicationWillResignActive:(UIApplication *)application { [[ appController] applicationWillResignActive: application];}- (void)applicationDidEnterBackground:(UIApplication *)application { [[ appController] applicationDidEnterBackground: application];}- (void)applicationWillEnterForeground:(UIApplication *)application { [[ appController] applicationWillEnterForeground: application];}- (void)applicationDidBecomeActive:(UIApplication *)application { [[ appController] applicationDidBecomeActive: application];}- (void)applicationWillTerminate:(UIApplication *)application { [[ appController] applicationWillTerminate: application];}@end
调用方法
AppDelegate *appDelegate = (AppDelegate *)(.delegate);; functionName:[@"sendArMessage" UTF8String] message:];
通过这个调用方法就可以打开新的Unity的页面。
页: [1]
查看完整版本: Unity3D内嵌iOS