1

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;
?>

And the result of this:

 1 Server = SomeIP1
 2 Server = SomeIP2
 3 Server = SomeIP3

How to read these (as variables) in C# and/or JS (Unity3D)?

Marek Grzyb
  • 177
  • 13
  • `responseFromServer.Split('\n').Select(v => new KeyValuePair(v.Split('=')[0], v.Spit('=')[1]));` will result with `{ "1 Server ", " SomeIP1" }, { "2 Server ", " SomeIP2" }...` – mrogal.ski Feb 16 '17 at 13:28

1 Answers1

3

Don't do that. That is the appropriate answer to your question. Use xml or json to hold the data.

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:

Send to Unity:

{"server1":"SomeIP1","server2":"SomeIP2","server3":"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);
}

I suggest you spend time and learn how json works.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328