使用JavaScript和/或C#(Unity3D)从PHP脚本加载一些变量 [英] Load some variables from PHP Script using JavaScript and/or C# (Unity3D)
问题描述
我在Unity Game Engine中有困难的项目,我需要创建从服务器上的PHP文件加载变量的脚本。我的PHP脚本是:
I have difficult project in Unity Game Engine, i need to create script that load variables from PHP file on server. My PHP script is:
<?php
$MasterServer = "SomeIP1";
$MasterServer2 = "SomeIP2";
$MasterServer3 = "SomeIP3";
echo "1 Server = ".$MasterServer;
echo "2 Server = ".$MasterServer2;
echo "3 Server = ".$MasterServer3;
?>
结果:
1 Server = SomeIP1
2 Server = SomeIP2
3 Server = SomeIP3
如何阅读这些(作为变量)在C#和/或JS(Unity3D)?
How to read these (as variables) in C# and/or JS (Unity3D)?
推荐答案
不要这样做。这是你问题的恰当答案。使用xml或json来保存数据。
Don't do that. That is the appropriate answer to your question. Use xml or json to hold the data.
在php端,将所有这些数据构造成一个json字符串。在C#端,使用 WWW
类然后反序列化 json成对象。然后,您可以从中访问1个服务器,2个服务器和3个服务器数据。
On the php side, construct all those data into one json string. On the C# side, receive it with the WWW
class then deserialize the json into object. You can then access 1 Server, 2 Server and 3 Server data from it.
PHP :
发送给Unity:
{"server1":"SomeIP1","server2":"SomeIP2","server3":"SomeIP3"}
你必须用IP替换SomeIP1,SomeIP2和SomeIP3 。
You have to replace the SomeIP1,SomeIP2 and SomeIP3 with the IPs.
C#:
[Serializable]
public class ServerInfo
{
public string server1;
public string server2;
public string server3;
}
....
IEnumerator downLoadFromServer()
{
WWW www = new WWW("yourUrl");
yield return www;
string json = www.text;
ServerInfo serverInfo = JsonUtility.FromJson<ServerInfo>(json);
Debug.Log("server1: " + serverInfo.server1);
Debug.Log("server2: " + serverInfo.server2);
Debug.Log("server3: " + serverInfo.server3);
}
我建议你花时间学习json的工作原理。
I suggest you spend time and learn how json works.
这篇关于使用JavaScript和/或C#(Unity3D)从PHP脚本加载一些变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!