Unity網頁呼叫Windows Form函式

Unity網頁呼叫Windows Form函式

這個Unity範例是台灣地圖,只有台北與花蓮可以點擊,點擊後會顯示地區的名稱在Windows Form上的TextBox,中間要設定IIS。

 

完整範例(包含Windows Form專案與Web.html)在以下網址:

 

Unity中加入呼叫未來輸出成的html內的Javascript函式ShowCounty

 

     public void PlayCamera(int PlaceID)

       {

              if (PlaceID == 0)

              {

                     anim.Play("AnimaTaipei");

                     Application.ExternalCall("ShowCounty", "Taipei");

              }

              else if (PlaceID == 1)

              {

                     anim.Play("AnimaHualien");

                     Application.ExternalCall("ShowCounty", "Hualien");

              }

 

       }

 

輸出Unity網頁,選Offline,網頁名稱為Web

會產生以下檔案,將這些檔案放到IIS,建立一個網頁應用程式,定義預設文件為Web.htmlMIME.unity application/unity3d

 

jquery.min.js

UnityObject2.js

Web.html

Web.unity3d

 

Web.html內可以改變顯示的尺寸

 

                        var config = {

                                width: 400,

                                height: 600,

                                params: { enableDebugging:"0" }       

                        };

 

                div.content {

                        margin: auto;

                        width: 400px;

                }

 

                div#unityPlayer {

                        cursor: default;

                        height: 600px;

                        width: 400px;

                }

 

Web.html內加入以下函式在script區塊內,這是被Unity呼叫的函式

function ShowCounty(name)

{

       window.external.sendMessage(name);//sendMessageWindows Form的函式

}

 

Windows Form加入一個WebBrowser的控制項,urlIIS設定的Web.html的路徑

也加入一個textBox1來顯示結果

     [System.Runtime.InteropServices.ComVisibleAttribute(true)]//<==加入ComVisibleAttribute的宣告

     public partial class Form1 : Form

     {         

           public Form1()

           {

                InitializeComponent();

                webBrowser1.ObjectForScripting = this;//add

           }

 

           public void sendMessage(string text)

           {

                 textBox1.Text = text;

           }

     }

PS:這樣即可串聯UnityWindows Form

 

 

 

Referemce:

 

How to handle javascript events via WebBrowser control for WinForms

 

Windows Application interaction with a Web Page

 

flex集成IFrameIFrame集成UnityWebPlayer直接通讯调用解决方案,flexunitywebplayer