Unity3D嵌入WPF教程
- 創建一個 類庫工程


- 添加 WindowForm 用戶控件 (UserControl)

1).引入 UntiyWebPlayer COM 組件
在工具-》選擇工具箱中找到UnityWebPlayer.dll並添加;

2)在用戶控件中添加UNityWedPlayer控件(在工具箱中直接拖拉即可)


2).將 這個組件拖到 UserControl 里, 並將 Dock屬性設置為 Fill 讓它充滿整個控件

3)之后刪除UntiyWebPlayer,生成文件

4)
編寫用戶控件的后台代碼以便加載地圖
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace U3DDisPlayControl
{
public partial class U3DControl : UserControl
{
U3DPlayer axPlayer;//繼承自AxUnityWebPlayerAXLib.AxUnityWebPlayer的類主要是實現Unity3D與WPF通信的類
public U3DControl()
{
InitializeComponent();
}
public void LoadScence(string Src)
{
//加載的路徑地址不為空
if (Src != null && Src != "")
{
this.axPlayer = new U3DPlayer();
this.axPlayer.BeginInit();//開始 ActiveX 控件的初始化。
base.Controls.Add(this.axPlayer);//往UserControl的控件集合里面添加
this.axPlayer.EndInit();
//
//Type t = this.axPlayer.GetOcx().GetType();
//t.InvokeMember("baseDownloadUrl", BindingFlags.Instance | BindingFlags.SetProperty, null, this.axPlayer.GetOcx(), new object[] { "http://127.0.0.1:8080/download_webplayer-3.x/" });
//t.InvokeMember("autoupdateURL", BindingFlags.Instance | BindingFlags.SetProperty, null, this.axPlayer.GetOcx(), new object[] { "http://127.0.0.1:8080/autodownload_webplugin-3.x" });
//
this.axPlayer.src = Src;
AxHost.State ocxState = this.axPlayer.OcxState;//獲取或設置 ActiveX 控件的持久狀態。返回結果:System.Windows.Forms.AxHost.State,表示 ActiveX 控件的持久狀態。
axPlayer.IsAccessible = false;
this.axPlayer.Dispose();
this.axPlayer = new U3DPlayer();
this.axPlayer.BeginInit();
this.axPlayer.OcxState = ocxState;
this.axPlayer.Dock = DockStyle.Fill;
base.Controls.Add(this.axPlayer);
//this.axPlayer.EndInit();
//注冊外部調用回調函數,內部腳本:Application.ExternalCall("Function", params); -> Function(params);
//this.axPlayer.OnExternalCall += new AxUnityWebPlayerAXLib._DUnityWebPlayerAXEvents_OnExternalCallEventHandler(axPlayer_OnExternalCall);
}
}
public delegate void OnReceiveMessageEventHandler(object sender, string functionname, string args);
public event OnReceiveMessageEventHandler OnReceiveMessage;//用於WPF接受Unity3D的信息
private void axPlayer_OnExternalCall(object sender, AxUnityWebPlayerAXLib._DUnityWebPlayerAXEvents_OnExternalCallEvent e)
{
if (OnReceiveMessage != null)
{
//解析函數名
string functionname = e.value.Substring(0, e.value.IndexOf("(\""));
//解析參數
string args = e.value.Substring(e.value.IndexOf("(\"") + 2, e.value.IndexOf("\")") - e.value.IndexOf("(\"") - 2);
//調用外部接受消息的事件
OnReceiveMessage(sender, functionname, args);
}
}
public void SendMessageToObject(string obj, string method, object val)//用於WPF向Unity3D發送信息
{
//設置默認值
if (obj == null || obj == string.Empty)
obj = "GameController";
if (method == null || method == string.Empty)
method = "OnMessageReceive";
//調用底層方法
this.axPlayer.SendMessage(obj, method, val);
}
public void SendMessageToObject(object val)
{
//調用另一個方法
SendMessageToObject(null, null, val);
}
public void Clear()
{
if (this.axPlayer != null)
{
base.Controls.Remove(this.axPlayer);
//this.axPlayer.EndInit();
this.axPlayer.Dispose();
// this.axPlayer = null;
}
}
}
}
4).在程序中添加一個類對 UnityWebPlayer 的Public引用. 這樣做的目的是,之后可以對其進行操作,(也可不添加)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace U3DDisPlayControl
{
public class U3DPlayer:AxUnityWebPlayerAXLib.AxUnityWebPlayer
{
public override bool PreProcessMessage(ref Message msg)
{
switch (msg.Msg)
{
//鼠標右鍵按下消息
case 0x204:
this.SendMessage("GameController", "OnRightMouseEvent", "RightMouseDown");
this.Focus();
return true;
//鼠標右鍵抬起消息
case 0x205:
this.SendMessage("GameController", "OnRightMouseEvent", "RightMouseUp");
return true;
//鼠標右鍵點擊消息
case 0x206:
this.SendMessage("GameController", "OnRightMouseEvent", "RightMouseClick");
return true;
}
return base.PreProcessMessage(ref msg);// 如果消息已由控件處理,則為 true;否則為 false。
}
}
}
4).生成 , 在 bin 中會有三個 DLL 文件 , 只有兩個有用 . 一個是 AxInterop.UnityWebPlayerAXLib 另一個是 你定義的那個自定義組件的 DLL.
將那兩個有用的 DLL 引入到我們的 WPF 工程中. 並且 再引入 System.Windows.Forms 及 WindowsFormIntegration.



使用
- 在 WPF 的XAML的 Window 標簽中 引入我們的 自定義控件的名稱空間. 如: xmlns:unity="..." 在 <Grid> 中, 加入一個 <WindowsFormHost> 標簽,用來承載我們的 WIndowsForm 的自定義組件. 並在其中 加入 如: <unity:UnityPlayer x:Name="UnityPlayer">. 這樣, 就將UnityWebPlayer 嵌入了 WPF中.


出現問題可能是組件沒有加載上去,



