从JavaScript访问C#列表

问题描述 投票:6回答:2

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

的index.html

          <select id="wallsSelect"></select>

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++;
    }

最后是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;

}

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

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

javascript c# html unity-webgl
2个回答
0
投票

我认为你应该尝试使用你可以使用C#修改的html中的隐藏字段。然后,您可以使用JavaScript访问该数据。例如:

HTML:

<input type="hidden" value="currentValue" id="hiddenField1">

C#:

private void changeHiddenField(){

    hiddenField1.Value = "differentValue";

}

JS:

var hiddenFieldVal = document.getElementById("hiddenField1").value;

有关HTML https://www.w3schools.com/tags/att_input_type_hidden.asp中隐藏字段的更多信息

请原谅任何语法错误。


0
投票

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

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

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 -> https://stackoverflow.com/a/36244111/11013226

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

.jslib文件

mergeInto (LibraryManager.library, {

    UpdateModel : function(model){
        model = Pointer_stringify(model);
        updateModel(model);
    },
//rest of code
});

最后我可以在我的网页中使用我的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);
    }
}

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

© www.soinside.com 2019 - 2024. All rights reserved.