LoginSignup
17
14

More than 5 years have passed since last update.

Unityで各種イベントへのフックを提供するAppDelegateListener(iOSのみ)

Last updated at Posted at 2016-06-22

Unityアプリで、

  • カスタムURLスキームから起動されたときに特定の処理を行いたい
  • didReceiveMemoryWarningを受け取ったときに特定の処理を行いたい

といった場合、方法のひとつとしてAppDelegateListenerというものがあります。
ドキュメント化されていない上にネット上にもほぼ情報がなかったのでメモを残しておきます。

※ iOSのみ
※ 動作検証は Unity 5.3.5f1 で行ってます
※ 公式にはドキュメント化されておらず、Unity 4.3当時のリリースノートで言及があるのみのようです(http://unity3d.com/jp/unity/whats-new/unity-4.3

AppDelegateListenerでできること

AppDelegateListenerを使うと、下記イベント発生時に実行するハンドラを登録できます

  • didRegisterForRemoteNotificationsWithDeviceToken
  • didFailToRegisterForRemoteNotificationsWithError
  • didReceiveRemoteNotification
  • didReceiveLocalNotification
  • onOpenURL
  • applicationDidReceiveMemoryWarning
  • applicationSignificantTimeChange
  • applicationWillChangeStatusBarFrame
  • applicationWillChangeStatusBarOrientation

これだけならUnityAppControllerを継承したクラスを作成してIMPL_APP_CONTROLLER_SUBCLASSを呼び出すことでもできますが、
たとえば追加したい処理が複数ある場合には、AppDelegateListenerを使ったほうが簡単に各処理の独立性を保てるので便利です。

使い方とサンプル

  1. AppDelegateListenerプロトコルを実装したオブジェクトを生成して、 UnityRegisterAppDelegateListenerを呼び出すプログラムを作る
  2. 上記ソースファイルをPluginsディレクトリに追加する

※ コードはcapnslippさんのgistを一部参考にしました https://gist.github.com/capnslipp/036f7c98f5ccf42e8428

AppDelegateListenerTest.mm
#import "AppDelegateListener.h"

@interface AppDelegateListenerTest : NSObject <AppDelegateListener>
@end

@implementation AppDelegateListenerTest

static AppDelegateListenerTest *_instance = nil;

+ (void)load {
    if(!_instance) {
        _instance = [[AppDelegateListenerTest alloc] init];
    }
}

- (id)init {
    self = [super init];
    if(!self)
        return nil;

    _instance = self;

    // register to unity
    UnityRegisterAppDelegateListener(self);

    return self;
}

- (void)didRegisterForRemoteNotificationsWithDeviceToken:(NSNotification*)notification {
    NSLog(@"didRegisterForRemoteNotificationsWithDeviceToken was called!");
}

- (void)didFailToRegisterForRemoteNotificationsWithError:(NSNotification*)notification {
    NSLog(@"didFailToRegisterForRemoteNotificationsWithError was called!");
}

- (void)didReceiveRemoteNotification:(NSNotification*)notification {
    NSLog(@"didReceiveRemoteNotification was called!");
}

- (void)didReceiveLocalNotification:(NSNotification*)notification {
    NSLog(@"didReceiveLocalNotification was called!");
}

- (void)onOpenURL:(NSNotification*)notification {
    NSLog(@"onOpenURL was called!");
}

- (void)applicationDidReceiveMemoryWarning:(NSNotification*)notification {
    NSLog(@"applicationDidReceiveMemoryWarning was called!");
}

- (void)applicationSignificantTimeChange:(NSNotification*)notification {
    NSLog(@"applicationSignificantTimeChange was called!");
}

- (void)applicationWillChangeStatusBarFrame:(NSNotification*)notification {
    NSLog(@"applicationWillChangeStatusBarFrame was called!");
}

- (void)applicationWillChangeStatusBarOrientation:(NSNotification*)notification {
    NSLog(@"applicationWillChangeStatusBarOrientation was called!");
}

@end

上記ファイルをPluginsディレクトリに配置してビルド、実行すると、
各種イベントが発生したときに対応したハンドラメソッドが呼びだされます。

ちなみに各ハンドラは定義されてるものだけが呼び出されるようになっているので、
たとえばonOpenURLだけ欲しいなら、他のdidRegisterForRemoteNotificationsWithDeviceTokenとかdidReceiveLocalNotificationとかは省いちゃってOKです。

あとはやりたいことに応じてUnitySendMessageしたりするよう改造するとよいと思います。

17
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
17
14