LoginSignup
30
31

More than 3 years have passed since last update.

UnityのiOSプラグインを作成するときに毎回ぐぐってることをメモ((φ(・д・。)

Last updated at Posted at 2015-12-14

テンプレ

Unity側のテンプレ
using System.Runtime.InteropServices;
class Foo {
  [DllImport ("__Internal")]
  private static extern float FooPluginFunction();
}
Xcode側のテンプレ
extern "C" {
  float FooPluginFunction();
}

C# とC++の関数名を一緒にする => プラグイン毎にユニークな名前にしておいた方がいい。
C++の拡張子を.mmにする。
.hファイルは作成しなくても動く。

やりとりできる型

string <=> char*
string[] <=> char**
int <=> int

処理関連メモ

char* => NSString

[NSString stringWithCString:c encoding:NSUTF8StringEncoding];

char* <= NSString

(char *) [str UTF8String]
or
strdup([str UTF8String])

char** => NSArray

NSMutableArray* mutableArray = [NSMutableArray array];
// (sizeof addObject/sizeof addObject[0])で長さが取得できる
for (int i = 0; i < (sizeof addObject/sizeof addObject[0]); ++i) {

for (int i = 0; i < length; ++i) {
    [mutableArray addObject:_toNSString(array[i])];
}
※ポインタはサイズの取得方法ができない

UIViewControllerの取得

UIViewController *viewController = UnityGetGLViewController();

UINavigationControllerの取得

UINavigationController *navController = (UINavigationController*)UnityGetGLViewController();

Unity側にメッセージを送信する

UnitySendMessage("GameObjectName", "MethodName", "message");

import系

#import <objc/runtime.h> // selector使うときとか
#import <Foundation/Foundation.h> // 基本的なクラスを使うとき

おまけ

cs 側のコールバックをiOSに設定できるらしい
ただ、AndroidとiOSと挙動を似たような感じにして管理をしやすくしたいのなら、
UnitySendMessageを使うのが一番だと思う。
※追記2016/01/06
AndroidJavaRunnableってクラスで実現できそうです。

30
31
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
30
31