Making a simple network call to retrieve JSON in Unity?

I looked at the network documentation for Unity, and most of the examples seem to be related to the network game for a multiplayer game. I just want to get a JSON response from api for dynamically creating a menu. Are there any good examples for making simple network calls to get a response from a web server?

thanks

+6
source share
3 answers

The easiest way to access the web interface is to use the WWW class in Unity.

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

Be sure to check the documentation for asynchronous use or block execution until the web server is responding (or not responding).

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

+2
source

If you use C # rather than targeting Webplayer or iOS, you can use HttpWebRequest and a library like Json.NET if you want (if you have experience with this class instance). You will likely have to spend a little extra time processing the request correctly asynchronously.

There is also a Unity WWW class , where you will check and analyze the resulting text property. The WWW class has the advantage of requiring very little code to execute the request asynchronously.

Please note that without an asynchronous request, you can easily block progress in the rest of the game loop, which is almost always undesirable.

+3
source

I would call wget $urlOfApi --header=Accept:application/json -O file.json and read the file.json file.

-8
source

Source: https://habr.com/ru/post/902032/


All Articles