UnityWebCore 插件是实现将一个网页的内容呈现在一个Mesh Render中,并且允许鼠标键盘与网页内容交互.
使用第一步:
配置好UnityWebCore的环境:
如下图,将dll文件添加到插件文件夹Plugins下(没有就新建一个):
第二步:
在场景中添加的一个panel与灯光panel是用来显示网页内容的容器,我这就把它命名为browserView.
第三步:
将官方给的脚本复制过来
编写脚本 UnityWebView.cs:
using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
public class UnityWebView : MonoBehaviour
{
public int width, height;
private GUITexture gui;
privateTexture2D m_ texture;
publicint m_TextureID;
private Color[] m_pixels = null;
private GCHandle m_pixelsHandler;
publicbool m_bForceOpenGL = true;
public bool m_bInitialized = false;
privateUnityWebViewEvents browserEventHandler;
privateUnityWebCore.BeginNavigationDelFunc beginNavigationDelFunc;
privateUnityWebCore.BeginLoadingDelFunc beginLoadingDelFunc;
privateUnityWebCore.FinishLoadingDelFunc finishLoadingDelFunc;
privateUnityWebCore.ReceiveTitleDelFunc receiveTitleDelFunc;
privateUnityWebCore.ChangeTooltipDelFunc changeTooltipDelFunc;
privateUnityWebCore.ChangeTargetURLDelFunc changeTargetURLDelFunc;
privateUnityWebCore.ChangeCursorDelFunc changeCursorDelFunc;
privateUnityWebCore.JSCallbackDelFunc showControlWindowFunc;
publicstring url ="http://www.baidu.com";
// Use this for initialization
void Start()
{
browserEventHandler = GetComponent();
Init(800, 600); //这里初始化网页窗口的大小(类似于浏览器的大小)它会因为 //meshRender的大小而进行拉伸变化
LoadURL(url); //调用这个函数开始加载网页。
}
voidOnGUI () {
if(Event.current.keyCode == KeyCode.Return){
this.enabled= false;
//ithappens right after the component is disabled
}
}
voidOnApplicationQuit()
{
UnityWebCore.DestroyView(m_TextureID);
}
public void Init(int width, int height)
{
this.width = width;
this.height = height;
m_texture = new Texture2D(width, height, TextureFormat.ARGB32, false);
m_TextureID= m_texture.GetNativeTextureID();
if(m_bForceOpenGL&& !Application.isEditor)
{
UnityWebCore.CreateView(m_TextureID,new System.IntPtr(0), width, height, false, 10);
}
else
{
//Get Color[] (pixels) from texture
m_pixels= m_texture.GetPixels(0);
//Create GCHandle - Allocation of m_pixels in memory.
m_pixelsHandler= GCHandle.Alloc(m_pixels, GCHandleType.Pinned);
UnityWebCore.CreateView(m_TextureID,m_pixelsHandler.AddrOfPinnedObject(), width, height, true, 10);
}
//assign m_texture to this GUITexture texture
gameObject.renderer.material.mainTexture = m_texture;
beginNavigationDelFunc= new UnityWebCore.BeginNavigationDelFunc(this.onBeginNavigationDelFunc);
beginLoadingDelFunc= new UnityWebCore.BeginLoadingDelFunc(this.onBeginLoadingDelFunc);
finishLoadingDelFunc= new UnityWebCore.FinishLoadingDelFunc(this.onFinishLoadingDelFunc);
receiveTitleDelFunc= new UnityWebCore.ReceiveTitleDelFunc(this.onReceiveTitleDelFunc);
changeTooltipDelFunc= new UnityWebCore.ChangeTooltipDelFunc(this.onChangeTooltipDelFunc);
changeTargetURLDelFunc= new UnityWebCore.ChangeTargetURLDelFunc(this.onChangeTargetURLDelFunc);
changeCursorDelFunc= new UnityWebCore.ChangeCursorDelFunc(this.onChangeCursorDelFunc);
showControlWindowFunc= new UnityWebCore.JSCallbackDelFunc(this.onShowControlWindow);
UnityWebCore.SetBeginNavigationFunc(m_TextureID,beginNavigationDelFunc);
UnityWebCore.SetBeginLoadingFunc(m_TextureID,beginLoadingDelFunc);
UnityWebCore.SetFinishLoadingFunc(m_TextureID,finishLoadingDelFunc);
UnityWebCore.SetReceiveTitleFunc(m_TextureID,receiveTitleDelFunc);
UnityWebCore.SetChangeTooltipFunc(m_TextureID,changeTooltipDelFunc);
UnityWebCore.SetChangeTargetURLFunc(m_TextureID,changeTargetURLDelFunc);
UnityWebCore.SetChangeCursorFunc(m_TextureID,changeCursorDelFunc);
UnityWebCore.AddJSCallback(m_TextureID,"showControlWindow", showControlWindowFunc); //这里实现了JS与Unity3D脚本的传值,在 //onShowControlWindow函数用来接收JS传来的 //值------------在网页的JS的脚本:
//if(typeof(UnityWebCore)!="undefine")
//{
//UnityWebCore.showControlWindow("nihao");
//}
browserEventHandler.setDimensions(width,height);
m_bInitialized = true;
browserEventHandler.interactive = true;
}
// Update is called once per frame
void Update()
{
// if ( m_bInitialized &&controlWindow.showBrowser) //修改BY-lirentai 2014/5/17
if( m_bInitialized)
{
UnityWebCore.Update();
if (UnityWebCore.IsDirty(m_TextureID))
{
m_texture.SetPixels(m_pixels,0);
m_texture.Apply();
}
}
if (browserEventHandler.focused && Input.inputString.Length >0)
{
// Send WM_CHAR message
for(int i = 0; i < Input.inputString.Length; ++i)
{
// Backspace - Remove the lastcharacter
if (Input.inputString[i] =='\b')
{
//Debug.Log("backspace");
UnityWebCore.InjectKeyboard(m_TextureID,0x0100, 0x08, 0);
UnityWebCore.InjectKeyboard(m_TextureID,0x0101, 0x08, 0);
}
elseif(Input.inputString[i] == '\r' || Input.inputString[i] == '\n')
{
//Debug.Log("enter");
UnityWebCore.InjectKeyboard(m_TextureID,0x0100, 0x0D, 0);
UnityWebCore.InjectKeyboard(m_TextureID,0x0101, 0x0D, 0);
}
else
UnityWebCore.InjectKeyboard(m_TextureID, 0x0102, Input.inputString[i],0);
}
}
intdyScroll = (int)Input.GetAxis("Mouse ScrollWheel");
if (dyScroll != 0)
{
Debug.Log("MouseScroll" + dyScroll);
UnityWebCore.MouseWheel(m_TextureID, dyScroll);
}
if(browserEventHandler.hovered)
{
if(Input.GetMouseButtonDown(2))
browserEventHandler.handleMouseDown(1);
elseif(Input.GetMouseButtonDown (1))
browserEventHandler.handleMouseDown(2);
elseif(Input.GetMouseButtonUp (2))
browserEventHandler.handleMouseUp(1);
elseif(Input.GetMouseButtonUp (1))
browserEventHandler.handleMouseUp(2);
}
}
public UnityWebViewEvents getEventHandler()
{
return browserEventHandler;
}
publicvoid setFPS(float fps)
{
object[]args = new object[1];
args[0]= fps;
UnityWebCore.CallJS(m_TextureID,"setFPS", args);
Debug.Log(fps);
}
publicvoid onShowControlWindow(string arguments)
{
Debug.Log("shoudao:"+arguments);
}
publicvoid onBeginNavigationDelFunc(string frameName, string url)
{
Debug.Log("BeginNavigationto " + url);
}
public void onBeginLoadingDelFunc(string frameName, string url, intstatusCode, string mimeType)
{
Debug.Log("BeginLoading:" + url);
}
public void onFinishLoadingDelFunc()
{
Debug.Log("FinishLoading");
}
publicvoid onReceiveTitleDelFunc(string frameName, string title)
{
Debug.Log("ReceiveTitle:" + title);
}
public void onChangeTooltipDelFunc(string tooltip)
{
}
publicvoid onChangeCursorDelFunc(int cursorID)
{
UnityWebCore.ApplyCursor(cursorID);
}
public void onChangeTargetURLDelFunc(string url)
{
}
public void Loadfile(string filePath)
{
if (m_bInitialized)
UnityWebCore.LoadFile(m_TextureID,filePath);
}
public void LoadURL(string url)
{
if (m_bInitialized)
{
UnityWebCore.LoadURL(m_TextureID, url);
Debug.Log("加载 url");
}
}
public void Destroy()
{
try
{
if (m_TextureID != 0)
{
UnityWebCore.DestroyView(m_TextureID);
if(m_pixels!= null)
m_pixelsHandler.Free();
Destroy(m_texture);
GetComponent().interactive = false;
m_TextureID = 0;
m_bInitialized = false;
}
}
catch (System.Exception e)
{
Debug.Log(e);
}
}
}
第四步:
再把写好的脚本UnityWebView.cs添加到browserView上去:
点击运行就能看到效果了:
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
官方也有个更好的例子!