-
模拟器编译
target:UnityFramework
target:Native
-
missing symbol called
选择debug
-
MemoryManager::InitializeMemory()
0x137ab1596 <+208>: leaq 0x218a083(%rip), %rdi ; MemoryManager::InitializeMemory()::s_initializeMemory
0x137ab159d <+215>: callq 0x138c35716 ; Mutex::Unlock()
0x137ab15a2 <+220>: movq %rbx, %rdi
0x137ab15a5 <+223>: callq 0x1395d11f6 ; symbol stub for: _Unwind_Resume
-> 0x137ab15aa <+228>: ud2
0x137ab15ac <+230>: jmp 0x137ab15ae ; <+232>
0x137ab15ae <+232>: movq %rax, %rdi
0x137ab15b1 <+235>: callq 0x137929da0 ; __clang_call_terminate
-
unityView抽取
- 启动初始化引擎
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:@(argc) forKey:@"argc"];
[userDefaults synchronize];
[userDefaults setObject:[NSString stringWithFormat:@"%p",argv] forKey:@"argv"];
[userDefaults synchronize];
/// 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;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//初始化Unity
[self initUnity];
// Override point for customization after application launch.
return YES;
}
#pragma mark - Unity
/// 判断Unity是否已经初始化
- (BOOL)unityIsInitialized {
return [self ufw] && [[self ufw] appController];
}
/// 初始化Unity
- (void)initUnity {
// 判断Unity 是否已经初始化
if ([self unityIsInitialized]) return;
// 初始化Unity
self.ufw = UnityFrameworkLoad();
[self.ufw setDataBundleId:"com.unity3d.framework"];
[self.ufw registerFrameworkListener:self];
// [NSClassFromString(@"FrameworkLibAPI") registerAPIforNativeCalls:self];
NSString *argvStr = [[NSUserDefaults standardUserDefaults] valueForKey:@"argv"];
char **argv;
sscanf([argvStr cStringUsingEncoding:NSUTF8StringEncoding], "%p",&argv);
int argc = [[[NSUserDefaults standardUserDefaults] valueForKey:@"argc"] intValue];
NSDictionary *launchOptions = [[NSUserDefaults standardUserDefaults] valueForKey:@"launchOptions"];
[self.ufw runEmbeddedWithArgc:argc argv:argv appLaunchOpts:launchOptions];
}
- 延迟执行unityView的添加
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = UIColor.redColor;
// 延迟2秒执行的代码块
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// 在主线程执行的代码
NSLog(@"延迟2秒后执行的代码");
NTAppDelegate *appDelegate = (NTAppDelegate *)([UIApplication sharedApplication].delegate);
self.NTAppDelegate = appDelegate;
// Unity的view
UIView *view = self.NTAppDelegate.ufw.appController.rootViewController.view;
view.frame = CGRectMake(0, 0, 300, 500);
[self.view addSubview:view];
});
}
-
渲染暂停
https://www.cnblogs.com/U-tansuo/p/u3d_as_FrameWork.html
- (void)pauseUnity {
//[self applicationWillResignActive:[UIApplication sharedApplication]];
UnityPause(1);
}
- (void)startUnity {
//[self applicationDidBecomeActive:[UIApplication sharedApplication]];
UnityPause(0);
}
-
Native通信
Unity调用iOS
https://docs.unity3d.com/Manual/PluginsForIOS.html
- Unity:
using System.Runtime.InteropServices;
// DllImport这个方法相当于是告诉Unity,有一个CallObjCFunc函数在外部会实现。
// 使用这个方法必须要导入System.Runtime.InteropServices;
[DllImport("__Internal")]
private static extern void CallObjCFunc(string funcName);
- iOS
14 extern "C" {
15 void CallObjCFunc(const char* methodName)
16 {
17 SEL selector = NSSelectorFromString([NSString stringWithUTF8String:methodName]);
18
19 if ([Plugin respondsToSelector:selector]) {
20 [Plugin performSelector:selector];
21 }
22 }
23 }
Unity调用Anroid
https://docs.unity3d.com/Manual/PluginsForAndroid.html
https://docs.unity3d.com/cn/2021.1/Manual/AndroidJARPlugins.html
- Unity
//实例化AndroidJavaClass,传入Java类路径
AndroidJavaClass jc = new AndroidJavaClass("ccom.devnn.demo.MyJavaClass")
//如果找不到Java类,返回null
if(jc == null) return;
//调用Java类的getInstance方法获取实例
AndroidJavaObject jo = jc.CallStatic<AndroidJavaObject>("getInstance");
//如果找不到Java方法,返回null
if (jo == null) return;
string param = "Hello,Android!";
string result = jo.Call<string>("test", param); //调用test方法,返回值是字符串。
- Android
package com.devnn.demo;
public class MyJavaClass{
private static MyJavaClass instance=new MyJavaClas();
public static getInstance() {
return instance;
}
public String test(String param) {
return "This message is from Android!";
}
}
iOS调用Unity
//第一个参数:消息的接受者,或者说谁来执行方法
//第二个参数:函数名
//第三个参数:需要传的值
UnitySendMessage(const char * GameObjectName, const char *methodName, const char *msg);//注意,三个参数都是C语言字符串,没有'@'
Android调用Unity
// objectName: Unity 对象的名称
// methodName: Unity 对象绑定的脚本方法名
// message: 自定义消息
UnityPlayer.UnitySendMessage(String objectName, String methodName, String message);
该方法有一个弊端,就是发送消息的方法对应的类,必须要挂载在一个 game object 上。
该方法有一点需要注意,就是在原生触发回调接口的时候,可能跟 Unity 的主线程并不是一个线程,此时需要通知 Unity 主线程执行回调。
-
IL2CPP initialization failed
Data目录勾选UnityFramework
-
Embed Frameworks
buildsettings-EXCLUDED_ARCHS-any ios simulator sdk = arm64
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |config|
config.build_settings['IPHONEOS_DEPLOYMENT_TARGET'] = '11.0'
config.build_settings["EXCLUDED_ARCHS[sdk=iphonesimulator*]"] = "arm64"
end
end
end
-
Undefined symbol: OBJC_CLASS$_UnityFramework
不是.mm文件不支持类(+)方法
+ (void)sendMessage:(const char*)goName
[UnityFramework getInstance]
-
Use of '@import' when C++ modules are disabled, consider using -fmodules and -fcxx-modules
-fmodules
-fcxx-modules
-
dyld[447]: Library not loaded
dyld[447]: Library not loaded: @rpath/LZAvatar.framework/LZAvatar
Referenced from: <18A35ABF-00DE-3519-BD52-2A8B0DEFE447> /private/var/containers/Bundle/Application/4F68E670-8722-4863-A72B-D184A0ECE37C/LZAvatar_Example.app/LZAvatar_Example
Reason: tried: '/private/preboot/Cryptexes/OS@rpath/LZAvatar.framework/LZAvatar' (errno=2), '/System/Library/Frameworks/LZAvatar.framework/LZAvatar' (errno=2, not in dyld cache)
解决: