[EN] Scanning a QR Code and displaying 3D objects in Unity3D

As for the ultimate game creation program like Unity3D, it has a lot to offer as mentioned before. One of which is that the program can create games on almost every platform such as computer or mobile. This article will talk about the use on mobile phones. Of course, there must be a component that the computer does not have, such as a camera, which will discuss how to scan a QR Code to operate according to the text that can be extracted and ordered it to be rendered as a 3D object.

QR Code

Introductory bit for everyone to understand that QR Code is similar to Barcode, that is, the text is converted into a specified format which looks like a square. There are three direction points to know which side is the top and use the program to scan to extract text which can be letters, numbers, various characters, links, etc.

To decode a QR code to text, you can either scan it with a camera or save an image and use a decoder.

Figure 1 QR code of text “Hello world!! we are Jarutex!!”

Reading QR code in Unity3d

In order to read QR code in Unity3d, there is already a library to use: IBarcodeReader which has sample code as follows:

IBarcodeReader barcodeReader = new BarcodeReader();

IBarcodeReader barcodeReader = new BarcodeReader();
var result = barcodeReader.Decode(camTexture.GetPixels32(), camTexture.width, camTexture.height);
if( result.Text == "Jarutex" ){
  //Do something
}

Creating a 3D object displayed on the screen.

Creating a 3D object in Unity3d is very easy with the following steps:

  1. Import a 3D object in Assets and name it as you want, as shown in Figure 2.
  2. Create prefabs with imported objects.
  3. Declare a variable to hold an object as a GameObject.
  4. Declare a variable to hold an object as a GameObject.
Figure 2 Importing object

Example Code

public GameObject crab, crab1
crab1 = Instantiate(crab, new Vector3(0f, 0f, 0f), Quaternion.Euler(70, 0, 0));
Figure 3 Crab in 3d object
Figure 4 Trout in 3d object

In the above statement, the crab variable is a variable that is used as a 3D object, which is only stored in the program but has not yet been created. Then order to create an object by referring the object from crab to store it in crab1 for further use.

Combining the two codes, 3D objects can be easily rendered.

IBarcodeReader barcodeReader = new BarcodeReader();
var result = barcodeReader.Decode(camTexture.GetPixels32(), camTexture.width, camTexture.height);
if( result.Text == "Jarutex" ){
  public GameObject fish, fish1
  fish1 = Instantiate(fish, new Vector3(0f, 0f, 0f), Quaternion.Euler(70, 0, 0));
}

Conclusion

It is very easy to read QR codes in Unity3d with an easy-to-use library, IBarcodeReader.

  1. Create IBarcodeReader object
  2. Use a variable to read the QR code through the camera and store it for the resulting variable.
  3. Read the result with an attribute named Text (result.Text).

The rendering of 3D objects is also very easy by importing the prepared objects and creating that object on the screen with the Instantiate command in the specified position and rotation. Have fun with programming.

(C) 2022, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2022-03-04