AWS, Cloud Computing

7 Mins Read

Calling REST APIs in UNITY for CRUD Operations

TABLE OF CONTENT

1. Overview
2. Introduction to REST APIs
3. CRUD Operations with Unity
4. Backend app.js Code
5. Step-by-Step Guide
6. Conclusion
7. About CloudThat
8. FAQs

 

Overview

Unity is one of the most popular game development engines that developers use for game development. It is beginner-friendly and supports various platforms like Windows, iOS, Android, etc. It uses C# as its scripting language, in which we will write the code.

This blog will teach you how to make CRUD operations using REST APIs in Unity.

We use REST APIs to communicate with the server from the client end.

Introduction to REST APIs

Before understanding REST, let us understand what an API is. An API is a software that is used by other software to communicate with each other and as well as hardware. It acts as a bridge between the software and hardware.

REST stands for Representational State Transfer. It is an architectural style that is used to create web services. It uses HTTP protocol and makes HTTP requests over World Wide Web. RESTful web services have a Uniform Resource Identifier (URI) like text, JSON, XML, etc., but JSON is the most popular.

REST APIs are stateless operations, i.e., the current operation does not depend on previous operations. The main functionalities which can be created through REST architecture are Create, Read, Update and Delete (CRUD). The commonly used HTTP methods for these operations are:

CRUD Operations with Unity

We can write the code for performing CRUD operations from Unity to the server. Create a local server where our endpoints are hosted and call from our client-side that is, Unity. We need to create a local server to perform this task, so we create them with NodeJS and Express JS. I will be attaching the link to the blog where you can create the server on your local machine.

Here is a blog for creating the local server.

We will be using the same example explained in the above blog to make the CRUD operations from Unity.

Backend app.js Code

Creating a server to run a port 3000

We are using objects as a database 

Get all books details

Create or add a new record

Update an existing record

Deleting an existing record

The code mentioned above can help you completely understand the backend code. So here, we are using objects as a database to store and retrieve data. We are using an array of books with a title and id associated with them.

Step by Step Guide

Step 1: Create a new 2D unity project named CRUD with REST.

Unity

Step 2: Let us create a new C# script exampleClass.cs script and create a new class called “book,” which has two attributes, “title” and “id.”

Unity

Step 3: Let us create a new C# Script apiCalls.cs script, in which we will write all the API calls that we are going to use in this blog.

After creating the C# Script, now let us write the code for the Get request. Here we shall retrieve the data from the server. Before we write the code let’s design the front end, on the click of a button, we should be able to retrieve the data from the server. To do that let us create a new canvas and insert a new empty white image. On top of that let’s place a button called GET response. On Click, all the books’ data will be retrieved from the back end and printed on the log.

Unity

Step 4: Write the code to get the book details from the server.

Unity

Here we have created an object of UnityWebRequest called get request.

Then in getBooksData() Coroutine, we are making a get request. We are initiating the get request with the URL “localhost:3000/api/books” and making the request with SendWebRequest() function.

Once we get the response back, we will check the status code if the between 200 to 300 request was successful, and we got the book data, which will be printed on the console. Now we need to create an empty object and attach the script to it. Let’s create a function called get() and call the coroutine there, then call the object as apiHandler and after that, let’s assign this object to the Onclick() function of the Get Response Button. We will be doing the same thing for all the buttons we are going to develop for upcoming API calls. 

Unity

Unity

Step 5: 

Similarly, let us write the code for creating, updating, and deleting the records. 

Create records: Add a button called Post details and one text field to enter the details of the new record, that is the title 

Update the records: Add a button called Put details and two text fields, title to modify the existing title, and id to recognize the title to be edited.

Delete the records: Add a button called Delete details and 1 text field to recognize which data to delete.

Unity

Post Details

Unity

Unity

Unity

Unity

PUT Details Code

Unity

Unity

Unity

Delete Details Code

Unity

Unity

Unity

Conclusion

In this way, we can make REST API calls from Unity. We have completed our article on how to make calls using a simple REST API in Unity. Feel free to ask any queries you may have while working on the same, and I will be happy to help you solve them.

About CloudThat

CloudThat is the official AWS (Amazon Web Services) Advanced Consulting Partner and Training partner and Microsoft Gold Partner, helping people develop knowledge of the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

Drop a query if you have any questions regarding Unity, RESTful APIs, or game development and I will get back to you quickly. To get started, go through our Expert Advisory page and Managed Services Package that is CloudThat’s offerings.

Frequently Asked Questions

  1. What is the difference between PUT and POST methods?

A ‘PUT’ request is made to a particular resource. Suppose the Request-URI refers to an already existing resource. In that case, an update operation will happen, otherwise create operation should happen if Request-URI is a valid resource URI (assuming the client is allowed to determine the resource identifier).

POST method is used to request that the origin server accept the entity enclosed in the 

request as a new subordinate of the resource identified by the Request-URI in the Request-Line. It essentially means that POST request-URI should be of a collection URI

  1. What are HTTP status codes?

An HTTP status code is a server response to a browser’s request. When you visit a           

website, your browser sends a request to the site’s server, and the server  then responds           

to the browser’s request with a three-digit code: the HTTP status code.

WRITTEN BY Subramanya Datta

SHARE

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!