开发环境:
1.Xcode Version 13.2.1 (13C100)
2.Unity 2019.3.13f1
3.Swift5.1
1.Unity打iOS项目导出步骤(Unity大佬可以跳过这个步骤)
1.1选择Tools 选择构建AssetBundle 构建完成
1.2选择Copy 资源到StreamingAssets
具体:如下图所示
截屏2022-04-21 11.50.03.png
1.3选择Build Settings 具体设置看个人项目需求
截屏2022-04-21 11.53.35.png
截屏2022-04-21 11.55.31.png
1.4需要设置一下Player Settings
截屏2022-04-21 11.57.40.png
[图片上传中...(截屏2022-04-21 12.00.23.png-334a78-1650513624924-0)]
1.5.设置完成后就可以点击Build了
截屏2022-04-21 12.00.35.png
1.6 打包结束后的iOS包出来
截屏2022-04-21 12.01.38.png
1.7 确保打出来的项目能正常运行: 填坑Xcode设置:否则运行不了真机具体步骤如下
[图片上传中...(截屏2022-04-21 12.04.19.png-df209c-1650513862551-0)]
截屏2022-04-21 12.04.55.png
截屏2022-04-21 12.05.51.png
[图片上传中...(截屏2022-04-21 12.06.45.png-40a959-1650514007265-0)]
截屏2022-04-21 12.07.16.png
截屏2022-04-21 12.12.36.png
成功运行了.此步骤结束
到下个环节:准备一个新创建的Swift项目包
2.创建XXXX.xcworkspace
截屏2022-04-21 12.15.50.png
[图片上传中...(截屏2022-04-21 12.19.03.png-2835b6-1650514745523-0)]
注意:Unity项目中的 Data要按照图勾对
截屏2022-04-21 12.19.15.png
截屏2022-04-21 12.31.25.png
截屏2022-04-21 12.32.56.png
截屏2022-04-21 12.32.10.png
版本:支持iOS12以上
截屏2022-04-21 12.33.38.png
最后Swift和Unity交互:
1.在Unity项目中创建两个文件
截屏2022-04-21 12.37.04.png
截屏2022-04-21 12.37.21.png
在Unity中放的位置
截屏2022-04-21 12.37.48.png
[图片上传中...(截屏2022-04-21 12.39.24.png-958341-1650515967181-0)]
[图片上传中...(截屏2022-04-21 12.40.46.png-798bd2-1650516048863-0)]
截屏2022-04-21 12.41.09.png
以上是全部流程的截图
现在给上交互干货代码:
#import <Foundation/Foundation.h>@interface UnityPlugin : NSObjectextern "C"{ //Mark: Unity调用oc调用 extern void CallIOSMethod(char *str); }@end#import "UnityPlugin.h"#import <Foundation/Foundation.h>@implementation UnityPlugin//Mark: Unity 调用 ocvoid CallIOSMethod(char *str){ NSLog(@"Unity调用oc成功 %s",str); NSString *valueStr = [[NSString alloc] initWithUTF8String:str]; [[NSNotificationCenter defaultCenter]postNotificationName:@"callSwift" object:valueStr];}@end#import <Foundation/Foundation.h>#include <UnityFramework/UnityFramework.h>#include <stdint.h>#include <stdarg.h>@interface UnityHelper : NSObject <UnityFrameworkListener>@property (nonatomic, strong) UnityFramework *ufw;#pragma mark -加载Unity 页面- (void)initUnity:(int)argc argV:(char **)argv launchOptions:(NSDictionary *)launchOptions;#pragma mark -- (void)setExecuteHeader:(const MachHeader*)header;#pragma mark - 给Unity 发送消息方法- (void)sendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg;@end#import "UnityHelper.h"/*UnityFrameworkLoad*/UIKIT_STATIC_INLINE 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 UnityHelper ()@end@implementation UnityHelper#pragma mark - Unity/*判断Unity 是否已经初始化*/- (BOOL)unityIsInitialized{ return [self ufw] && [[self ufw] appController];}- (void)initUnity:(int)argc argV:(char **)argv launchOptions:(NSDictionary *)launchOptions{ /* 判断Unity 是否已经初始化 */ if ([self unityIsInitialized]) { return; } /* 初始化Unity */ self.ufw = UnityFrameworkLoad(); [self.ufw setDataBundleId:"com.unity3d.framework"]; [self.ufw registerFrameworkListener:self]; [self.ufw runEmbeddedWithArgc:argc argv:argv appLaunchOpts:launchOptions];}- (void)setExecuteHeader:(const MachHeader*)header{ [self.ufw setExecuteHeader:header];}#pragma mark - 给callUnity 发送消息方法- (void)sendMessageToGOWithName:(const char*)goName functionName:(const char*)name message:(const char*)msg{ [self.ufw sendMessageToGOWithName:goName functionName:name message:msg]; }@end
截屏2022-04-21 12.49.33.png
var currentUnityAppController: UnityAppController? // Unity方法 let unityPlugin = UnityHelper() //MARK: Unity 页面 func UnityHome(){ unityPlugin.initUnity(CommandLine.argc, argV: CommandLine.unsafeArgv, launchOptions: [:]) } ///测 Unity Call Swift NotificationCenter.default.addObserver(self, selector: #selector(readRSSI(notice:)), name: NSNotification.Name(rawValue: "callSwift"), object: nil) // Unity点击调用iOS成功 @objc func readRSSI(notice:NSNotification){ let noticeStr = notice.object print("测试被Unity点击调用成功66999%@",noticeStr as Any) DispatchQueue.main.asyncAfter(deadline: .now()+2) { [self] in /// 测试:iOS点击传值给Unity self.unityPlugin.sendMessageToGO(withName: "UnityCallTest", functionName: "ChangeText", message: "IOSCallUnity666") } }
最后可以相互交互了.......致敬各大平台大佬的技术支撑:为了节约你们查找时间成本.我这边贡献出下面个人所查资料,参考资料如下:
https://blog.csdn.net/qq_39162826/article/details/108740398?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-2.control
https://blog.csdn.net/wenhao_china/article/details/118305620
https://zhuanlan.zhihu.com/p/460155219
https://www.jianshu.com/p/bbf9743f6545?from=groupmessage
https://www.jianshu.com/p/26ca2c7caa44
下个准备写一篇Swift与Cocos的交互,敬请期待 |