实现winform内嵌unity

源码和成品下载地址:https://download.csdn.net/download/xiaochenxihua/10674624 

效果如下:

一、需要下载UnityWebPlayer插件进行安装

下载地址: https://unity3d.com/cn/webplayer/

如下图所示:

二、新建一个winform项目,然后给工具箱添加UnityWebPlayer组件,如下图所示:

三、在Winform中添加UnityWebPlayer control组件,进行基础的布局配置工作,如下图所示

 

然后双击中间的 UnityWebPlayer control组件,进入代码编辑区域获取Unity传来的值

 四、创建一个简单的Unity控制Cube控制程序项目(注意Unity版本需要为5.3.0以下版本才能打包Unity项目为WebPlayer),编写一个控制Cube旋转的脚本,然后添加给Cube进行打包,如下图所示:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class Test_SelfRotate : MonoBehaviour {

    public float rotateSpeed = 15.0f;
    public bool isRotate = false;
    public Text text;


    private void Update()
    {
        if (isRotate)
        {
            this.transform.Rotate(Vector3.up * rotateSpeed * Time.deltaTime);
        }
       
    }


    /// <summary>
    /// unity发送信息给Winform窗体
    /// </summary>
    public void SendMessageToWPF()
    {
        Application.ExternalCall("文本测试","这是Unity发来的文本内容");
    }

    //开始旋转物体
    public void StartRotateGameObject()
    {
        isRotate = true;
    }

    //停止旋转物体
    public void PauseRotateGameObject()
    {
        isRotate = false;
    }

    //Unity接收WPF的信息
    public void GetWPFSendMessage(float speed)
    {
        this.isRotate = true;
        this.rotateSpeed = speed;
        text.text ="接收到的旋转速度为:"+ speed.ToString();
    }
}

 

 

五、转到创建好的winform项目中,给窗体上的UnityWebPlayercontrol组件属性的Src指定对应的地址:我这里为:D:\AllProjects\Test\Test_SelfRotate\Web\Web.unity3d(依照个人打包的Unity项目位置改变),然后编写对应的功能如下图所示:

using System;
using System.Windows.Forms;

namespace Test_WFM_Unity
{
    public partial class Form1 : Form
    {
        string inputSpeed ;//输入框的速度
        double speed;//真实的速度


        public Form1()
        {
            InitializeComponent();
        }

        //接收Unity发来的信息
        private void axUnityWebPlayer1_OnExternalCall(object sender, AxUnityWebPlayerAXLib._DUnityWebPlayerAXEvents_OnExternalCallEvent e)
        {

            label1.Text = e.value;

            
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 发送信息给Unity
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                inputSpeed = textBox1.Text.Trim();
                speed = Convert.ToDouble(inputSpeed);
                if (!string.IsNullOrEmpty(inputSpeed))
                {
                    if (speed > 0)
                    {
                        if (checkBox1.Checked == true)
                        {
                            checkBox1.Text = "开启旋转";
                            axUnityWebPlayer1.SendMessage("Cube", "GetWPFSendMessage", Math.Abs(speed));
                        }
                        else
                        {
                            checkBox1.Text = "关闭旋转";
                            MessageBox.Show("请检查是是否开启旋转", "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
                        }

                    }
                    else
                    {
                        MessageBox.Show("请检查是输入的速度是否大于0","错误提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
                    }
                }

              
            }
            catch (Exception)
            {

                MessageBox.Show("请检查是否输入对应大于0的速度值", "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
           
            //axUnityWebPlayer1.SendMessage("Cube", "GetWPFSendMessage",true);
        }

        /// <summary>
        /// 是否开启旋转
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            //选中则开启旋转
            if (!string.IsNullOrEmpty(inputSpeed))
            {
                if (checkBox1.Checked == true)
                {
                    checkBox1.Text = "开启旋转";
                    axUnityWebPlayer1.SendMessage("Cube", "StartRotateGameObject", null);
                    axUnityWebPlayer1.SendMessage("Cube", "GetWPFSendMessage", Math.Abs(speed));
                }
                else
                {
                    checkBox1.Text = "关闭旋转";
                    axUnityWebPlayer1.SendMessage("Cube", "PauseRotateGameObject", null);
                }
            }
            else
            {
                MessageBox.Show("请检查是否输入对应大于0的速度值", "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            }
            //取消选中则关闭旋转
            if (checkBox1.Checked == false)
            {
                axUnityWebPlayer1.SendMessage("Cube", "PauseRotateGameObject", null);
            }
        }




    }
}

注意: axUnityWebPlayer1.SendMessage("Cube", "PauseRotateGameObject", null);方法中,第一个参数是Unity项目中脚本所挂载的物体对象,第二个参数是需要执行的Unity脚本控制方法,第三个是方法中的参数。

 参考:https://blog.csdn.net/lj34207310/article/details/56668057

          https://blog.csdn.net/xxdddail/article/details/49890643

猜你喜欢

转载自blog.csdn.net/xiaochenXIHUA/article/details/82760723
今日推荐