一. 在 WPF 中使用 WebBrowser,直接打開 WebPlayer.htmlhtml
以這種方式有一個問題是. 沒法在 WebBrowser 的上面 放置其它的控件, 在運行時,都不會顯示 .web
以 HTML爲中介, 能夠方便的換場景. (真接設置 WebBrowser.Source=new Uri(@"x:\\WebPlayr.unity3d");) 便可. WPF->Unity 或 Unity->WPF 都要經過 HTML的Javascript腳本. 這種方式是目前最簡單靠譜的方式 . ~數組
二 .嵌入UntiyWebPlayer 的 ActiveX 控件 this
-----此種方式有兩個未解決的問題 1. 未能屏蔽右鍵菜單 2.不能動態的改變 UnityWebPlayer的 src 屬性spa
一. 嵌入的流程3d
1.建立一個 WPF 工程code
2.建立一個 WindowForm 自定義控件庫 (UserControl)orm
1).引入 UntiyWebPlayer COM 組件xml
2).將 這個組件拖到 UserControl 裏, 並將 Dock屬性設置爲 Fill 讓它充滿整個控件htm
3).在自定義的程序文件中,增長一個 UnityWebPlayer 的Public引用. 這樣作的目的是,以後能夠對其進行操做
4).生成 , 在 bin 中會有三個 DLL 文件 , 只有兩個有用 . 一個是 AxInterop.UnityWebPlayerAXLib 另外一個是 你定義的那個組件的 DLL.
3. 將那兩個有用的 DLL 引入到咱們的 WPF 工程中. 而且 再引入 System.Windows.Forms 及 WindowsFormIntegration.
4. 在 WPF 的XAML的 Window 標籤中 引入咱們的 自定義控件的名稱空間. 如: xmlns:unity="..." 在 <Grid> 中, 加入一個 <WindowsFormHost> 標籤,用來承載咱們的 WIndowsForm 的自定義組件. 並在其中 加入 如: <unity:UnityPlayer x:Name="UnityPlayer">. 這樣, 就將UnityWebPlayer 嵌入了 WPF中.
二. 實現 Unity3D 與 WPF 間的雙向通信
WPF -> Unity : 要在 WPF 的 MainWindow 的 CS 文件中,引用 AxUnityWebPlayerAXLib 命名空間,這樣才能使用這個 UWP COM組件的 SendMessage 方法.
如: UnityPlayer.Player.SendMessage("Cube","ChangeToBig",null); (UnityPlayer: 爲自定義的組件, Player: 爲UWP 的COM組件的引用)
SendMessage 方法的參數 : 1.物體名(string) 2.方法名(string) 3.方法參數(object).
Unity -> WPF : 在建立 自定義組件的時候 (將 COM 組件封裝成 WPF 能夠使用的 控件) . 在處理 UWP 的 OnExternalCall 事件, 它的參數 e.value , 裏面就是 字符數組,
以 ',' 分開,第一個是 方法名. 以後都是參數也爲字符串. 這種處理的事件一組就是. Unity裏的變化, 須要 WPF的界面作出相應變化的時候, 最好是寫一個,處理 ExternalCall的類, 而後使用反射使用字符串獲得相應的方法名進行處理.
網上找到的,能夠動態改變 src 的解決方法
1 // Create a ocx state object with the correct path 2 _Unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer(); 3 ((System.ComponentModel.ISupportInitialize)(_Unity)).BeginInit(); 4 _Unity.OcxState = (AxHost.State)(Resources.Unity3DOcx); 5 _Unity.TabIndex = 0; 6 Controls.Add(_Unity); 7 ((System.ComponentModel.ISupportInitialize)(_Unity)).EndInit(); 8 _Unity.src = _File; 9 AxHost.State state = _Unity.OcxState; 10 _Unity.Dispose(); 11 12 // Create the unity web player object 13 _Unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer(); 14 ((System.ComponentModel.ISupportInitialize)(_Unity)).BeginInit(); 15 this.SuspendLayout(); 16 _Unity.Dock = DockStyle.Fill; 17 _Unity.Name = "Unity"; 18 _Unity.OcxState = state; 19 _Unity.TabIndex = 0; 20 Controls.Add(_Unity); 21 ((System.ComponentModel.ISupportInitialize)(_Unity)).EndInit(); 22 this.ResumeLayout(false);