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.html,MIME為.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);//sendMessage是Windows Form的函式
}
在Windows Form加入一個WebBrowser的控制項,url為IIS設定的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:這樣即可串聯Unity到Windows Form。
Referemce:
How to handle javascript events via WebBrowser control for WinForms
Windows Application interaction with a Web Page
flex集成IFrame,IFrame集成UnityWebPlayer直接通讯调用解决方案,flexunitywebplayer