1. 程式人生 > >實現winform內嵌unity

實現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