How to call REST Web Service in unity3d

In this tutorial, I will explain how to call REST Web Service in unity3d. First let’s create a simple REST web Service using ASP.NET Web API then we will call it in unity3d. We will test the application for GET and POST requests.

IDE/Engine used for this project: Visual studio 2017 community edition, unity 2017.1 Personal Edition

Create a REST Web Service using ASP.NET Web API

We will create a simple REST web service which will GET and POST values from a sample text file.

  • Create a new project in visual studio. Select Web template and create ASP.NET Web Application project.

REST Web Service in unity3d - Create Project

  • Select Web API template and click on OK button. This will create the project with ASP.NET Web API template.
    REST Web Service in unity3d - Create Project Web API
  • Add a class in Models folder. Let’s name the class as Test. The body of the class is given below.
  • Open Values controller under Controller folder. Edit the class as below given code. This will be the functionality of our GET and POST service.
  • Run the application. It will open a web page in the browser. Click on the API link on the top. It will open the page with REST API details. Our service setup is now complete.
    REST Web Service in unity3d - Web Page

Call Rest Web Service in unity3d

Now we will see how to call REST Web Service in unity3d. We can either use WWW class or UnityWebRequest class to GET and POST the data from the REST Web Service.  First, we will post some value then we will retrieve them using GET method.

POST Request

Create the below given script and attach it to the Main Camera or any other GameObject.

Using WWW class

Result: Form upload complete message will print in unity console.

REST Web Service in unity3d - Result WWW Console

The sample text file will be updated after this POST request.

REST Web Service in unity3d - Result WWW Text

Using UnityWebRequest class

If you want to get response message after POST request from the server, then add DownloadHandler also with the unity web request.

After completing POST request, you can access the response as given below.

Result: Form upload complete message with response code will print in unity console.

REST Web Service in unity3d - Result Unity Web Request Console

The sample text file will be updated after this POST request.

REST Web Service in unity3d - Result Unity Web Request

GET Request

Create the below given script and attach it to the Main Camera or any other GameObject for implementing GET request.

Using WWW class

Result:

REST Web Service in unity3d - Result WWW Class

Using UnityWebRequest class

Result:

REST Web Service in unity3d - Result Unity Web Request

Hope, You get an idea about how to call REST Web Service in unity3d. Please post your comments for queries and feedback.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

4 thoughts on “How to call REST Web Service in unity3d

  • February 8, 2018 at 2:02 pm
    Permalink

    How to call https certificates by bypassing ssl policy errors and certificates

    Reply
  • March 26, 2018 at 11:28 am
    Permalink

    Hi, Thanks for the article. I’m trying to call an api as told by you but it’s giving me error – “Cannot connect to destination host”. I’ve provided correct url as when I enter that url in browser, it gives me result. But it’s not getting hit from my app.
    Please provide some help. Thanks.

    Reply
    • March 26, 2018 at 6:51 pm
      Permalink

      Hi Shailesh,
      Did you test your service first with some client side app like Postman. If It is working in Postman, then it should also work in unity as given in the tutorial. Check your url using debug.log and make sure it is correct. If the service is hosted on cloud then try without proxy network.

      Reply
  • May 12, 2018 at 11:43 pm
    Permalink

    Thank you much Gyanendu… Was trying to use .net WebRequest which was locking up unity. UnityWebRequest solved the problem. I’m using this to interact with a raspberry pi express api to toggle pins. Works great.

    UnityWebRequest unityWebRequest =
    UnityWebRequest.Get(“http://208…./toggle/27”);
    unityWebRequest.SendWebRequest();
    unityWebRequest.Dispose();

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *