从浏览器访问 Unity Webgl 输出,反之亦然
Access Unity Webgl output from browser and vice versa
我收到了一个 Unity webgl 包,并将其放入 Wordpress 的课程中。 index.html 生成的 Unity 表单在 iframe 中访问。这必须能够统计访问课程时的用户点击。
Unity开发者只说变量是Click 还有资料:
Application.ExternalCall("Send_Clicks", clickCount.ToString());
我阅读了 Unity 文档,我应该创建一个 js 脚本来从 Unity 检索消息。如何从 Unity 检索消息?脚本应该放在哪里?
非常感谢任何答案。谢谢。
从技术上讲,您也可以使用 jQuery。唯一的问题是,你必须确保它在 onReady 函数中。只是一个旁注。
Application.ExternalCall("Send_Clicks", clickCount.ToString());
为了进一步解释,上面的代码非常简单。 `ExternalCall 的参数是 1:方法名称 2:参数参数。
你也可以传入不同数量的参数,只要确保你有正确的重载。
这是一个基本示例。在您的 Unity 中,假设您调用了此函数。
Application.ExternalCall("MyInfo", "John", 23);
并且在您的 wordpress 页面中。确保你有一个 <script>
标签一个普通的 JS 就可以。
<script>
function MyInfo( arg1, arg2 )
{
alert("My name is :"+arg1 + " Age is : "+ arg2 );
}
</script>
就这么简单,一般在MVC中,JS会放在页面底部的某处<body>
标签。为确保它知道所有 ID 和 class 名称。祝你好运。
编辑:
Unity 现在支持 Application.ExternalCall
WebGL,因此您不再需要插件。对于那些使用旧版 Unity 的人,我将在此处保留旧答案。仍然建议使用下面的旧答案,因为在打包的 Chrome 应用程序中使用 Application.ExternalCall
时会出现 security restrictions。插件不会发生这种情况。
旧答案:
Application.ExternalCall
仅适用于 WebPlayer。 Application.ExternalCall
将 NOT 用于 WebGL。
这可以通过插件来完成。
使您的 JavaScript 代码看起来像下面的东西。然后命名为PluginName.jslib
。扩展名必须是 .jslib。保存到 Assets/Plugins/WebGL/PluginName.jslib
var MyPlugin = {
Hello: function()
{
window.alert("Hello, world!");
},
HelloString: function(str)
{
window.alert(Pointer_stringify(str));
},
};
mergeInto(LibraryManager.library, MyPlugin);
现在在 C# 中,您应该可以这样做:
using UnityEngine;
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
[DllImport("__Internal")]
private static extern void HelloString(string str);
void Start() {
Hello(); //Call Hello from JavaScript
HelloString("This is a string."); //Call HelloString from JavaScript with parameter
}
}
您必须包含 using System.Runtime.InteropServices;
才能执行此操作。
有关此的更多信息,请转至 http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html 并从“从插件中调用 JavaScript 函数”开始阅读。
我收到了一个 Unity webgl 包,并将其放入 Wordpress 的课程中。 index.html 生成的 Unity 表单在 iframe 中访问。这必须能够统计访问课程时的用户点击。
Unity开发者只说变量是Click 还有资料:
Application.ExternalCall("Send_Clicks", clickCount.ToString());
我阅读了 Unity 文档,我应该创建一个 js 脚本来从 Unity 检索消息。如何从 Unity 检索消息?脚本应该放在哪里?
非常感谢任何答案。谢谢。
从技术上讲,您也可以使用 jQuery。唯一的问题是,你必须确保它在 onReady 函数中。只是一个旁注。
Application.ExternalCall("Send_Clicks", clickCount.ToString());
为了进一步解释,上面的代码非常简单。 `ExternalCall 的参数是 1:方法名称 2:参数参数。
你也可以传入不同数量的参数,只要确保你有正确的重载。
这是一个基本示例。在您的 Unity 中,假设您调用了此函数。
Application.ExternalCall("MyInfo", "John", 23);
并且在您的 wordpress 页面中。确保你有一个 <script>
标签一个普通的 JS 就可以。
<script>
function MyInfo( arg1, arg2 )
{
alert("My name is :"+arg1 + " Age is : "+ arg2 );
}
</script>
就这么简单,一般在MVC中,JS会放在页面底部的某处<body>
标签。为确保它知道所有 ID 和 class 名称。祝你好运。
编辑:
Unity 现在支持 Application.ExternalCall
WebGL,因此您不再需要插件。对于那些使用旧版 Unity 的人,我将在此处保留旧答案。仍然建议使用下面的旧答案,因为在打包的 Chrome 应用程序中使用 Application.ExternalCall
时会出现 security restrictions。插件不会发生这种情况。
旧答案:
Application.ExternalCall
仅适用于 WebPlayer。 Application.ExternalCall
将 NOT 用于 WebGL。
这可以通过插件来完成。
使您的 JavaScript 代码看起来像下面的东西。然后命名为PluginName.jslib
。扩展名必须是 .jslib。保存到 Assets/Plugins/WebGL/PluginName.jslib
var MyPlugin = {
Hello: function()
{
window.alert("Hello, world!");
},
HelloString: function(str)
{
window.alert(Pointer_stringify(str));
},
};
mergeInto(LibraryManager.library, MyPlugin);
现在在 C# 中,您应该可以这样做:
using UnityEngine;
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
[DllImport("__Internal")]
private static extern void Hello();
[DllImport("__Internal")]
private static extern void HelloString(string str);
void Start() {
Hello(); //Call Hello from JavaScript
HelloString("This is a string."); //Call HelloString from JavaScript with parameter
}
}
您必须包含 using System.Runtime.InteropServices;
才能执行此操作。
有关此的更多信息,请转至 http://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html 并从“从插件中调用 JavaScript 函数”开始阅读。