Barcode Scanner for Unity Android

Barcode Scanner for Android Documentation

Introduction

Barcode Scanner for Android gives an option to scan all types of barcodes and QR codes. Support only Android devices with Google Play Services.

Installation

  • Download the package from Package Manager
  • Import it in your project
  • Download Google’s Unity Jar Resolver from: Unity Jar Resolver and import in your project. After that resolve all dependencies by using: Assets -> External Dependency Manager -> Android Resolver -> Resolve or Force Resolve
  • Add BarcodeScanner prefab from Prefabs folder to your scene.
  • And that’s it, you can start using it!

Setup

Before starting the scan process you should subscribe for events in order to receive the result from any of your scripts.

    private void OnEnable()
    {
        // Subscribe to receive event when there is an error.
        BarcodeScanner.OnErrorOccurred -= OnErrorOccurred;
        // Subscribe to receive event when usee cancelled the scan process.
        BarcodeScanner.OnScanCancelled += OnScanCancelled;
        // Subscribe to receive the result of the scan process.
        BarcodeScanner.OnBarcodeScanned += OnBarcodeScanned;
    }

    private void OnDisable()
    {
        // Unsubscribe from all events.
        BarcodeScanner.OnErrorOccurred -= OnErrorOccurred;
        BarcodeScanner.OnScanCancelled -= OnScanCancelled;
        BarcodeScanner.OnBarcodeScanned -= OnBarcodeScanned;
    }    

    private void OnBarcodeScanned(Barcode barcode)
    {
        // User barcode object to read the data.
    }

    private void OnScanCancelled ()
    {
        // Do something when the user cancelled the pro
    }

    private void OnErrorOccurred(string message, string errorCode)
    {
        // Error received.
    }

Once that’s done you can start the scan process by simply calling:

    BarcodeScanner.Scan();

And that’s it.