New in Unity here and an AS3 developer. Having a tough time figuring out the best/easiest way to send and receive JSON from some web services.
In AS3 it’s something like this:
var js:String = JSON.stringify({"userName":auth.user, "password":auth.pwd});
var req:URLRequest = new URLRequest(BASE_URL + "authorize/validateuser");
req.method = URLRequestMethod.POST;
req.data = js;
req.requestHeaders.push(new URLRequestHeader("Content-type", "application/json"));
req.requestHeaders.push(new URLRequestHeader("Accept", "application/json"));
var lo:URLLoader = new URLLoader();
lo.addEventListener(Event.COMPLETE, gotToken, false, 0, true);
lo.addEventListener(IOErrorEvent.IO_ERROR, tokenError, false, 0, true);
lo.load(req);
It’s just so nice and easy in AS3…
Is there a relatively pain free way to accomplish this in Unity/C#?
I’ve found the JSONObject utility but wondering if there’s any built in ways in Unity or .NET?
Also how to easily send/get data from a service? Do I use the WWW class?
Just had this thought - since AS3 is an ECMA Script language - like JavaScript - would it be better for someone coming from AS3 to use JS in Unity, instead of C#?
You can use either. I’ve done both AS3 and C# and find the switchover quite painless. As for JSON, there’s loads of decent free JSON API’s on the Asset Store, like you saw. There’s also plugins you can add into Unity, like this:
I made mine in a very messy way (early draft version), but you get an idea of how it’s put together.
I believe there is a .Net version of JSON, however Unity hasn’t updated their compiler so we’re not able to include it in Unity projects (yet). There is a slew of plugins and the Asset Store things you found.
There’s no JavaScript in Unity. There’s UnityScript, which is .Net with JS makeup. It’s called “JavaScript” in the docs and the files are .js, but other than that they’re not even close.