- iOSアプリ開発のトレーニングセミナー
- Androidアプリ開発のトレーニングセミナー
- Flex4のカスタムコンポーネントのトレーニングセミナー
- FlexやAIRのアプリケーション開発のトレーニングセミナー
- AIR 3 Native Extension を使ったアプリケーション開発のトレーニングセミナー
申し込みはこちらから http://www.akabana.net
申し込みはこちらから http://www.akabana.net
Flash Player 11.2 beta 4
http://labs.adobe.com/technologies/flashplatformruntimes/flashplayer11-2/
AIR 3.2 beta 4
http://labs.adobe.com/technologies/flashplatformruntimes/air3-2/
playerglobal.swc
http://download.macromedia.com/pub/labs/flashplatformruntimes/flashplayer11-2/flashplayer11-2_p4_playerglobal_011912.swc
Unity と Flashの連携させるために相互のメソッドを呼び出す方法があります。
ActionScript → Unity
GameObjectに対してsendMessageを呼び出せます。
※現状、引数をいれるとエラーになります。引数なしならOK
・Unity側の関数定義
function MethodName(data){
_message = data;
}
・ActionScript側からの呼び出し
unityContent.sendMessage("GameObjectName","MethodName",data);
参考URL:GameObject.SendMessage
http://unity3d.com/support/documentation/ScriptReference/GameObject.SendMessage.html
Unity → ActionScript
Unityの中からActionScriptのメソッドも呼び出せます。
・ActionScript側での関数定義
IUnityContentHostを実装したクラスに関数型の変数を用意します。
public var MyFunction1:Function = _MyFunction1;
private function _MyFunction1(...args):void
{
}
・Unity側から呼び出し
Application.ExternalCall ("MyFunction1");
参考URL:Application.ExternalCall
http://unity3d.com/support/documentation/ScriptReference/Application.ExternalCall.html
Unity は、GUI Scripting Guideというものがあります。
http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html
下記のような感じで、ボタンやラベルなどが生成できます。
function OnGUI () {
GUI.Box (Rect (10,10,100,90), "Loader Menu");
if (GUI.Button (Rect (20,40,80,20), "Level 1")) {
Application.LoadLevel (1);
}
if (GUI.Button (Rect (20,70,80,20), "Level 2")) {
Application.LoadLevel (2);
}
}
これをFlashコンテンツにしてAIRに入れると下記のようになります。
簡単なUIはそろっているのでSkinを変えたりすれば実用レベルだ。
簡単な作り方
1. [File]メニュー→[New Project]でプロジェクト作成
2. [Assets]メニュー→[Create]→[JavaScript]でスクリプトを定義
3. [GameObject]メニュー→[Create Empty]で左下の[Hierarchy]に追加
4. [Hierarchy]ビューに追加したGameObjectを選択する
5. [Component]メニュー→[Script]→[NewBehaviourScript]を選択
6. 右の[Inspector]ビューに[NewBehaviourScript]が追加
7. [Inspector]タブの[NewBehaviourScript]の歯車をクリックして[Edit Script]で編集可能
8. 上部真ん中の再生ボタンを押して実行

Unity 3.5 Developer Previewが公開されました。
http://unity3d.com/unity/preview/
このリリースでは、Flash deployment アドオンが試すことができます。
UnityのコンテンツをFlashのStage3Dを使ったコンテンツとしてビルドすることができます。
サンプルで試してみましょう。
Unity 公式サイト サンプル
下記よりサンプルがダウンロードできます。Flashコンテンツに変換してみましょう。
http://unity3d.com/support/resources/example-projects/
2. [File]メニューから[Build Settings...]をクリック

5. ビルド&実行
[Build]は、SWFファイル作成のみ
[Build And Run]は、SWFファイル作成してブラウザを起動
7. Flashコンテンツに変換したフォルダを確認
下記に生成されたSWFファイルなどが入っています。
C:\Users\Public\Documents\Unity Projects\AngryBots
次に、そのサンプルをAIRで動かしてみましょう。
UnityShared.swcというActionScriptライブラリも自動生成されています。
このUnityShared.swcの中にあるクラスでUnityコンテンツSWFをロードします。
8. UnityShared.swcの中
9. UnityShared.swcの使い方
AIRプロジェクトを作成してlibsにUnityShared.swc入れます。
src配下にUnityコンテンツSWFを置きます。今回は、UnityコンテンツSWFをMain.swfです。
ポイント
<?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>
10. AIRの実行
Stage3Dコンテンツなので、アプリケーション記述ファイルを下記のように修正します。
<renderMode>direct</renderMode>
Android アプリのルートViewの取得
ViewGroup decor = (ViewGroup)this.getWindow().getDecorView();
Android アプリのコンテンツルートViewの取得
ViewGroup contentRoot = (ViewGroup)this.findViewById(android.R.id.content);
AIR for Android アプリのコンテンツルートViewの取得
ViewGroup contentRoot = (ViewGroup)activity.findViewById(android.R.id.content); ViewGroup airContentGroup = (ViewGroup)contentRoot.getChildAt(0);
AIR for Android アプリのコンテンツルートViewの子供たち
たぶんAIRのアプリがここにおかれそう。
AIRWindowSurfaceView view =(AIRWindowSurfaceView)airContentGroup.getChildAt(0);
たぶんこのほかにもStageなんたらってクラスはここにViewが置かれると推測されます。
自前Stageなんたらって作るならこのairContentGroup に置きましょう。