Objective: Use the URL to return the Map image!
In the last tutorial we constructed our URL and now it’s time to fetch that URL and return the actual Image. We need to understand how to send a URL request. We can learn the ins and outs of sending a URL by going to the Unity scripting API: WWW.
You start a download in the background by calling WWW(URL) which returns a new WWW object. In the example given they have a URL similar to ours. from there they use a coroutine to say, “using the WWW and create a new WWW and passing URL for that object and it fetches that URL.
Then the program waits for the WWW result to come back and that image downloads. From there they get the renderer component. They get the texture of the render component and assign it to the texture downloaded. This is how we are going to handle the map.
Let’s open the location panel script, the script where we wrote our URL.
We are currently using the On Enable method which cannot be changed into a coroutine. This means we need to create a coroutine and make sure to call this coroutine after the URL is constructed.
Writing our routine, we start with the Using statement to create a new WWW class and yield return for the WWW.
This yield makes the program wait until we receive information back from this WWW request. Once the map comes back, we can check for errors.
Now if there is an error it will display on the console. Now we can take the map texture and assign to our raw image.
Now we can save and play the editor and test our code.
It works! In the next tutorial we are going to discuss using Geo Location Services. I’ll see you in the next tutorial!