55
57

More than 5 years have passed since last update.

Unity iOS/Android共用プラグイン C++ライブラリ作成

Last updated at Posted at 2015-06-01

■DLLImportのDLL名の注意点

 めっちゃはまった、下記の方を参考にしたら解決
 ライブラリ名がlibhello.soだった場合、DLLImportに設定するのはlibと.soを除いた部分を設定する事。

 http://qiita.com/hibiki29/items/527544abccc07563e7a7

こちらのページに従ってAndroidNDKをインストール
 http://dench.flatlib.jp/opengl/androidsdk

■Android NDK の install

下記から NDK のアーカイブをダウンロードします。
http://developer.android.com/tools/sdk/ndk/index.html
NDK r8e から 64bit OS 版を選ぶことができるようになっています。
例えば Windows x64 上で開発する場合 android-ndk-r10e-windows-x86_64.exe をダウンロードします。(頻繁に更新されるのでバージョン番号は新しいものに置き換えてください)
自己解凍形式なので、実行して任意のフォルダに展開します。
解凍方法
Windows の場合はそのまま exe を実行します。
Linux / Mac OS X の場合は実行属性を追加してからコマンドラインで実行します。
chmod 700 android-ndk-r10e-linux-x86_64.bin
./android-ndk-r10e-linux-x86_64.bin
展開場所を説明では仮に C:\android\android-ndk-r10e としておきます。
環境変数の PATH にも NDK のフォルダを追加しておきます。
例: C:\android\android-ndk-r10e を追加します。

■AndroidNDK
 http://developer.android.com/tools/sdk/ndk/index.html

■下記ページの内容を丸パクリで真似をしてみる

リンクが死んでたのでキャッシュから取得
http://webcache.googleusercontent.com/search?q=cache:zQ5sVulM7OYJ:devops.vccorp.net/blog/2015041701/+&cd=1&hl=ja&ct=clnk&gl=jp

UnityでAndroid、iOS両対応のC++プラグインの実装

はじめに

Unityでは、プラグインとしてC言語(C++)で作成したスタティックライブラリの呼び出しが可能です。
今回は、Android、iOSで共通のコードで作成したライブラリの作成について紹介します。

前提条件

Unity 5.0.1 Personal Edition
開発環境:Mac OS
Xcode
Andorid SDK, NDK
実機での実行(Unity Pro版ではシミュレータでの実行も可能※1)
※1 iOSではすこし工夫が必要なようです。参考


ここからは自分のも交えて改良

■フォルダ・ファイル構成は最終的に下記のようになる。

Assets
|-------Plugins
|      |------PliginTest.cs
|      |
|      |------iOS
|      |------Android
|      |------src
|          |------helloworld
|                 |-------Android.mk
|                 |-------Application.mk
|                 |-------build_plugin.sh
|                 |-------hello.cpp
|                 |-------hello.h
|                 |-------helloworld.xcodeproj
|                 |-------helloworldTests
|                        |---------Info.plist
|
|-------Scripts
|------PluginCall.cs

Xcodeを立ち上げ下記のようにCocoa Touch Static Library を選択

スクリーンショット 2015-06-01 14.34.38.png

helloworld を入力してプロジェクトを作成

スクリーンショット 2015-06-01 14.36.33.png

helloworld.m helloworld.h を削除して hello.cpp hello.h を作る

スクリーンショット 2015-06-01 14.39.46.png

スクリーンショット 2015-06-01 14.38.39.png

hello.cpp hello.h を下記のようにする。

■ANDROID_LOG_DEBUGを使用するには*.mkファイルも含めて下記の設定が必要
http://gjtwiki.wiki.fc2.com/wiki/NDK%2FDDMS%E3%81%AELog%E3%81%AB%E5%87%BA%E5%8A%9B%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95

●hello.cpp

#include "hello.h"
#include <stdio.h>

#ifdef __ANDROID__
#include <android/log.h>
#endif

int test(){
#ifdef __ANDROID__
    __android_log_write(ANDROID_LOG_DEBUG, "hello.cpp", "Hello,World!");
#else
    printf("Hello,World!\n");
#endif
    return 0;
}

●hello.h

#ifndef __hello__hello__
#define __hello__hello__

extern "C" {
    int test();
}

#endif /* defined(__hello__hello__) */

iOS側の設定

■Plugin/iOSの下にライブラリが作成されるようにしたいので、Xcodeの設定を
  下記のようにする。

Per-configuration Build Products Path = ../../iOS
Build Active Architecture Only -> Release = Yes
iOS Deployment Target = iOS 5.0

スクリーンショット 2015-06-01 14.46.57.png

 デバイスを接続してビルドするとiOSフォルダの下にのlibhelloworld.aを置く

Android側の設定

■ANDROID_NDK_ROOTAndroidNDKのパスを設定する。

●Android.mk

include $(CLEAR_VARS)

LOCAL_ARM_MODE  := arm
LOCAL_PATH      := $(NDK_PROJECT_PATH)
LOCAL_MODULE    := libhello
LOCAL_CFLAGS    := -Werror
LOCAL_SRC_FILES := hello.cpp
LOCAL_LDLIBS    := -llog

include $(BUILD_SHARED_LIBRARY)

●Application.mk

■stlを使用するには下記の宣言が必要
APP_STL := gnustl_static

APP_OPTIM        := release
APP_ABI          := armeabi
APP_PLATFORM     := android-8
APP_BUILD_SCRIPT := Android.mk
APP_STL := gnustl_static

●build_plugin.sh

ANDROID_NDK_ROOT=AndroidNDKのあるフォルダのパス

$ANDROID_NDK_ROOT/ndk-build NDK_PROJECT_PATH=. NDK_APPLICATION_MK=Application.mk $*
mv libs/armeabi/libhello.so ../../Android/

rm -rf libs
rm -rf obj

■build_plugin.shのあるパスまでターミナルで移動して下記コマンドを打つ

コマンド:sh build_plugin.sh

下記のように.soファイルが作成されたら成功

[armeabi] Compile++ arm : hello <= hello.cpp
[armeabi] StaticLibrary : libstdc++.a
[armeabi] SharedLibrary : libhello.so
[armeabi] Install : libhello.so => libs/armeabi/libhello.so

Unity側

CanvasとButtonでクリックするとOnClickedButton()をコールするようにしておく

■PluginCall.cs

using UnityEngine;
using System.Collections;

public class PluginCall : MonoBehaviour {

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void OnClickedButton(){
        Debug.Log("OnClickedButton");
        PliginTest.helloTest ();

    }
}

■PliginTest.cs

using UnityEngine;
using System.Runtime.InteropServices;

public class PliginTest {

    #if UNITY_IPHONE || UNITY_XBOX360
    [DllImport ("__Internal")]
    #else
    [DllImport ("hello")]
    #endif

    private static extern int test ();

    public static int helloTest() {
        return test ();
    }
}

■下記のようにInspector上でAny Platformを選択してApply
 (なんか、これをしないと警告がでる)

スクリーンショット 2015-06-01 15.10.10.png

■そして・・・ようやくビルド・・・めっちゃしんどいねんけど・・

■XcodeのログやAndroidStudioのLogcatを見るとHello、Worldが表示されてるのを確認

 ●Android Studio のログ
 スクリーンショット 2015-06-01 15.15.39.png

 ●xCode のログ
スクリーンショット 2015-06-01 15.18.59.png

55
57
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
55
57