首页 技术 正文
技术 2022年11月17日
0 收藏 660 点赞 3,291 浏览 3928 个字

Unity3D嵌入WPF教程

  1. 创建一个 类库工程

Unity3D嵌入WPF教程Unity3D嵌入WPF教程

  1. 添加 WindowForm 用户控件 (UserControl)

Unity3D嵌入WPF教程

1).引入 UntiyWebPlayer COM 组件

在工具-》选择工具箱中找到UnityWebPlayer.dll并添加;

Unity3D嵌入WPF教程

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

Unity3D嵌入WPF教程

Unity3D嵌入WPF教程

  2).将 这个组件拖到 UserControl 里, 并将 Dock属性设置为 Fill 让它充满整个控件

Unity3D嵌入WPF教程

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

Unity3D嵌入WPF教程

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.

Unity3D嵌入WPF教程

Unity3D嵌入WPF教程

Unity3D嵌入WPF教程

使用

  1. 在 WPF 的XAML的 Window 标签中 引入我们的 自定义控件的名称空间. 如: xmlns:unity=”…”  在 <Grid> 中, 加入一个 <WindowsFormHost> 标签,用来承载我们的 WIndowsForm 的自定义组件. 并在其中 加入 如: <unity:UnityPlayer x:Name=”UnityPlayer”>. 这样, 就将UnityWebPlayer 嵌入了 WPF中.

Unity3D嵌入WPF教程

Unity3D嵌入WPF教程

出现问题可能是组件没有加载上去,

Unity3D嵌入WPF教程

Unity3D嵌入WPF教程

Unity3D嵌入WPF教程

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,088
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,565
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,413
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,186
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,822
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,905