Reading and Generating QR codes with C# in Unity 3D — the easy way

the smooth solution

using ZXing;
using ZXing.QrCode;

private WebCamTexture camTexture;
private Rect screenRect;
void Start() {
screenRect = new Rect(0, 0, Screen.width, Screen.height);
camTexture = new WebCamTexture();
camTexture.requestedHeight = Screen.height;
camTexture.requestedWidth = Screen.width;
if (camTexture != null) {

void OnGUI () {
// drawing the camera on screen
GUI.DrawTexture (screenRect, camTexture, ScaleMode.ScaleToFit);
// do the reading — you might want to attempt to read less often than you draw on the screen for performance sake
try {
IBarcodeReader barcodeReader = new BarcodeReader ();
// decode the current frame
var result = barcodeReader.Decode(camTexture.GetPixels32(),
camTexture.width, camTexture.height);
if (result != null) {
Debug.Log(“DECODED TEXT FROM QR: “ + result.Text);
} catch(Exception ex) { Debug.LogWarning (ex.Message); }

rendering QR codes from text in Unity

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);
public Texture2D generateQR(string text) {
var encoded = new Texture2D (256, 256);
var color32 = Encode(text, encoded.width, encoded.height);
return encoded;
Texture2D myQR = generateQR("test");
if (GUI.Button (new Rect (300, 300, 256, 256), myQR, GUIStyle.none)) {}

was it so hard?


Full-stack software engineer — “The computer programmer is a creator of universes” ~ Joseph Weizenbaum

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store