这是一个更一般的问题!我创建了一个vue js网页,想要添加一个unity webgl。似乎很容易从javascript调用unityfunction。但是我找不到合适的解决方案来将数据从我的unity webgl发送到vue js。
在手册中,他们推荐创建类似于javascript库的东西,但我认为在我的情况下这没有意义:这里。
有人有什么想法如何管理或是否可能实现吗? 提前感谢:)
有人有什么想法如何管理或是否可能实现吗? 提前感谢:)
既然你能够从Vue向Unity发送数据,那么从Unity向JavaScript发送数据也很容易。
Create an object to represent data we want to send to JS.
public class Person
{
public string Name{get;set;}
public string PhoneNumber{get;set;}
}
Create your UnityJavascipt.jslib
file and place it in a plugins
folder.
We'll start with a simple javascript bridge function called SendToJavscript
var UnityJavascipt =
{
// This object can hold variables for you.
$JustAWebGLObject:
{
},
SendToJavscript: function (dataJsonPtr)
{
// string paramters from unity get delivered to javascript as pointers.
// So we get the actual string from the pointer.
var dataJson = Pointer_stringify(dataJsonPtr);
// Now convert the string to a javascript object.
var jsobject = JSON.parse(dataJson);
// Now you have the jsObject, Vue can access it from here.
// You can use Vue API from here too.
// I'll just debug some variables.
console.log(jsobject.Name); // Kevin
console.log(jsobject.PhoneNumber); // 011244455
},
};
autoAddDeps(UnityJavascipt , '$JustAWebGLObject');
mergeInto(LibraryManager.library, UnityJavascipt );
Create the extern PInvoke C# function, which by calling it, it calls the Javascipt bridge function, this extern function must match the Javascript SendToJavscript
bridge function in name, parameters count, parameters types, parameters order.
public static extern void SendToJavscript(string jsonData);
Now sending a person object to javascript.
private void SendToJavscript_Test()
{
Person person = new Person();
person.Name = "Kevin";
person.PhoneNumber = "011244455";
// We need to convert the object to javascript, cause we can't send objects
directly between C# and javascript.
// Our bridge function handles converting the object back to a javascript
object.
SendToJavscript(JsonUtility.ToJson(person));
}
只需将包含您的C#函数和插件的代码整个文件夹放置在已安装为包的vue-unity-webgl文件夹下,如果您按照https://github.com/votetake/vue-unity-webgl的方法进行操作。