Unity3D嵌入WPF教程
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中.
出现问题可能是组件没有加载上去,
Unity3D嵌入WPF教程的更多相关文章
- Unity3D 将 Unity 嵌入WPF中的一些研究笔记
一. 在 WPF 中使用 WebBrowser,直接打开 WebPlayer.html 以这种方式有一个问题是. 无法在 WebBrowser 的上面 放置其它的控件, 在运行时,都不会显示 . 以 ...
- Unity3d ngui基础教程
Unity3d ngui基础教程 NGUI教程:步骤1-Scene 1.创建一个新的场景(New Scene).2.选择并删除场景里的MainCamera.3.在NGUI菜单下选择Create a N ...
- Winform 程序嵌入WPF程序 并发送消息
废话不多说,先看解决方案目录 WindowsFormsDemo是主程序,WpfApp是嵌入的WPF程序,先看WPF程序,程序默认启动的页面是MainWindow.xaml,这里注释掉App.xaml里 ...
- 在WinForm应用程序中嵌入WPF控件
我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...
- 在WinForm里嵌入WPF模拟公交运行状态
公司有个公交项目,模拟公交运行的时候使用的纯WinForm技术,5秒钟刷新一次,不仅看起来感觉很丑,而且性能上很有问题,听说一段时间后就会因为内存问题崩溃(估计是没释放非托管资源吧,不断重绘,非托管资 ...
- WPF教程002 - 实现Step步骤条控件
原文:WPF教程002 - 实现Step步骤条控件 在网上看到这么一个效果,刚好在用WPF做控件,就想着用WPF来实现一下 1.实现原理 1.1.该控件分为2个模块,类似ComboBox控件分为Ste ...
- WPF 精修篇 Winform 嵌入WPF控件
原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现W ...
- WPF教程十一:简单了解并使用控件模板
WPF教程十一:简单了解并使用控件模板 这一章梳理控件模板,每个WPF控件都设计成无外观的,但是行为设计上是不允许改变的,比如使用Button的控件时,按钮提供了能被点击的内容,那么自由的改变控件外观 ...
- WPF教程九:理解WPF中的对象资源
在WPF中,所有继承自FrameworkElement的元素都包含一个Resources属性,这个属性就是我们这篇要讲的资源. 这一篇讲解的资源是不是上一篇的程序集资源(那个是在编译过程中打包到程序集 ...
随机推荐
- 客户端浏览器判断(ios .android)
在开发工程中,我们可能需要判断客户端浏览器的版本而作相应的处理:通常做法是通过浏览器的userAgent去判断浏览器版本,故在此总结下,方便以后使用. <script type="te ...
- C-Free 您不能使用调试解决方案
什么时候C-Free 当您调试 找不到gdb.exe解决方案 http://www.programarts.com/ C-Free 官方网站 下载Mingw或者其他编译器 版权声明:本文博主原创文章. ...
- dev gridcontrol 单箱效果
private void gridView1_CellValueChanging(object sender, DevExpress.XtraGrid.Views.Base.CellValueChan ...
- ASP.NET MVC 学习之路-3
本文在于巩固基础 到这里不得不说ASP.NET MVC一个规则:惯例优先原则 ASP.NET会假定开发人员遵循特定的规则来构建自己的程序而不是使用配置文件 ASP.NET MVC文件夹结构也遵循惯例优 ...
- html系列教程--input label
<input> 标签:用于提交用户输入数据的文本框. input属性: 1.checked:用于checkbox,radio等元素,确定是否选中,true/false 2.disabled ...
- EBS-利用form个性化 调用报表【Z】
1.在工具中添加调用报表的功能 条件: 触发器事件:WHEN-NEW-FORM-INSTANCE 活动: 类型为:菜单 菜单项:specialn n为1..6 菜单标签:打印xx报表 2.对speci ...
- 项目中经常用到的reset.css文件
html,body{width:100%; height: auto;} *{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box- ...
- LintCode (9)Fizz Buzz
下面是AC代码,C++风格: class Solution { public: vector<string> fizzBuzz(int N) { vector<string> ...
- 已经包含了#include <atlcom.h> #include <comutil.h>还是报错
在WTL工程的.h中 #include <atlbase.h>#include <atlcom.h>#include <atlcomcli.h>#include & ...
- Qt 之 show,hide,setVisible,setHidden,close 等小结
0QObject::deleteLater()delete obj;析构对象1QWidget::setVisible(bool)使得Widget可见或不可见2QWidget::setHidden(bo ...