Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing simple network call to retrieve JSON in Unity? [closed]

I have looked at the network documentation for Unity and most examples seem to be related to networking your game for multiplayer. I am just looking to grab a JSON response from an api for dynamically generating a menu. Is there some good examples for doing just simple network calls to get a response from a web server?

Thanks

like image 510
Bobbake4 Avatar asked Nov 21 '11 17:11

Bobbake4


2 Answers

If you are using C# and not targeting Webplayer or iOS, you can use HttpWebRequest and a library like Json.NET if you like (if you have past experience with these classes instance). You will likely need to spend a little extra time properly handling the request asychronously.

There is also Unity's WWW class where you would inspect and parse the resulting text property. The WWW class has the advantage of requiring very little code to perform the request asynchronously.

Note that without making such a request asynchronously you can easily block progress in the rest of the game loop, which is almost always not desirable.

like image 96
DuckMaestro Avatar answered Nov 06 '22 23:11

DuckMaestro


For access to a web based API it would be easiest to use the WWW class in Unity.

http://unity3d.com/support/documentation/ScriptReference/WWW.html

Make sure to check the documentation about using it asynchronously, or it can block execution while waiting for the web server to respond (or not respond).

http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

like image 40
CLo Avatar answered Nov 06 '22 23:11

CLo