UnityARKitPluginを使ってsimulator buildを通す


はじめに

本記事の環境

  • Unity: 2018.2.5
  • Unity ARKit Plugin: v1.5.0
  • Xcode: 10.2.1

目的

Unity ARKit Pluginを導入したUnity Projectを、iOS Simulatorビルドをし、xcode上でビルドが通る状態にする。
※ 今回は、iOS Simulatorはカメラデバイスが使えないため、ビルドが通るだけの状態が最終ゴールです。

現状確認

Unity ARKit Pluginを導入し、simulator buildをし、XCodeで開くと下記のエラーが出力され、ビルドが失敗する。

Error.log
Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:752:15: error: use of undeclared identifier 'MTLCreateSystemDefaultDevice'
    _device = MTLCreateSystemDefaultDevice();
              Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:753:5: error: use of undeclared identifier 'CVMetalTextureCacheCreate'
    CVMetalTextureCacheCreate(NULL, NULL, _device, NULL, &_textureCache);
    ^
Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:888:9: error: unknown type name 'CVMetalTextureRef'
        CVMetalTextureRef texture = NULL;
        ^
Libraries/UnityARKitPlugin/Plugins/iOS/UnityARKit/NativeInterface/ARSessionNative.mm:903:9: error: unknown type name 'CVMetalTextureRef'
        CVMetalTextureRef texture = NULL;
        ^

解決方法

基本的に、足りない宣言を追加するだけです。下記の内容をARSessionNative.mmに追記します。

ARSessionNative.mm

#if TARGET_OS_SIMULATOR
#import <Metal/MTLDevice.h>

typedef struct CV_BRIDGED_TYPE(id) __CVMetalTextureCache *CVMetalTextureCacheRef;
typedef CVImageBufferRef CVMetalTextureRef;
CV_EXPORT CVReturn CVMetalTextureCacheCreate(
                                             CFAllocatorRef CV_NULLABLE allocator,
                                             CFDictionaryRef CV_NULLABLE cacheAttributes,
                                             id <MTLDevice> CV_NONNULL metalDevice,
                                             CFDictionaryRef CV_NULLABLE textureAttributes,
                                             CV_RETURNS_RETAINED_PARAMETER CVMetalTextureCacheRef CV_NULLABLE * CV_NONNULL cacheOut ) {
    return kCVReturnSuccess;
}
CV_EXPORT CVReturn CVMetalTextureCacheCreateTextureFromImage(
                                                             CFAllocatorRef CV_NULLABLE allocator,
                                                             CVMetalTextureCacheRef CV_NONNULL textureCache,
                                                             CVImageBufferRef CV_NONNULL sourceImage,
                                                             CFDictionaryRef CV_NULLABLE textureAttributes,
                                                             MTLPixelFormat pixelFormat,
                                                             size_t width,
                                                             size_t height,
                                                             size_t planeIndex,
                                                             CV_RETURNS_RETAINED_PARAMETER CVMetalTextureRef CV_NULLABLE * CV_NONNULL textureOut ) {
    return kCVReturnSuccess;
}
CV_EXPORT id <MTLTexture> CV_NULLABLE CVMetalTextureGetTexture( CVMetalTextureRef CV_NONNULL image ) {
    return NULL;
}
#endif