我真正想问的是,如果有不可能编译成统一构建的依赖关系,是否还有一种方法可以从统一内部调用它们,简单地使用从网站加载到浏览器中的脚本并与它们通信呢?
相关文档并未深入解决这一问题:https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html
我正在创建一个统一应用程序的网站包装。体验按钮位于网站内,因为按钮会影响网站的其他部分,而不仅仅是统一应用程序。
但是,当某些内容被加载到统一游戏中时,应用程序需要能够影响网站。有没有办法将数据以创造性的方式传回网站?目前,我在统一编译中包含了网站的所有javascript代码,它正在错误地运行:
gameInstance = UnityLoader.instantiate("gameContainer", "/Build/DomumProto16_Web.json", {
onProgress: UnityProgress
}); 从网站向游戏发送数据:
gameInstance.SendMessage('Manager','Filter', JSON.stringify(filterActive));需要从统一游戏中调用此功能。但是,由于ajax.ajax_url是在后端使用wp_localize_script()本地化的,所以没有定义它。
function showStudentWork(studentIndex){
//make sure to remove all the
var x = document.getElementById("studentWork");
var studentID = studentWork[studentIndex];
console.log(studentID);
$.ajax({
url: ajax.ajax_url,
type: "POST",
data: {
action: 'getStudentPost',
currentStudent: studentID
},
success: function (data) {
x.innerHTML = "";
x.innerHTML = data;
x.style.display = "grid";
},
error: function (error) {
console.log(`Error ${error}`);
}
});
return false;
}我真正想问的是,如果有不可能编译成统一构建的依赖关系,是否还有一种方法可以从统一内部调用它们,简单地使用从网站加载到浏览器中的脚本并与它们通信呢?
发布于 2020-11-02 03:53:00
这里有两种方法。一个是,在我看来,更容易,但它是不可取的,你应该不要使用它。选项二是'corrrect‘的方式,但这是有点丑陋的海事组织。
备选案文1: Application.ExternalCall
这个选项允许您直接调用浏览器javascript,但是Unity已经放弃了对它的支持,对于任何长期而言,这可能是个坏主意。
在具有Unity工作的给定浏览器中,请考虑以下几点:在浏览器源中,定义一个javascript函数
<other html>
<script>
function foo(msg) {
alert(msg);
}
</script>在“团结”中,无论何时议会:
Application.ExternalCall( "foo", "The game says hello!" );这允许从统一调用Javascript。在相反的方向上,通信也有类似的功能。
备选案文2: jslibs
这是团结一致的做事方式。它涉及到将javascript库打包到游戏中。
首先,创建一个与游戏一起打包的javascript文件。下面是一个示例文件:
// Unity syntactic sugar
mergeInto(LibraryManager.library, {
// Declare all your functions that can be called from c# here
alert_user: function (msg) {
window.alert(msg);
},
other_func: function () {
// does something else
// etc.
// put as many functions as you'd like in here
}
}将该文件放在项目的.jslib文件夹中,扩展名为Plugins。
然后,在c#文件中,您需要:
// Declare a monobehavior, whatever
using UnityEngine;
using System.Runtime.InteropServices;
public class NewBehaviourScript : MonoBehaviour {
// IMPORTANT
// You have to declare the javascript functions you'll be calling
// as private external function in your c# file. Additionally,
// They need a special function decorator [DllImport("__Internal")]
// Example:
[DllImport("__Internal")]
private static extern void alert_user(string msg);
// With that unpleasantness over with, you can now call the function
void Start() {
alert_user("Hello, I am a bad person and I deserve to write c#");
}
}中提琴。您可以从您的c# javascript中调用其他javascript,并与dom进行交互,但我将把所有这些决定留给您。
另一个方向
在这两种情况下,相反方向的通信(浏览器对统一说了什么)都是相同的格式。
在javascript中,创建一个UnityInstance (这是一个小的两条长缠绕的方法,可以放入这个答案,检查任何一个文档)。然后,只有.sendMessage。
例如: c#
...
void DoSomething (string msg) {
// this is a c# function that does something in the game
// use your imagination
}
...javascript:
let game = UnityLoader // Actually load the game here
game.SendMessage('TheNameOfMyGameObject', 'DoSomething', 'This is my message');发布于 2020-11-02 18:25:21
如果我理解的话,可以使用WWW调用函数来完成
这不是正确的代码好吗。这将只是你的一个想法。
向PHP函数发送一个变量,如下所示
string url = "" // add your URL here;
WWWForm form = new WWWForm ();
form.AddField("YourFunctionName", "SampleFunction");
WWW www = new WWW(url, form);
yield return www;现在在PHP中这样做
function YourFunctionName()
{
// your function code here
}而现在
$YourFunctionName = $_POST["functionName"];
switch ($functionName){
case "SampleFunction":
SampleFunction();
break;
}所以这里的想法仍然是,您仍然需要一个PHP,从这个PHP调用ajax :)
https://stackoverflow.com/questions/64533232
复制相似问题