我正在Unity 2018.1中使用Vuforia插件,并且对UnityAppController有自己的替代项。当我这样做时,Vuforia会消灭我的,所以它永远不会被召唤。
我发现了许多可能的解决方案,但我设法解决的唯一方法是用也能调用我自己的代码的Vuforia手动替换。这是一个完全的hack,而不是一个好的方法。 我找到了可能的解决方案/// Can you have more than one subclass of UnityAppController?
但是我不明白。
#import "UnityAppController.h"
namespace {
typedef BOOL (*ApplicationDidFinishLaunchingWithOptionsImp)(UnityAppController *appController,
SEL selector,
UIApplication *application,
NSDictionary *launchOptions);
ApplicationDidFinishLaunchingWithOptionsImp OriginalApplicationDidFinishLaunchingWithOptions;
BOOL ApplicationDidFinishLaunchingWithOptions(UnityAppController *appController,
SEL selector,
UIApplication *application,
NSDictionary *launchOptions) {
// Initialize Google Play Games, etc
return OriginalApplicationDidFinishLaunchingWithOptions(appController, selector, application, launchOptions);
}
IMP SwizzleMethod(SEL selector, Class klass, IMP newImp) {
Method method = class_getInstanceMethod(klass, selector);
if (method != nil) {
return class_replaceMethod(klass, selector, newImp, method_getTypeEncoding(method));
}
return nil;
}
} // anonymous namespace
@interface AppController : UnityAppController
@end
@implementation AppController
+ (void)load {
OriginalApplicationDidFinishLaunchingWithOptions = (ApplicationDidFinishLaunchingWithOptionsImp)
SwizzleMethod(@selector(application:didFinishLaunchingWithOptions:),
[UnityAppController class],
(IMP)&ApplicationDidFinishLaunchingWithOptions);
}
@end
一旦将原始代码淹没,您将在哪里添加自己的代码?
这是我用来替换Vuforia版本的代码:
@implementation VuforiaNativeRendererController
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//printf_console("Did Finish Launching with options\n");
NSURL *URL = [launchOptions valueForKey:UIApplicationLaunchOptionsURLKey];
if (URL)
{
const char *URLString = [URL.absoluteString UTF8String];
//printf_console("Application started with URL:\n");
//printf_console("%s\n", URLString);
UnitySendMessage("Scripts", "openURLComplete", URLString);
}
BOOL ret = [super application:application didFinishLaunchingWithOptions:launchOptions];
if (ret)
{
_unityView.backgroundColor = UIColor.clearColor;
}
return ret;
}
但是我将如何使用上面的“转换”方法来引入它呢?