开发环境:
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
具体:如下图所示
1.3选择Build Settings 具体设置看个人项目需求
1.4需要设置一下Player Settings
[图片上传中...(截屏2022-04-21 12.00.23.png-334a78-1650513624924-0)]
1.5.设置完成后就可以点击Build了
1.6 打包结束后的iOS包出来
1.7 确保打出来的项目能正常运行: 填坑Xcode设置:否则运行不了真机具体步骤如下
成功运行了.此步骤结束
到下个环节:准备一个新创建的Swift项目包
2.创建XXXX.xcworkspace
注意:Unity项目中的 Data要按照图勾对
版本:支持iOS12以上
最后Swift和Unity交互:
1.在Unity项目中创建两个文件
在Unity中放的位置
以上是全部流程的截图
现在给上交互干货代码:
#import
@interface UnityPlugin : NSObject
extern "C"
{
//Mark: Unity调用oc调用
extern void CallIOSMethod(char *str);
}
@end
#import "UnityPlugin.h"
#import
@implementation UnityPlugin
//Mark: Unity 调用 oc
void CallIOSMethod(char *str){
NSLog(@"Unity调用oc成功 %s",str);
NSString *valueStr = [[NSString alloc] initWithUTF8String:str];
[[NSNotificationCenter defaultCenter]postNotificationName:@"callSwift" object:valueStr];
}
@end
#import
#include
#include
#include
@interface UnityHelper : NSObject
@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
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的交互,敬请期待