Unity Web Request
Objective: Working with Unity Web Request.
Before working with Web request, I have prepared the scene with canvas ,Image with raw Image component. Then create a script called DownloadImage and attach to the RawImage.
Open the script, create a string variable called URL, then right click image and select copy image address for the Image and paste it to the inspector.
To send request to the Internet in unity we use UnityWebRequest handler ,then we assign this handler to type of content we need since we downloads image we will assign to class called UnityWebRequestTexture.GetTexture.
But Why you are using using keyword and with double parenthesis with closed and open curly bracket??
Important
We are using the using keyword because this make sure when we get the response the object get closed, and memory allocated are released properly.
Until now we did nothing we just open the get to file destination, to send this request we need to using our variable to send request to the Interned using keyword SendWebRequest.
Important!!
If we do like this we will cause a problem to our application?
Because we my have other code want to access this request here is the Unity documentation warn us about this.
Solution
we need to wait until the server finish all calculation and give us result.
But to do this we need to use yield keyword, so now we need to change our method to coroutine and use yield keyword before request.SendWebRequest().
Checking error before accessing result
We use two type of error checking isHttpError and isNetworkingError.
To get the downloads data we need to create a texture variable and assign it to DownloadHandlerTexture.GetContent ,and pass request in double parenthesis.
To assign the image as texture to our raw Image in unity, first create raw Image variable, then set rawImage texture to our texture.
Last call the coroutine in the start method.
Download html data using api.
In this one we use UnityWebRequest handler, then assign it to the UnityWebRequest.Get, then passing URL in double parenthesis.
Go the unity editor ,add text component and our script to this text component then paste catfact url.
To download the html file we need to use request.downloadHandler.text but this need to be assign to the handler of text component , I have add Serialize Field to a text variable I created at this moment, drag and drop the text game Object to the text inspector.
If we play like this we will get a bunch of Json file from catFact
How to convert this Json file to normal text / string in C#.
Go to the CastFact/Get/Response data copy that json format
Go to website called Json to csharp
here a link https://json2csharp.com/
Now take that C# class and create paste to new file or same file.
Rename it and remove the properties and leave the string variable.
Create a local text variable and assign it to request.downloadHandler.text.
Then create another variable called catFact assign this to JsonUtility.fromJson<T>(Pass json here).
This utility class will convert json format to normal string, it take type of CatFact class as type and convert our json to text.
And last I have assign the text.text to catfact.fact, means set our converted text to our text component in unity.
See you into the next one,
Note: learn about json its important if you use API and move thing from internet to unity or to other part.