This is the sixth day of my participation in the August More text Challenge. For details, see:August is more challenging
An overview of the
Two-dimensional Code, also known as two-dimensional bar Code, the common TWO-DIMENSIONAL Code is QR Code, QR full name Quick Response, is a very popular way of coding on mobile devices in recent years. It is often used in daily life. Has become an indispensable part of our life. So how do you do that in Unity? Let’s take a look.
There are two ways to create a QR code in Unity (found by the author so far, maybe others).
- The first one is based on Zxing and depends on Zxing.Unity.dll. It is suitable for PC and Android and can be used offline.
- The second is to generate it directly from a web page and download it (more on that in the next article)
This article mainly introduces how to create qr codes using ZXing.
ZXing
ZXing is an open source, Java implementation of a variety of formats of 1D/2D bar code image processing library, it includes ports for other languages. ZXing can scan and decode bar codes using the phone’s built-in camera. The official website is here (with tools). The interface is as follows
Use in Unity
Use in Unity requires the zxing. DLL file. Import it into the Unity project file, preferably in the Plugins folder. The general idea of generating a QR code is to generate the string/url information of the child. Use the Write method of BarcodeWriter to generate the QR code. The returned data is Color32[], which is converted to a Texture2D image by Texture2D’s SetPixels32 method. Finally, the Texture2D image is converted to the desired image format and displayed in Unity. First, create the QRcode generation script qrCoDecreate.cs, and then introduce the namespace into the script
using ZXing;
using ZXing.QrCode;
2, define a string, which is used to generate qr code, generated after successful scan will appear a defined content string url = “https://juejin.cn/user/4143372312773549/”; 3, write the generate QR code method, which returns a Color32 array
<param name="textForEncoding"> </param> /// <param name="textForEncoding"> </param> /// <param Name ="width"> </param> </param> </param> </param> </returns> </returns> private static Color32[] Encode(string) textForEncoding, int width, int height) { var writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions { Height = height, Width = width } }; return writer.Write(textForEncoding); }Copy the code
4, create Texture2D image from the Color32 array
Var color32 = Encode(QrCodeStr[Nmuber], encoded. Width, encoded. Height); Texture2D texture2D; texture2D.SetPixels32(color32); texture2D.Apply();Copy the code
At this point, the TWO-DIMENSIONAL code image is generated. You can change the type of Texture2D image to display or save it locally according to your needs. The final generated effect is
BarcodeFormat enumeration is explained
There are altogether 21 BarcodeFormat, representing different types of QR codes, such as those commonly used aboveQR_CODE = 2048
.
Its specific explanation please refer to this article, explain in more detail. This article addresses
conclusion
Using ZXing to create qr codes is still very convenient and fast, can be used on PC and Android, and can be used offline. Is right there’re a total of 21 BarcodeFormat can, but should pay attention to each has its own bar code string format, an error may arise if using the wrong format, for example, ITF only bar code Numbers, if used when creating the qr code strings will be submitted to the following error (I use the above url)
Write in the last
All the shared content are the author in the daily development process used a variety of small function points, share out also in a disguised review, if there is a bad place to write also please give advice. The source code of the Demo will be sorted out and shared later. Welcome to learn from each other and make progress. The next article will show you another way to create qr codes.