Accessing SWAPI in Unity

A few weeks ago, I learned about SWAPI (Star Wars API) – the REST API for getting information about people, planets, ships, films, and species related to the Star Wars universe. As part of a Meta AR hackathon project that I worked on this weekend, I decided to integrate SWAPI into Unity. You can find the whole project on GitHub here.

SWAPI

Stereoscopic rendering in Unity of a sphere GameObject representing Hoth. The MGui objects for the Meta HMD are displaying information pulled from SWAPI in realtime as the planet is accessed in the application.

A little bit about SWAPI: I have a bit of an obsession with Star Wars, so when I was introduced to a full API to access the information about the Star Wars universe, I knew that I had to include it into one of my projects. SWAPI allows you to make a GET request to a base URL, http://swapi.co/api/, which you then append a specific suffix to in order to make requests for specific things – we focused on /planets during this demonstration. Each category can be requested on its own, which will return a JSON object with an array of all elements (i.e http://swapi.co/api/planets) or you can specify an id for which particular planet you want data for (i.e. http://swap.co/api/planets/1, which returns data for Tatooine).

So, onto the implementation! First, I created a new C# script from within Unity. In addition to the standard Start() and Update() functions, you’ll need to add in a new function that returns an IEnumerator object. Since we were accessing information about individual planets based on their tag in Unity, I called this function DownloadPlanet(string s), which took a string parameter that specified which number planet to retrieve data for.

In the script that I had created, I stored the basic URL in a string object, and when the desired planet was selected using the Meta onTouchDwell() method (you could use an onclick for integration in a non-AR project), the DownloadPlanet() function would append the passed in string to the root URL and I used that string for making the GET request. After playing around a bit with this, I realized it was actually a lot simpler than I first thought, and was able to get the JSON object without any trouble in just three lines of code.

IEnumerator DownloadPlanet(string s)
 {
 // Grab the tag and append it to the baseURL
 WWW w = new WWW(baseURL + s);
 yield return w;
 yield return new WaitForSeconds(1f);
 Debug.Log("Received planet");
 ExtractPlanetData(w.text);
 }

I grabbed a JSON class for parsing through the result from the asset store in order to get the results in a readable format, which I used in a new method called ExtractPlanetData(String s). This method takes in the WWW object in a text format, which we convert to a JSON object in the ExtractPlanetData method. After that, it was just a matter of parsing out the JSON based on their keys to get the relevant data. We knew up front which fields we wanted to pull out, but you can modify the function to iterate through the keys if you need different behaviors for the API you choose.

void ExtractPlanetData(string json_planet)
    {
        JSONObject planet = new JSONObject(json_planet);
        nameText.text = planet.GetField("name").ToString().Trim('"');
        climateText.text = planet.GetField("climate").ToString().Trim('"');
        terrainText.text = planet.GetField("terrain").ToString().Trim('"');
        populationText.text = planet.GetField("population").ToString().Trim('"');
        gravityText.text = planet.GetField("gravity").ToString().Trim('"');
    }

The main goal of the hack was to play around with the Meta API and headset (more on that coming soon) but what I really wanted to do was find a definitive way to grab data from REST APIs in Unity, and it was a success!

You can find full documentation for accessing the Star Wars API here: http://swapi.co/documentation 

Related Posts

2 thoughts on “Accessing SWAPI in Unity

Leave a Reply

Your email address will not be published.