Skip to content

Add a UIToolkit control for displaying QR codes #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions QRCoder.Unity/QRCodeDisplay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

namespace QRCoder.Unity
{
public class QRCodeDisplay : VisualElement
{
public QRCodeDisplay()
{
value = null;
style.unityBackgroundScaleMode = ScaleMode.ScaleToFit;
}

public void Update(string value, QRCodeGenerator.ECCLevel eccLevel, int pixelsPerModule)
{
_value = value;
_eccLevel = eccLevel;
_pixelsPerModule = pixelsPerModule;
RegenerateImage();
}

public QRCodeGenerator.ECCLevel eccLevel
{
get => _eccLevel;
set
{
if (_eccLevel == value)
return;
_eccLevel = value;
RegenerateImage();
}
}

public int pixelsPerModule
{
get => _pixelsPerModule;
set
{
if (_pixelsPerModule == value)
return;
_pixelsPerModule = value;
RegenerateImage();
}
}

private string _value;
private Texture2D _image;
private QRCodeGenerator.ECCLevel _eccLevel = QRCodeGenerator.ECCLevel.Q;
private int _pixelsPerModule = 8;

public string value
{
get => _value;
set
{
if (_value == value)
return;
_value = value;
RegenerateImage();
}
}

private void RegenerateImage()
{
if (_image != null)
Object.DestroyImmediate(_image, true);

_image = null;

if (value == null)
return;

using var generator = new QRCodeGenerator();
using var data = generator.CreateQrCode(value, eccLevel);
var unityCode = new UnityQRCode(data);
_image = unityCode.GetGraphic(pixelsPerModule);
_image.name = "Generated QR code";

style.backgroundImage = _image;
}

public new class UxmlFactory : UxmlFactory<QRCodeDisplay, UxmlTraits>
{
}

public new class UxmlTraits : VisualElement.UxmlTraits
{
private UxmlStringAttributeDescription m_Value = new() { name = "value" };

private UxmlEnumAttributeDescription<QRCodeGenerator.ECCLevel> m_ECCLevel = new()
{ name = "ecc-level", defaultValue = QRCodeGenerator.ECCLevel.Q };

private UxmlIntAttributeDescription m_PixelsPerModule = new()
{ name = "pixels-per-module", defaultValue = 8 };

public override IEnumerable<UxmlChildElementDescription> uxmlChildElementsDescription
{
get { yield break; }
}

public override void Init(VisualElement ve, IUxmlAttributes bag, CreationContext cc)
{
base.Init(ve, bag, cc);

var qr = (QRCodeDisplay)ve;
qr.Update(m_Value.GetValueFromBag(bag, cc),
m_ECCLevel.GetValueFromBag(bag, cc),
m_PixelsPerModule.GetValueFromBag(bag, cc));
}
}
}
}