【发布时间】:2023-04-08 14:05:01
【问题描述】:
作为实习的一部分,我们的任务是使用 Unity WebGL 创建一个业务应用程序。在阅读了 JS 和 C# 代码之间的交互之后,我们陷入了困境。
我们正在尝试从 C# 中取回一个列表以填充我们网页上的 Select,但我们只是不知道该怎么做。我们遵循 Unity 文档,可以轻松地从我们的网页进行交流和取回数据以在我们的 C# 中使用。
但是,我们无法在浏览器中访问 C# 数据。 SendMessage() 方法不允许返回。
到目前为止,这是我们的代码
index.html
<select id="wallsSelect"></select>
js文件
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