What is Unity UnityWebRequest?
Hello everyone, in this article I will explain to you a more current version of a topic I discussed previously. A few posts ago, I talked about using WWW in Unity and even shared code where we fetched an image via the internet using WWW, but after writing the code, I realized that the code I wrote with WWW was always being marked as "[OBSOLOTE]".
Let me give you a little info about OBSOLOTE; this warning is generally given in C# for functions that are not recommended anymore and are outdated.
Anyway, I did not stop there and browsed Unity documentation for an alternative to this discouraged code, and I found UnityWebRequest as a counterpart to WWW, so I wanted to share it with you immediately. Happy reading.
What is UnityWebRequest?
Unity WebRequest is a built-in library that allows us to connect to any website from within a game.
Thanks to this library, we can do many things in our games using Web Services. From user registration, preparing leaderboards, even downloading textures via the internet, and many more purposes.
You can read and save JSON files, manage data with MYSQL. What you can do with this library depends on your programming knowledge and imagination.
UnityWebRequest Post Operation
IEnumerator _VeriPostEt() // We create a numerator
{
yield return new WaitForEndOfFrame(); // we wait for the last frame
WWWForm _wwwForm = new WWWForm(); // we create a www form
_wwwForm.AddField("veri", "veri"); // we add to the form the data we want to send, left side is the field on the receiving side, right side is the value sent
UnityWebRequest veriGonder = UnityWebRequest.Post("http://postedilecekveriurl", _wwwForm); // we create the webrequest, left side is the destination url, right side is our form
yield return veriGonder.SendWebRequest(); // we send our data
Debug.Log(veriGonder.downloadHandler.text.ToString()); // we fetch the data coming from the site.
}
UnityWebRequest Reading Data from Site
IEnumerator VeriCek() // We create a numerator function to fetch data
{
UnityWebRequest _veriCek = UnityWebRequest.Get("http://urhoba.net"); // we create a WebRequest by entering the site the data will be fetched from
yield return _veriCek.SendWebRequest(); // we send a request to fetch data
var cekilenVeri = veriCek.downloadHandler.text.ToString(); // we assign the data coming from the site to a variable.
}


Yorum Gönder