如何在WPF中mmd怎么载入场景Unity3D场景

/NuclearBoy/p/6092221.html
写在前面:
把Unity3D嵌入winform或者wpf程序,过去大部分使用UnityWebPlayer插件来实现,这个插件其实就是网页上播放unity页游的插件。
但是使用UnityWebPlayer嵌入桌面开发有各种问题,我认为最大的问题是效率问题(加载缓慢),毕竟是网页的加载方式,而且可以确认未来也不会得到任何优化。
由于WebGL的高速发展,unity公司认识到了webplayer十分鸡肋,毕竟WebGL不需要任何插件可以直接显示3d内容了,所以Unity3D在5.4.x版本以后明确表示
不再支持webplayer了,所以桌面上UnityWebPlayer插件能不用也就别用了吧。所以大家不要走弯路!!
主要内容:
将Unity嵌入桌面程序最好的方式是嵌入unity生成的exe程序,winform程序和unity之间通过socket进行通讯,我认为
这也是效率最高,效果最好和最好实现的方式。在Unity程序脚本中,嵌入socket内容,我推荐做成客户端(client),使用wpf程序做服务器端,这是一个谁是主体的问题。
这样wpf可以加载多个unity程序。
嵌入后的结果如下图所示(请无视具体内容):
下面简单写了一个脚本,其中man是游戏中的一个gameobject对象。就是上图中穿蓝衣服的男人。在他身上挂着socket脚本如下:
using UnityE
using System.C
using System.Net.S
public class demoshows : MonoBehaviour {
public GameO
const int portNo = 500;
private TcpClient _
string Error_M
void Start () {
this._client = new TcpClient();
this._client.Connect(&127.0.0.1&, portNo);
data = new byte[this._client.ReceiveBufferSize];
//SendMessage(txtNick.Text);
SendMessage(&Unity Demo Client is Ready!&);
this._client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
catch (Exception ex)
void Update () {
transform.Rotate(new Vector3(0, 1, 0),0.1f);
public void rotation()
transform.Rotate(new Vector3(0, 10, 0));
//targetRotation = Quaternion.Euler(45.0f, 45.0f, 45.0f);
//// 直接设置旋转角度
//transform.rotation = targetR
////man.transform.rotation.SetAxisAngle(new Vector3(0, 1, 0), 30);;
public void translateX(float x)
transform.Translate(new Vector3(x,0,0));
public void translateY(float y)
transform.Translate(new Vector3(0, y, 0));
public void translateZ(float z)
transform.Translate(new Vector3(0, 0, z));
void OnGUI()
GUI.Label(new Rect(50, 50, 150,50 ), Error_Message);
public new void SendMessage(string message)
NetworkStream ns = this._client.GetStream();
byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
ns.Write(data, 0, data.Length);
ns.Flush();
catch (Exception ex)
Error_Message = ex.M
//MessageBox.Show(ex.ToString());
public void ReceiveMessage(IAsyncResult ar)
//清空errormessage
Error_Message = &&;
int bytesR
bytesRead = this._client.GetStream().EndRead(ar);
if (bytesRead & 1)
Debug.Log(System.Text.Encoding.ASCII.GetString(data, 0, bytesRead));
string message = System.Text.Encoding.ASCII.GetString(data, 0, bytesRead);
switch (message)
translateX(1);
translateX(-1);
translateY(1);
translateY(-1);
translateZ(1);
translateZ(-1);
Error_Message = &unknown command&;
this._client.GetStream().BeginRead(data, 0, System.Convert.ToInt32(this._client.ReceiveBufferSize), ReceiveMessage, null);
catch (Exception ex)
Error_Message = ex.M
void OnDestroy()
this._client.Close();
脚本很简单,就是通过向unity程序发送消息(1~6)实现模型的平移。
服务器端,在wpf程序中简单建立一个socket类,ip和端口要和unity对应,在程序启动时先建立服务器端。
using System.Net.S
using System.N
using System.T
using System.D
using System.T
namespace Demo_Song
class TcpServer
//私有成员
private static byte[] result = new byte[1024];
private int myProt = 500;
static Socket serverS
static Socket clientS
Thread myT
static Thread receiveT
public int port { get; set; }
internal void StartServer()
//服务器IP地址
IPAddress ip = IPAddress.Parse(&127.0.0.1&);
serverSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
serverSocket.Bind(new IPEndPoint(ip, myProt));
//绑定IP地址:端口
serverSocket.Listen(10);
//设定最多10个排队连接请求
Debug.WriteLine(&启动监听{0}成功&, serverSocket.LocalEndPoint.ToString());
//通过Clientsoket发送数据
myThread = new Thread(ListenClientConnect);
myThread.Start();
internal void QuitServer()
serverSocket.Close();
clientSocket.Close();
myThread.Abort();
receiveThread.Abort();
internal void SendMessage(string msg)
clientSocket.Send(Encoding.ASCII.GetBytes(msg));
/// &summary&
/// 监听客户端连接
/// &/summary&
private static void ListenClientConnect()
while (true)
clientSocket = serverSocket.Accept();
clientSocket.Send(Encoding.ASCII.GetBytes(&Server Say Hello&));
receiveThread = new Thread(ReceiveMessage);
receiveThread.Start(clientSocket);
catch (Exception)
/// &summary&
/// 接收消息
/// &/summary&
/// &param name=&clientSocket&&&/param&
private static void ReceiveMessage(object clientSocket)
Socket myClientSocket = (Socket)clientS
while (true)
//通过clientSocket接收数据
int receiveNumber = myClientSocket.Receive(result);
Debug.WriteLine(&接收客户端{0}消息{1}&, myClientSocket.RemoteEndPoint.ToString(), Encoding.ASCII.GetString(result, 0, receiveNumber));
catch (Exception ex)
Debug.WriteLine(ex.Message);
myClientSocket.Shutdown(SocketShutdown.Both);
myClientSocket.Close();
catch (Exception)
使用socket一定要注意使用线程,并且退出时及时结束,我这里实现的也不是很好,谁有更好的方法可以告诉我。
下面大概就是server的启动和释放,效果还好,至少不卡死....
TcpServer WpfS
int size_state = 0;
public MainWindow()
InitializeComponent();
this.Closed += MainWindow_C
this.Activated += MainWindow_A
this.Deactivated += MainWindow_D
WpfServer = new TcpServer();
WpfServer.StartServer();
void MainWindow_Closed(object sender, EventArgs e)
unityhost.Form1_FormClosed();
WpfServer.QuitServer();
关于socket通讯的问题大概就这样,大家估计更关系如何嵌入的问题
如何嵌入?
首先在wpf程序中建立一个winform的自定义控件(不是wpf控件)usercontrol
在usercontrol内新建一个panel(或者其他带有句柄的控件),并设置dock属性为fill。
启动unity.exe,通过几个api将unity窗口附加在panel句柄上。
(说明:借鉴别人的程序)
需要把unity程序命名为child.exe,放在下面指定位置(Debug\UnityApp\Child.exe,或者release)
process.StartInfo.FileName =Application.StartupPath +@&\UnityApp\Child.exe&;
详细代码如下:
using System.Windows.F
using System.Runtime.InteropS
using System.D
using System.T
namespace Demo_Song
public partial class UnityControl : UserControl
[DllImport(&User32.dll&)]
static extern bool MoveWindow(IntPtr handle, int x, int y, int width, int height, bool redraw);
internal delegate int WindowEnumProc(IntPtr hwnd, IntPtr lparam);
[DllImport(&user32.dll&)]
internal static extern bool EnumChildWindows(IntPtr hwnd, WindowEnumProc func, IntPtr lParam);
[DllImport(&user32.dll&)]
static extern int SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
private IntPtr unityHWND = IntPtr.Z
private const int WM_ACTIVATE = 0x0006;
private readonly IntPtr WA_ACTIVE = new IntPtr(1);
private readonly IntPtr WA_INACTIVE = new IntPtr(0);
public UnityControl()
InitializeComponent();
this.Load += UnityControl_L
panel1.Resize+=panel1_R
private void UnityControl_Load(object sender, EventArgs e)
process = new Process();
process.StartInfo.FileName =Application.StartupPath +@&\UnityApp\Child.exe&;
process.StartInfo.Arguments = &-parentHWND & + panel1.Handle.ToInt32() + & & + mandL
process.StartInfo.UseShellExecute = true;
process.StartInfo.CreateNoWindow = true;
process.Start();
process.WaitForInputIdle();
// Doesn't work for some reason ?!
//unityHWND = process.MainWindowH
EnumChildWindows(panel1.Handle, WindowEnum, IntPtr.Zero);
unityHWNDLabel.Text = &Unity HWND: 0x& + unityHWND.ToString(&X8&);
catch (Exception ex)
unityHWNDLabel.Text = ex.M
//MessageBox.Show(ex.Message);
internal void ActivateUnityWindow()
SendMessage(unityHWND, WM_ACTIVATE, WA_ACTIVE, IntPtr.Zero);
internal void DeactivateUnityWindow()
SendMessage(unityHWND, WM_ACTIVATE, WA_INACTIVE, IntPtr.Zero);
private int WindowEnum(IntPtr hwnd, IntPtr lparam)
unityHWND =
ActivateUnityWindow();
private void panel1_Resize(object sender, EventArgs e)
MoveWindow(unityHWND, 0, 0, panel1.Width, panel1.Height, true);
ActivateUnityWindow();
// Close Unity application
internal void Form1_FormClosed()
process.CloseMainWindow();
Thread.Sleep(1000);
while (process.HasExited == false)
process.Kill();
catch (Exception)
internal void Form1_Activated()
ActivateUnityWindow();
internal void Form1_Deactivate()
DeactivateUnityWindow();
最后附上源码,github重置密码死活连不上,现在就传压缩包到百度云把,vs版本较高(2012)以上可以打开,注意,低版本打不开工程。
链接: /s/1gf4vIiZ
密码: fv9f
貌似写得有点长了,能看到这里的人不多吧哈哈,希望有做类似需求的人少走弯路吧,碎觉去,over~。
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:8435次
排名:千里之外
转载:15篇
(1)(5)(1)(2)(1)(7)1525人阅读
如何解决(52)
WinForm中可以顺利载入U3D场景,但是WPF中一直报错:【未能加载文件或程序集“Interop.UnityWebPlayerAXLib….】
初步判断是因为缺少相关dll引用导致的,如WindowsFormIntegration.dll;
在WPF中加载U3D场景的步骤:
创建一个 WPF 工程;
创建一个 WindowForm 自定义控件库 (UserControl)
引入 UntiyWebPlayer COM 组件(AxInterop.UnityWebPlayerAXLib.dll和Interop.UnityWebPlayerAXLib);
将AxInterop.UnityWebPlayerAXLib.UnityWebPlayer这个组件拖到 UserControl 里, 并将 Dock属性设置为 Fill 让它充满整个控件;
在自定义的程序文件中,增加一个 UnityWebPlayer 的Public引用. 这样做的目的是,之后可以对其进行操作;
生成 , 在 bin 中会有三个 DLL 文件 , 只有两个有用 . 一个是 AxInterop.UnityWebPlayerAXLib 另一个是 你定义的那个组件的 DLL;
将那两个有用的 DLL 引入到我们的 WPF 工程中. 并且 再引入 System.Windows.Forms.dll 及 WindowsFormIntegration.
(有两个地方要注意:1.必须引用【System.Windows.Forms.dll】 及 【WindowsFormIntegration.dll】;2.将【Interop.UnityWebPlayerAXLib.dll】手动拷贝到debug/release目录下;)
在 WPF 的XAML的 Window 标签中 引入我们的 自定义控件的名称空间. 如: xmlns:unity=”…” 在
中, 加入一个
标签,用来承载我们的 WIndowsForm 的自定义组件. 并在其中 加入 如: . 这样, 就将UnityWebPlayer 嵌入了 WPF中.
部署项目到客户机器时,建议安装Full版本的【UnityWebPlayerFull.exe】,否则会出现断网的情况下,Unity3D场景无法加载显示;
下载地址:
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:77697次
积分:1594
积分:1594
排名:千里之外
原创:60篇
评论:94条
(2)(13)(42)(1)(1)(1)(1)(1)(4)(2)(1)(7)(1)(1)(1)

我要回帖

更多关于 unity 重新载入场景 的文章

 

随机推荐