1、使用插件 Embedded Browser(3.1.0)
(1)使用方法
将预制体Browser (GUI)
挂载到Canvas
下面,在Url
中输入网址即可。

(2)监听前端网页的方法。在Unity中监听前端代码中同名方法Fun1()
、Fun2()
void Start()
{
Init();
}
//监听前端的方法
void Init()
{
//无参数
browser.RegisterFunction("Fun1", args =>
{
UnityFun1(); //Unity中的方法
});
//带参数
browser.RegisterFunction("Fun2", (JSONNode jk) =>
{
UnityFun2(jk[0].Value.ToString());
});
}
void UnityFun1()
{
Debug.Log("调用了方法1");
}
void UnityFun2(string str)
{
Debug.Log("调用了方法2" + str);
}
(3)Unity调用前端,给前端发消息。
browser.CallFunction("Fun3", str).Done();//Fun3为前端同名方法,str为参数
(4)注意事项
在客户端中嵌入的前端网页会遮挡整个UI面板,影响后面的物体点击,如果需要点击后面的物体需要将RawImage
中的Raycast Target
取消勾选。
然后判断鼠标位置,如果在前端网页的位置上时,将RawImage
中的Raycast Target
勾选上。(若不勾选则点击不了网页上的按钮等控件)。
Vector3 v3
void Update()
{
v3 = Input.mousePosition;
if (v3.x <= 480f && v3.y <= 1000f)
{
browser.GetComponent<RawImage>().raycastTarget = true;
}
else
{
browser.GetComponent<RawImage>().raycastTarget = false;
}
}
网友评论