从unity webGL向Vue js发送消息

问题描述 投票:1回答:1

这是一个更普遍的问题!我创建了一个vue js网页,我想在其中添加一个统一的webgl。从javascript调用unityfunction似乎很容易。但是我找不到合适的解决方案将数据从统一webgl发送到vue js。他们在手册中建议创建类似javascript库的内容,但我认为在我的上下文中这没有意义:here

有人知道如何管理它,或者甚至有可能吗?在此先感谢:)

unity3d vue.js communication unity-webgl
1个回答
0
投票

由于您能够将数据从vue发送到unity,因此将数据从unity发送到javascript也很容易。

  1. 创建一个对象来表示我们要发送给JS的数据。

    public class Person
    {
        public string Name{get;set;}
        public string PhoneNumber{get;set;}
    }
    
  2. 创建您的UnityJavascipt.jslib文件并将其放在插件中文件夹。

  3. 我们将从名为SendToJavscript的简单javascript桥接函数开始

    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 );
    
  4. 创建extern PInvoke C#函数,通过调用它,它调用Javascipt桥函数,此extern函数必须在名称,参数计数,参数类型,参数顺序上与Javascript SendToJavscript桥函数匹配。

  5. public static extern void SendToJavscript(string jsonData);

  6. 现在将人对象发送到javascript。

  7. 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));
    }
    
© www.soinside.com 2019 - 2024. All rights reserved.