Skip to content

iOS Implement LobiRecSDK Unity 5 2

osawa-shunsuke edited this page Dec 17, 2014 · 6 revisions

Unity Lobi Rec SDK 導入手順書


事前にお読みください

Unity 導入準備

LobiRecフレームワークではiOSアプリにゲーム実況録画機能を提供します。

  • 対応OS: iOS 5.1 またはそれ以降のバージョン
  • 対応アーキテクチャ: armv7, armv7s, arm64
  • 事前に用意いただくファイル・情報
    • LobiCoreフレームワーク・バンドル (LobiSDK統合フレームワーク)
    • LobiRecフレームワーク・バンドル
    • Client ID

LobiRecフレームワーク構成

LobiRecフレームワークは以下のファイル群で構成されています。

フレームワークファイル

  • LobiRec.framework
    LobiRecのプログラムを含むフレームワーク本体

ヘッダーファイル

  • LobiRec.h (LobiRecフレームワーク同梱)

バンドルファイル

  • LobiRec.bundle
    LobiRecの画像ファイルやローカライズファイル含む、素材をまとめたファイルです。

Client ID

Lobi SDKを利用するには、クライアントIDを用意する必要があります。
クライアントIDは開発者向けページにて取得することができます。
詳しくはLobi SDK導入までのフローを参照してください。

インストール手順 - iOS Unity

Unitypackageの構成

  • Lobi.unitypackage
    LobiSDKとUnityプログラムとの連携を行うためのファイルが格納されたUnityパッケージです。

以下のファイルが含まれます。

  • Plugins/iOS/LobiCoreCommon.h
  • Plugins/iOS/LobiCoreAPICommon.h
  • Plugins/iOS/LobiCoreAPICommon.m
  • Plugins/iOS/LobiCoreBridge.h
  • Plugins/iOS/LobiCoreBridge.m
  • Plugins/iOS/LobiCoreAPIBridge.m
  • Plugins/iOS/LobiRecBridge.m
  • Plugins/iOS/LobiRankingBridge.m
  • Scripts/LobiCoreBridge.cs
  • Scripts/LobiCoreAPIBridge.cs
  • Scripts/LobiRankingBridge.cs
  • Scripts/LobiRankingAPIBridge.cs
  • Scripts/LobiRecBridge.cs
  • Scripts/LobiRecAPIBridge.cs

各フレームワークに必要なコードを使用してください。全てのフレームワークはLobiCoreに依存するため、LobiCore*を含めてください。

(例)LobiRecフレームワークを利用する場合に必要なコード

- Plugins/iOS/LobiCoreCommon.h
- Plugins/iOS/LobiCoreAPICommon.h
- Plugins/iOS/LobiCoreAPICommon.m
- Plugins/iOS/LobiCoreBridge.h
- Plugins/iOS/LobiCoreBridge.m
- Plugins/iOS/LobiCoreAPIBridge.m
- Plugins/iOS/LobiRecBridge.m
- Scripts/LobiCoreBridge.cs
- Scripts/LobiCoreAPIBridge.cs
- Scripts/LobiRecBridge.cs
- Scripts/LobiRecAPIBridge.cs

(例)Lobiチャットフレームワークを利用する場合に必要なコード

- Plugins/iOS/LobiCoreCommon.h
- Plugins/iOS/LobiCoreAPICommon.h
- Plugins/iOS/LobiCoreAPICommon.m
- Plugins/iOS/LobiCoreBridge.h
- Plugins/iOS/LobiCoreBridge.m
- Plugins/iOS/LobiCoreAPIBridge.m
- Scripts/LobiCoreBridge.cs
- Scripts/LobiCoreAPIBridge.cs

(例)Lobiランキングフレームワークを利用する場合に必要なコード

- Plugins/iOS/LobiCoreCommon.h
- Plugins/iOS/LobiCoreAPICommon.h
- Plugins/iOS/LobiCoreAPICommon.m
- Plugins/iOS/LobiCoreBridge.h
- Plugins/iOS/LobiCoreBridge.m
- Plugins/iOS/LobiCoreAPIBridge.m
- Plugins/iOS/LobiRankingBridge.m
- Scripts/LobiCoreBridge.cs
- Scripts/LobiCoreAPIBridge.cs
- Scripts/LobiRankingBridge.cs
- Scripts/LobiRankingAPIBridge.cs

Unityプロジェクトへの追加

通常のUnityパッケージと同様にAssets->Import Package->Custom Package...からLobi.unitypackageを選択し、インポートしてください。

AudioSettings.outputSampleRateの設定

アプリの起動時に AudioSettings.outputSampleRate = 44100 が行われるように以下のコードを記述してください。

public class Sample : MonoBehaviour {
	// Use this for initialization
	void Start () {
		// Lobi Rec SDK を使用する際は起動時に以下の設定を必ず行ってください。
        #if UNITY_IOS || UNITY_IPHONE
		AudioSettings.outputSampleRate = 44100;
        #endif
	}

録画コードの実装

録画を開始及び停止したい箇所に以下のコードを記述してください。

using Kayac.Lobi.SDK;
...

public class RecWrapper : object {

    void StartRecoding() {
        LobiRecBridge.SetMicEnable(true);             // 実況用にマイクを有効にする
        LobiRecBridge.SetMicVolume(1.0f);          // マイクの入力音量を指定する
        LobiRecBridge.SetGameSoundVolume(0.2f);    // ゲームの入力音量を指定する
    
        LobiRecBridge.SetLiveWipeStatus(LobiRecBridge.LiveWipeStatus.InCamera);    // 実況用にカメラを有効にする
        LobiRecBridge.SetWipePositionX(100.0f);                                    // 端末左端からの位置を指定する(non-retina)
        LobiRecBridge.SetWipePositionY(100.0f);                                    // 端末上端からの位置を指定する(non-retina)
        LobiRecBridge.SetWipeSquareSize(100.0f);                                   // ワイプのサイズを指定する(non-retina)
    
        LobiRecBridge.SetCapturePerFrame(2);    // 何フレームに一度録画するか指定する。

        LobiRecBridge.StartCapturing();         // 録画を開始する
    }

    void StopRecoding() {
        LobiRecBridge.StopCapturing();          // 録画を停止する
    }
}

プレイ動画シェア画面表示コードの実装

using Kayac.Lobi.SDK;
...

public class RecWrapper : object {

    void PresentLobiPost(){
        LobiRecBridge.PresentLobiPost(
            "sample title",          // 動画タイトルを設定します。
            "sample description",    // 動画詳細を設定します。
            123,                     // ゲームで獲得した得点等を設定します。
            "Category"               // 得点に紐づくカテゴリを設定します。
        );
    }
}

NOTE
カテゴリの詳細は開発者ページをご確認ください。

動画一覧画面表示コードの実装

using Kayac.Lobi.SDK;
...

public class RecWrapper : object {

    void PresentLobiPlay(){
        LobiRecBridge.PresentLobiPlay();
    }
}

Xcodeプロジェクトの出力

通常のUnityプロジェクトと同様にXcodeプロジェクトを出力してください。

Xcodeプロジェクトへの追加

LobiCoreフレームワークの追加

LobiSDKでは、各機能のチャット / Rec / ランキングを利用する際にLobiCoreフレームワークが必要になります。
LobiCore.framework、LobiCore.bundleをファイルシステムからドラッグしてXcodeプロジェクトに追加します。

Other Linker Flags の設定

TARGETSの設定 -> Build SettingsのOther Linker Flags に-ObjCが無ければ追加してください。

Info.plistの設定

Lobi本体アプリへのアカウントの接続やアプリ間連携のため、URLスキームを設定します。
nakamapapp-<クライアントID>という形式のURLスキームを登録してください。
クライアントIDの取得はLobi SDK導入までのフローを参照してください。

LobiRecフレームワークの追加

LobiRec.framework、LobiRec.bundleをファイルシステムからドラッグしてXcodeプロジェクトに追加します。

依存ライブラリの追加

プロジェクトのTARGETの設定 -> Build PhasesのLink Binary with Librariesに下記のframeworkを追加してください。
※iOS4.3を含むiOSをターゲットとする場合、フレームワークをrequiredからoptionalに設定してください。

  • LobiCore.framework
  • libsqlite3.0.dylib
  • OpenGLES.framework
  • QuartzCore.framework
  • MediaPlayer.framework
  • MessageUI.framework
  • CoreData.framework
  • CoreMedia.framework
  • Security.framework
  • CoreImage.framework (optional)
  • StoreKit.framework (optional)
  • AVFoundation.framework (optional)
  • Foundation.framework (optional)
  • AudioToolbox.framework (optional)
  • AssetsLibrary.framework (optional)
  • UIKit.framework (optional)

追加後のライブラリは以下のイメージになります。

プログラムの実装 - Lobi SDK初期化処理

UnityのViewControllerを取得、また、Lobi SDKを初期化をするためにClasses/UnityAppController.mmに以下の修正を行ってください

#import "UnityAppController.h"
#import "iPhone_Sensors.h"

...

// Lobi用にヘッダをインポートする
#import <LobiCore/LobiCore.h>
#include "../Libraries/LobiCoreBridge.h"
// 録画機能使用時のみ必要
#include "../Libraries/LobiRecBridge.h"

... 

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
    // UnityのViewControllerを取得する 
    LobiCore_set_root_view_controller_func(UnityGetGLViewController);

    // UnityPause関数を取得する
    LobiRec_set_unity_pause_func(UnityPause);

    // 初期化を行う
    [LobiCore setupClientId:@"<クライアントID>"
            accountBaseName:@"<デフォルトのユーザ名>"];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    if ([LobiCore handleOpenURL:url]) {
        return YES;
    }
    return NO;
}
  • clientID (必須)
    開発者向けページ(http://developer.lobi.co)から取得したIDを指定してください。

  • accountBaseName (必須)
    LobiSDKにサインアップする際のデフォルトのユーザー名となります。
    ユーザー名が重複している場合には自動的に異なるユーザー名に変更されます。
    例えば、accountBaseNameを"勇者"とした場合、ユーザー名は"勇者1", "勇者2", "勇者3" ...となります。
    製作アプリにアカウントの概念が存在する場合、そのアカウント名をaccountBaseNameとすることを推奨します。
    アカウントの概念が存在しない場合には固定値を入れることも可能です。

※LobiSDKの初期化設定は必ず以下のイベントが発生する前までに完了させてください。

  • LobiAPIクラスを利用してAPIを利用する
  • LobiCore / LobiRec / LobiRankingが提供するviewを表示する、機能を利用する

プログラムの実装 - Lobi Rec SDK録画処理

動画をキャプチャする為にClasses/Unity/GlesHelper.mmLobiRecの処理を挿入してください

  • setCurrentContext:withGLView: メソッド
  • createFramebuffer: メソッド
  • activeFramebufferCallback プロパティ
  • isCapturing プロパティ
  • appendFrame: メソッド
  • prepareFrame メソッド

の挿入を行います。

#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>
#include <OpenGLES/ES2/glext.h>

#include <stdio.h>

#include "GlesHelper.h"
#include "EAGLContextHelper.h"

#include "iPhone_Profiler.h"

// Lobi用にヘッダをインポートする
#import "iPhone_View.h"
#import <LobiRec/LobiRec.h>

...

void CreateUnityRenderBuffers(UnityRenderingSurface* surface)
{
	extern void* UnityCreateUpdateExternalColorSurface(int api, void* surf, unsigned texid, unsigned rbid, int width, int height, bool is32bit);
	extern void* UnityCreateUpdateExternalDepthSurface(int api, void* surf, unsigned texid, unsigned rbid, int width, int height, bool is24bit);

	int w 	= surface->targetW;
	int h 	= surface->targetH;
	int api = surface->context.API;

	unsigned texid = 0, rbid = 0;

	if(surface->msaaFB)			rbid  = surface->msaaColorRB;
	else if(surface->targetFB)	texid = surface->targetColorRT;
	else						rbid  = surface->systemColorRB;

	surface->unityColorBuffer = UnityCreateUpdateExternalColorSurface(api, surface->unityColorBuffer, texid, rbid, w, h, surface->use32bitColor);
	surface->unityDepthBuffer = UnityCreateUpdateExternalDepthSurface(api, surface->unityDepthBuffer, 0, surface->depthRB, w, h, surface->use24bitDepth);

    // コンテキスト及びフレームバッファを設定する
    [LobiRec setCurrentContext:surface->context withGLView:UnityGetGLView()];
    [LobiRec createFramebuffer:surface->systemFB];
    [LobiRec sharedInstance].activeFramebufferCallback = ^(GLuint activeFramebuffer){
        gDefaultFBO = activeFramebuffer;
    };
}

...

void PreparePresentRenderingSurface(UnityRenderingSurface* surface, EAGLContext* mainContext)
{
    // 録画中かどうか取得する
    BOOL isCapturing = [LobiRec sharedInstance].isCapturing;

	{
		EAGLContextSetCurrentAutoRestore autorestore(surface->context);

	#if GL_APPLE_framebuffer_multisample
		if(surface->msaaSamples > 1 && _supportsMSAA)
		{
			Profiler_StartMSAAResolve();

			GLuint targetFB = surface->targetFB ? surface->targetFB : surface->systemFB;

			GLES_CHK(glBindFramebufferOES(GL_READ_FRAMEBUFFER_APPLE, surface->msaaFB));
			GLES_CHK(glBindFramebufferOES(GL_DRAW_FRAMEBUFFER_APPLE, targetFB));
			GLES_CHK(glResolveMultisampleFramebufferAPPLE());

			Profiler_EndMSAAResolve();
		}
	#endif

		if(surface->allowScreenshot && UnityIsCaptureScreenshotRequested())
		{
			GLint targetFB = surface->targetFB ? surface->targetFB : surface->systemFB;
			GLES_CHK(glBindFramebufferOES(GL_FRAMEBUFFER_OES, targetFB));
			UnityCaptureScreenshot();
		}
	}

	if(surface->targetColorRT)
	{
		// shaders are bound to context
		EAGLContextSetCurrentAutoRestore autorestore(mainContext);

		gDefaultFBO = surface->systemFB;
		GLES_CHK(glBindFramebufferOES(GL_FRAMEBUFFER_OES, gDefaultFBO));
		UnityBlitToSystemFB(surface->targetColorRT, surface->targetW, surface->targetH, surface->systemW, surface->systemH);
	}
    // 録画フレームを追加する
    else {
        [LobiRec appendFrame:surface->systemFB];
    }
    
#if GL_EXT_discard_framebuffer
	// if(_supportsDiscard)
    if(!isCapturing && _supportsDiscard)    // 条件に録画中かどうかを追加する
	{
		EAGLContextSetCurrentAutoRestore autorestore(surface->context);

		GLenum	discardAttach[] = {GL_COLOR_ATTACHMENT0_OES, GL_DEPTH_ATTACHMENT_OES, GL_STENCIL_ATTACHMENT_OES};

		if(surface->msaaFB)
			GLES_CHK(glDiscardFramebufferEXT(GL_READ_FRAMEBUFFER_APPLE, 3, discardAttach));

		if(surface->targetFB)
		{
			GLES_CHK(glBindFramebufferOES(GL_FRAMEBUFFER_OES, surface->targetFB));
			GLES_CHK(glDiscardFramebufferEXT(GL_FRAMEBUFFER_OES, 3, discardAttach));
		}

		GLES_CHK(glBindFramebufferOES(GL_FRAMEBUFFER_OES, surface->systemFB));
		GLES_CHK(glDiscardFramebufferEXT(GL_FRAMEBUFFER_OES, 2, &discardAttach[1]));
	}
#endif
}

void SetupUnityDefaultFBO(UnityRenderingSurface* surface)
{
	extern GLint gDefaultFBO;
	if(surface->msaaFB)			gDefaultFBO = surface->msaaFB;
	else if(surface->targetFB)	gDefaultFBO = surface->targetFB;
	else						gDefaultFBO = surface->systemFB;

	GLES_CHK(glBindFramebufferOES(GL_FRAMEBUFFER_OES, gDefaultFBO));
    // 録画フレームの準備を行う
    [LobiRec prepareFrame];
}

...

LobiSDKの多言語化対応

LobiSDKは現在、日本語と英語の表記に対応しています。 日本語のみを対象としているゲーム内のLobiSDKにおいて英語しか表示されないといった場合には、導入されるプロジェクト自体のデフォルト言語を変更する必要があります。

下図のようにTARGETのInfoタブより、Localization native development regionの値をJapanに設定し、Localizationsの値をJapaneseに変更することで対応可能です。

デフォルトの言語を変更しても日本語のLobiSDKが表示されない場合は、プロジェクトにInfoPlist.stringsを追加します。

Xcode右のユーティリティを開き、InfoPlist.stringsのローカライズを行います。

追加したInfoPlist.stringsが言語ごとに作成されます。


他のフレームワークの実装・導入

Clone this wiki locally