Web requests in unity are super easy to use. We gonna use the UnityWebRequest class as the WWW class is deprecated now. UnityWebRequest class can be used for both Get and Post methods. The 'UnityEngine.Networking' namespace is required to work with UnityWebRequests.
This tutorial is made with some simple UI elements. First Let's see how the GET method works.
01. UnityWebRequest.GET
We use this method to receive data from the server as normal get requests. Using coroutines in web requests is mandatory as we have to wait until the download is complete before showing the results, and the coroutines make this much easier. A simple button calls the method and a text element will show the result.
The script contains a reference to the text element and the string URL of your PHP file.
When the button is pressed the button executes the ButtonGetData method and that method simply starts the GetData coroutine. We use the using keyword as this data is disposable. We create a web request and call the 'SendWebRequest()' method with the yield return keyword to wait till the download is complete before executing the rest. If the web request throws an error a debug.log or a log warning notifies that there's an error. Finally, we access the download handler of the web request class and get the result text of it and then we set our text elements text to the result text.
Our PHP code is very simple and when we run the script the following result is generated.
02.UnityWebRequest.Post
This is much like the get method. We use the 'UnityWebRequest.post' and as the second parameter, we have to feed an instance of the WWWform class. Adding data fields is easy and we can add as many as we want in the www form in order to send those data to the server. 'WWWForm.AddField' takes usually a string key and a string text as parameters. This data can be accessed from the PHP file using the '$_POST' keyword.
An Input field is used to enter the data and the text field to show the result.
When the button is pressed following result is generated according to the input.
When we look at the use cases of these WebRequests the most common use cases are to login to the game server and to receive game data from the server. Let's try out a simple example. Imagine that we have a MySQL database which contains the details of some heroes.
We can send the hero name in a post method and run some simple MySQL queries and assign the results to this hero name, damage and armor values.
But here for simplicity, we use only a get method and show the results in the text fields.
The result looks like this.
Related Articles
This tutorial is made with some simple UI elements. First Let's see how the GET method works.
01. UnityWebRequest.GET
We use this method to receive data from the server as normal get requests. Using coroutines in web requests is mandatory as we have to wait until the download is complete before showing the results, and the coroutines make this much easier. A simple button calls the method and a text element will show the result.
The script contains a reference to the text element and the string URL of your PHP file.
Our PHP code is very simple and when we run the script the following result is generated.
02.UnityWebRequest.Post
This is much like the get method. We use the 'UnityWebRequest.post' and as the second parameter, we have to feed an instance of the WWWform class. Adding data fields is easy and we can add as many as we want in the www form in order to send those data to the server. 'WWWForm.AddField' takes usually a string key and a string text as parameters. This data can be accessed from the PHP file using the '$_POST' keyword.
An Input field is used to enter the data and the text field to show the result.
When the button is pressed following result is generated according to the input.
When we look at the use cases of these WebRequests the most common use cases are to login to the game server and to receive game data from the server. Let's try out a simple example. Imagine that we have a MySQL database which contains the details of some heroes.
We can send the hero name in a post method and run some simple MySQL queries and assign the results to this hero name, damage and armor values.
using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; using UnityEngine; using UnityEngine.Networking; using UnityEngine.UI; public class ConnectionController : MonoBehaviour { public Text heroNameText; public Text damageText; public Text armourText; public InputField inputField; private const string URL = "http://localhost/testserver.php"; public void ButtonGetData() { StartCoroutine(GetData()); } IEnumerator GetData() { using (UnityWebRequest webRequest = UnityWebRequest.Get(URL)) { yield return webRequest.SendWebRequest(); if (webRequest.isHttpError || webRequest.isNetworkError) { Debug.Log("Error"); } else { Debug.Log(webRequest.downloadHandler.text); string result = webRequest.downloadHandler.text; SplitWords(result); } } } void SplitWords(string result) { string[] words = result.Split('|'); heroNameText.text = $"Hero : {words[0]}"; damageText.text = $"Damage : {words[1]}"; armourText.text = $"Armour : {words[2]}"; } }
<?php $heroName = "SuperMario"; $damage = 56; $armour = 22; $output = $heroName."|".$damage."|".$armour; echo $output; ?>
But here for simplicity, we use only a get method and show the results in the text fields.
The result looks like this.
Related Articles
Keep it up, thanks for this!
ReplyDeleteСпасибо!
ReplyDelete