環(huán)境:
- VS2015
- Unity 5.3.6f1 (64-bit)
目的:
Unity官方提供的UnityWebPlayer控件在嵌入Winform時(shí)要求讀取的.unity3d文件路徑(Src)必須是絕對(duì)路徑,如果移動(dòng)代碼到另一臺(tái)電腦,需要重新修改src。于是考慮使用winform用戶控件來進(jìn)行封裝,以實(shí)現(xiàn)讀取存放在工程文件夾下的.unity3d文件的效果。
參考:
-
WinForm內(nèi)嵌Unity3D_winform嵌入unity3d并交互__學(xué)而時(shí)習(xí)之_的博客-CSDN博客
-
https://www.cnblogs.com/xieyulin/p/7050618.html
過程
首先新建windows用戶窗體庫。
然后在空白控件上添加UnityWebPlayer,這一步是為了添加Interop.UnityWebPlayerAXLib.dll、AxInterop.UnityWebPlayerAXLib.dll兩個(gè)引用。
當(dāng)引用列表里出現(xiàn)這兩個(gè)引用時(shí),把剛才添加的UnityWebPlayer控件刪除。
然后修改控件的.cs文件,代碼如下
//U3DPlayer.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace UnityPlayer
{
public partial class U3Dplayer: UserControl
{
#region 自定義事件
//委托
public delegate void ExternalCallHandler(object sender, AxUnityWebPlayerAXLib._DUnityWebPlayerAXEvents_OnExternalCallEvent e);
/// <summary>
/// 接收Unity調(diào)用宿主函數(shù)的消息
/// </summary>
[Browsable(true), Description("接收Unity調(diào)用宿主(如WinForm)函數(shù)的消息")]
public event ExternalCallHandler UnityCall;
//方法
public void OnUnityCall(object sender, AxUnityWebPlayerAXLib._DUnityWebPlayerAXEvents_OnExternalCallEvent e)
{
if (UnityCall != null)
{
UnityCall(sender, e);
}
}
#endregion
#region 內(nèi)部變量
private AxUnityWebPlayerAXLib.AxUnityWebPlayer _axUnityWebPlayer = null;
#endregion
/// <summary>
/// 用戶控件函數(shù)
/// </summary>
public U3Dplayer()
{
InitializeComponent();
string src = Application.StartupPath + "\\unity.unity3d";//在這里設(shè)置路徑
_axUnityWebPlayer = CreateUnity(this, src);
OnExternalCallCheck();
this.Controls.Add(_axUnityWebPlayer);//在原控件上添加UnityWebPlayer控件
}
/// <summary>
/// 建立一個(gè)新的unity控件并設(shè)置一些屬性
/// </summary>
/// <param name="form"></param>
/// <param name="src"></param>
/// <returns></returns>
public static AxUnityWebPlayerAXLib.AxUnityWebPlayer CreateUnity(Control form, string src)
{
var unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer();
((System.ComponentModel.ISupportInitialize)(unity)).BeginInit();
form.Controls.Add(unity);
((System.ComponentModel.ISupportInitialize)(unity)).EndInit();
unity.src = src;
AxHost.State state = unity.OcxState;//最關(guān)鍵的是OcxState,必須使用AxUnityWebPlayer才能依據(jù)Src動(dòng)態(tài)產(chǎn)生。
unity.Dispose();
unity = new AxUnityWebPlayerAXLib.AxUnityWebPlayer();
((System.ComponentModel.ISupportInitialize)(unity)).BeginInit();
form.SuspendLayout();
unity.Dock = DockStyle.Fill;
//unity.Size = new System.Drawing.Size(720, 450);
unity.Name = "Unity";
unity.OcxState = state;
unity.TabIndex = 0;
//unity.Location = new System.Drawing.Point(0, 0);
((System.ComponentModel.ISupportInitialize)(unity)).EndInit();
form.ResumeLayout(false);
return unity;
}
private void OnExternalCallCheck()
{
if (_axUnityWebPlayer == null)
{
throw new Exception("_axUnityWebPlayer init fail");
}
else
{
_axUnityWebPlayer.OnExternalCall += _axUnityWebPlayer_OnExternalCall;
}
}
void _axUnityWebPlayer_OnExternalCall(object sender, AxUnityWebPlayerAXLib._DUnityWebPlayerAXEvents_OnExternalCallEvent e)
{
if (e.value.StartsWith("LOAD_COMPLETE"))
{
if (!_axUnityWebPlayer.Visible)
{
_axUnityWebPlayer.Width = this.Width;
_axUnityWebPlayer.Height = this.Height;
_axUnityWebPlayer.Show();
}
}
OnUnityCall(sender, e);
}
#region SendMessage
/// <summary>
/// 發(fā)送消息給Unity
/// </summary>
/// <param name="unityObjName">Unity中的對(duì)象名稱</param>
/// <param name="unityScriptyMethod">Unity腳本中的方法</param>
/// <param name="val">傳送的值.僅限于int、float、string</param>
public void SendMessage(string unityObjName, string unityScriptyMethod, object val)
{
if (_axUnityWebPlayer == null)
{
return;
}
_axUnityWebPlayer.SendMessage(unityObjName, unityScriptyMethod, val);
}
#endregion
}
}
然后選擇生成動(dòng)態(tài)鏈接庫。
在Winform工程中選擇“工具”–“選擇工具箱項(xiàng)”–導(dǎo)入,即可使用
文章來源:http://www.zghlxwxcb.cn/news/detail-657797.html
轉(zhuǎn) :Winform基于UnityWebplayer封裝用戶控件以實(shí)現(xiàn).unity3d文件相對(duì)路徑(動(dòng)態(tài)src) - 灰信網(wǎng)(軟件開發(fā)博客聚合)文章來源地址http://www.zghlxwxcb.cn/news/detail-657797.html
到了這里,關(guān)于winform 封裝unity web player 用戶控件的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!