zoukankan      html  css  js  c++  java
  • Unity3D嵌入WPF教程

    Unity3D嵌入WPF教程

    1. 创建一个 类库工程

     

    1. 添加 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.

     

    使用 

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

     

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

  • 相关阅读:
    【Leetcode】23. Merge k Sorted Lists
    【Leetcode】109. Convert Sorted List to Binary Search Tree
    【Leetcode】142.Linked List Cycle II
    【Leetcode】143. Reorder List
    【Leetcode】147. Insertion Sort List
    【Leetcode】86. Partition List
    jenkins 配置安全邮件
    python 发送安全邮件
    phpstorm 同步远程服务器代码
    phpUnit 断言
  • 原文地址:https://www.cnblogs.com/wangboke/p/5329483.html
Copyright © 2011-2022 走看看