

This method doesn’t seem to do much, however, it let’s us flexibly make GET requests and pass the response from the server to a callback that we can process in other functions. Send the request and wait for a response Using (UnityWebRequest request = UnityWebRequest.Get(url))

IEnumerator GetRequest(string url, Action callback) Public class NetworkManager : MonoBehaviour This might look different than what you’re used to, but I’ll explain why we use this approach. Once you open it up, we’re going to add a simple GetRequest method. We’ll come back to this script during the tutorial to add more request types.

Coding a GET UnityWebRequestįirstly, create a new C# script and name it NetworkManager.cs. Next, let’s dig into our coding out the network request. The reason we added the Horizontal Layout Group is so that we can easily add more buttons in a row when we need to make more requests. Drag your Button under your Button Row and it should position itself in the top left of the screen. Hold down shift + alt and click top left to anchor your Button Row to the top left of the UI. Next, on your Button Row’s Rect Transform, click the Anchor Presets box. Search for “Horizontal Layout Group” and add it to your Button Row. Select it and then hit Add Component in the inspector. Add an empty GameObject under your Canvas named “Button Row”. This will create a Canvas element holding your button. Open a fresh project and select the Game tab to preview our UI. To start, let’s set up our scene in Unity. If not, there are a lot of great resources available. For brevity, we assume that you are familiar with web servers and making HTTP requests. Using UnityWebRequest.Getįirstly, we’ll cover a simple example of how to request data from a web server. If you’re coming from the video you can find the NetworkingManager.cs script in the scripts section below. But to do this, we need to know how to interact with web servers. We can enable features like leader boards, user logins, and cloud data backups. Using this data allows us to take our games and apps to the next level. Nowadays in app development, more data is coming from websites and APIs. In this post, we’ll cover different ways that we can make network requests using Unity’s UnityWebRequest class. “Learn how to efficiently use the UnityWebRequest class to download many different resources from the web.” Overview
