从JavaScript访问C#列表

JDe*_*Den 6 html javascript c# unity-webgl

作为我们实习的一部分,我们的任务是使用Unity WebGL创建业务应用程序.在阅读了JS和C#代码之间的交互之后,我们就在一个角落里.
我们试图从C#中取回一个列表来填充我们网页上的选择,我们只是不知道该怎么做.我们遵循了Unity文档,可以轻松地与我们的网页上的C#进行通信并获取数据.
但是,我们无法在浏览器中访问C#数据.SendMessage()方法不允许返回.
到目前为止,这是我们的代码

的index.html

          <select id="wallsSelect"></select>
Run Code Online (Sandbox Code Playgroud)

jsfile

    function getWallsList() {
    //function is called at the creation of our object
    var wallsSelect = document.getElementById("wallsSelect");
    index = 0;
    var wallsList = gameInstance.SendMessage('WallCreator', 'GetGameObjects'); //would like to get back our list of walls and populate our Select with it

    for (item in wallsList) {
        var newOption = document.createElement("option");
        newOption.value = index;
        newOption.innerHTML = item;

        wallsSelect.appendChild(newOption);
        index++;
    }
Run Code Online (Sandbox Code Playgroud)

最后是C#代码

public List<string> GetGameObjects()
{
    List<string> goNames = new List<string>();
    foreach (var item in goList)
    {
        goNames.Add(item.name);
    }
    Debug.Log("Accessed GetGameObjects method. GameObject count = " + goNames.Count.ToString()); //The object is instanciated and return the right count number so it does work without a problem
    return goNames;

}
Run Code Online (Sandbox Code Playgroud)

是的,我们确实检查了https://docs.unity3d.com/Manual/webgl-interactingwithbrowserscripting.html,我做了大量的研究,发现了一些有趣的资源,我无法将我的头脑包裹得太缺乏经验,例如http: //tips.hecomi.com/entry/2014/12/08/002719

总而言之,我想指出这是我们的第一个"真实世界"项目,而Unity-WebGL在看到缺少文档时非常有经验.

JDe*_*Den 1

好的,在广泛阅读 Unity 文档并获得我们技术主管的一些帮助后,我获得了一个“足够好”的解决方案。

Unity 为您提供了一种从 C# 代码调用 JS 函数的方法,以便与 Unity 模块所在的 HTML 页面进行通信。我必须创建一个可序列化的“虚拟”类,并且只存储对象的名称和坐标。

C# 代码

 //We create a class with the Serializable attribute and stock the name and size of our GameObject 

[Serializable]
    public class SzModel
    {
        public string modelName;
        public Vector3 modelSize;
    }

//we have to import our .jslib method into our C# (see below)
    [DllImport("__Internal")]
    private static extern void UpdateModel(string model);

//We use our dummy class to create a JSON parseable list of those objects
    void WallsList()
    {
        List<SzModel> szModelList = new List<SzModel>();
        foreach (var item in goList)
        {
            SzModel newWall = new SzModel();
            newWall.modelName = item.Name;
            newWall.modelSize = item.Size;
            szModelList.Add(newWall);
        }

        UpdateModel(JsonHelper.ToJson<SzModel>(szModelList.ToArray(), true));
    }

//We create an helper class to be able to use JsonUtility on list
//code can be found here -> /sf/answers/2537087801/
Run Code Online (Sandbox Code Playgroud)

之后,我们需要通知 HTML 页面新对象,我们使用 UpdateModel() 方法来做到这一点。Unity 使用 .jslib 文件在 C#(在构建时转换为 JS 代码)和我们的浏览器之间进行通信。所以我们可以在这个 .jslib 文件中声明一个函数。这些文件驻留在资源/插件中,并在构建时自动转换。正如您在下面看到的,我们必须使用 Pointer_stringify 方法来获取 json 数据,而不仅仅是指向它的指针。

.jslib 文件

mergeInto (LibraryManager.library, {

    UpdateModel : function(model){
        model = Pointer_stringify(model);
        updateModel(model);
    },
//rest of code
});
Run Code Online (Sandbox Code Playgroud)

最后,我可以在网页中使用 json 数据,在本例中显示墙名称列表。

function updateModel(model) {
    var jsonWallsList = JSON.parse(model);
    var wallsList = document.getElementById("wallsSelect"),
        option,
        i = jsonWallsList.Items.length - 1,
        length = jsonWallsList.Items.length;

    for (; i < length; i++) {
        option = document.createElement('option');
        option.setAttribute('value', jsonWallsList.Items.modelName);
        option.appendChild(document.createTextNode(jsonWallsList.Items[i]['modelName']));
        wallsList.appendChild(option);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的网页上的选择中给出以下内容 Unity-webgl 中 C# 的选择