我需要在使用Objective-C SDK配置Google AdWords转化跟踪的application:didFinishLaunchingWithOptions:
UnityAppController
中添加一些代码。
每次Unity生成Xcode项目时手动编辑此文件似乎容易出错,所以我正在考虑使用PostProcessBuild
属性添加一个后期构建步骤,该属性将为生成的Unity代码应用补丁。
但是,补丁文件很难维护,所以我正在寻找替代解决方案。似乎通过UnityAppController
宏对IMPL_APP_CONTROLLER_SUBCLASS
进行子类化并覆盖application:didFinishLaunchingWithOptions:
可以有其中一个。
但是在我这样做之后,另外使用相同方法的子类UnityAppController
的第三方插件(Google Play游戏)停止工作,因为不再调用其app控制器。以下是该插件的相关代码:
@interface GPGSAppController : UnityAppController {
}
@end
IMPL_APP_CONTROLLER_SUBCLASS(GPGSAppController)
所以我想知道是否有可能从多个地方继承Unity app控制器。我在网上找不到IMPL_APP_CONTROLLER_SUBCLASS
的任何文档。
或许在iOS上的Unity中添加自定义初始化代码有更好的方法吗?
答案 0 :(得分:1)
我最终调整了UnityAppController
个方法并在我的实现中进行了初始化。
如果有兴趣的话,这是我的解决方案:
#import <objc/runtime.h>
...
#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
只需将此文件另存为AppController.mm
,然后将其添加到Assets文件夹即可。 Unity会将其识别为Objective-C ++源文件,并自动将其包含在生成的Xcode项目中。
如果您需要以其他方式包含框架或修改Xcode项目,请查看PostProcessBuildAttribute
和Xcode API
。