您可以拥有一个以上的UnityAppController子类吗? [英] Can you have more than one subclass of UnityAppController?

查看:594
本文介绍了您可以拥有一个以上的UnityAppController子类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在UnityAppControllerapplication:didFinishLaunchingWithOptions:中添加一些代码,以便使用其Objective-C SDK配置Google AdWords转化跟踪.

I need to add some code to application:didFinishLaunchingWithOptions: in UnityAppController that configures Google AdWords Conversion Tracking using their Objective-C SDK.

每次Unity生成Xcode项目时手动编辑此文件似乎容易出错,因此我正在考虑使用PostProcessBuild属性添加后期构建步骤,该步骤将对生成的Unity代码应用补丁.

Editing this file manually each time Unity generates the Xcode project seems error prone, so I'm thinking about adding a post build step using the PostProcessBuild attribute that would apply a patch for generated Unity code.

但是,补丁文件很难维护,因此我正在寻找替代解决方案.似乎通过IMPL_APP_CONTROLLER_SUBCLASS宏子类化UnityAppController并覆盖application:didFinishLaunchingWithOptions:可以是其中之一.

However, patch files are hard to maintain so I'm looking for alternative solutions. It seems that subclassingUnityAppController via the IMPL_APP_CONTROLLER_SUBCLASS macro and overriding application:didFinishLaunchingWithOptions: there can be one of them.

但是,执行此操作后,另一个使用相同方法也将UnityAppController子类化的第三方插件(Google Play游戏)停止工作,因为不再调用其应用程序控制器.这是该插件的相关代码段:

But after I do this another third party plugin (Google Play Games) that also subclasses UnityAppController using the same method stops working because its app controller is no longer called. Here is a relevant piece of code from that plugin:

@interface GPGSAppController : UnityAppController {
}

@end

IMPL_APP_CONTROLLER_SUBCLASS(GPGSAppController)

所以我想知道是否有可能从多个地方对Unity应用程序控制器进行子类化.我找不到在线IMPL_APP_CONTROLLER_SUBCLASS的任何文档.

So I was wondering if it's possible to subclass the Unity app controller from multiple places. I could not find any documentation for IMPL_APP_CONTROLLER_SUBCLASS online.

或者也许有更好的方法在iOS的Unity中添加自定义初始化代码?

Or maybe there is a better approach for adding custom initialization code in Unity on iOS?

推荐答案

我最终解决了UnityAppController方法并对其实现进行了初始化.

I ended up swizzling UnityAppController methods and doing initialization in my implementation of them.

如果有人感兴趣,这是我的解决方案:

Here is my solution in case anyone is interested:

#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项目中.

Simply save this file as AppController.mm and add it to your Assets folder. Unity will recognize it as an Objective-C++ source file and automatically include it in the generated Xcode project.

如果您需要包括框架或以其他方式修改Xcode项目,请查看 PostProcessBuildAttribute Xcode API .

If you need to include frameworks or modify your Xcode project in other ways take a look at PostProcessBuildAttribute and Xcode API.

这篇关于您可以拥有一个以上的UnityAppController子类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆