Unity 3.5 Developer Preview On AIR

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

前回は、Unity 3.5のサンプルをFlashコンテンツに変換しました。
https://dev.classmethod.jp/etc/unity-3-5-developer-preview/
今回は、そのサンプルをAIRで動かしてみましょう。

1. Flashコンテンツに変換したフォルダを確認
UnityShared.swcというActionScriptライブラリも自動生成されています。
このUnityShared.swcの中にあるクラスでUnityコンテンツSWFをロードします。

2. UnityShared.swcの中

  • com.unity.IUnityContent
    Unityコンテンツを表すインターフェイスです。
    このインターフェイスを通してUnityコンテンツにアクセスすることができます。
  • com.unity.IUnityContentHost
    Unityエンジン初期化の開始と完了をハンドリングできます。
  • com.unity.UnityContentLoader
    Unityコンテンツの専用ローダーです。
  • com.unity.UnityContentParams
    Unityコンテンツの専用ローダーのパラメータです。


3. UnityShared.swcの使い方
AIRプロジェクトを作成してlibsにUnityShared.swc入れます。
src配下にUnityコンテンツSWFを置きます。今回は、UnityコンテンツSWFをMain.swfです。

ポイント

  • backgroundAlpha="0"
    Stage3Dは最背面にあるので、アプリケーションの背景は透明にします。


<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
                       xmlns:s="library://ns.adobe.com/flex/spark"
                       xmlns:mx="library://ns.adobe.com/flex/mx"
                       applicationComplete="init()"
                       backgroundAlpha="0"
                       implements="com.unity.IUnityContentHost">
    <fx:Script>
        <![CDATA[
            import com.unity.UnityContentLoader;
            import com.unity.UnityLoaderParams;

            import mx.core.UIComponent;
            import mx.events.FlexEvent;

            public var loader:UnityContentLoader;

            protected function init():void
            {
                var holder:UIComponent=new UIComponent();

                //Unityを読み込み設定
                var params:UnityLoaderParams=new UnityLoaderParams(
                    false, //scaleToStage
                    systemManager.stage.stageWidth, //unity width
                    systemManager.stage.stageHeight, //unity height
                    true, //usePreloader
                    true, //autoInit
                    true //catchGlobalErrors
                );

                loader=new UnityContentLoader("Main.swf", this, params);
                holder.addChild(loader);
                addElement(holder);
            }

            public function unityInitStart():void
            {
                //TODO
            }

            public function unityInitComplete():void
            {
                //TODO
            }
        ]]>
    </fx:Script>
</s:WindowedApplication>


4. AIRの実行
Stage3Dコンテンツなので、アプリケーション記述ファイルを下記のように修正します。

<renderMode>direct</renderMode>