Skip to content

shirleyman/unity-qrcode

Repository files navigation

Unity QR Code Generator - Create and display QR codes in your Unity projects.

A simple and easy-to-use Unity package for generating QR codes at runtime and displaying them on UI elements.

Table of Contents

Overview

This package provides a QRCodeGenerator MonoBehaviour that can be attached to a GameObject to render QR codes to a RawImage component. It is built upon the widely-used ZXing.Net library.

Ideal for sharing links, game levels, or other data in Unity applications.

Features

  • Generate QR codes from any string data and display the generated code on a UnityEngine.UI.RawImage.
  • Automatically adjusts the QR code size to fit the target RawImage component.
  • Configurable border margin.
  • Includes unit tests that can be launched in Test Runner.

Dependencies

  • Unity UI Package (com.unity.ui)
  • This package includes a pre-compiled version of zxing.unity.dll, which provides QR code generation functionality using the ZXing.Net library. The DLL is located in the zxing.unity folder of the package.

Compatibility

Important: This package has only been tested on Unity version 6.1 for Editor, iOS and Android. Compatibility with other Unity versions and platforms is not guaranteed.

Installation

Using Unity Package Manager (recommended)

You can install this package in Unity's Package Manager using the "Add package from git URL" option.

  1. Open the Package Manager (Window > Package Manager).
  2. Click the + button in the top-left corner.
  3. Select "Add package from git URL...".
  4. Enter the repository's HTTPS URL (e.g., https://github.com/shirleyman/unity-qrcode.git) and click "Add".

Manual Installation

  1. Clone this repository.
  2. Copy the package contents to your Unity project's Packages directory.

How To Use

  1. Create a RawImage component in your scene (GameObject > UI > Raw Image).
  2. Add the QRCodeGenerator component to the same GameObject.
  3. The component will automatically find the RawImage on the same GameObject.
  4. In your own script, get a reference to the QRCodeGenerator component.
  5. Call the GenerateQRCode(string textToEncode) method to generate and display the code.

Example

using UnityEngine;
using OrbitalNine.QRCode; // Make sure to include the namespace

public class MyQRCodeController : MonoBehaviour
{
    public QRCodeGenerator qrCodeGenerator;
    public string textToEncode = "https://unity.com/";

    void Start()
    {
        if (qrCodeGenerator == null)
        {
            qrCodeGenerator = GetComponent<QRCodeGenerator>();
        }
        
        // Generate the QR code on start
        if (qrCodeGenerator != null)
        {
            qrCodeGenerator.GenerateQRCode(textToEncode);
        }
    }
}

Configuration

The QRCodeGenerator component has the following configurable properties in the Inspector:

  • QR Code Size: The width and height of the generated QR code texture in pixels. On Awake() and Reset(), this value is automatically set to the smaller of the RawImage's width or height.
  • QR Code Margin: The width of the white border around the QR code, in modules (pixels of the QR code matrix).
  • QR Code Image: A reference to the RawImage component used for display. If not assigned, the component will attempt to find one on the same GameObject.

Testing

This package includes a suite of tests for both runtime and editor modes. You can run these tests using the Unity Test Runner window (Window > General > Test Runner).

License

Primary License

This project is licensed under the MIT License. See LICENSE for details.

Third-Party Dependencies

ZXing.NET

Acknowledgments

This package uses ZXing.Net, a .NET port of the ZXing barcode library, which is licensed under the Apache License 2.0.

Featured In

About

A simple and easy-to-use Unity package for generating QR codes at runtime and displaying them on UI elements.

Topics

Resources

License

MIT, Unknown licenses found

Licenses found

MIT
LICENSE
Unknown
LICENSE.meta

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages