using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; using ZXing; using ZXing.QrCode; public class QRCode : MonoBehaviour { public string QRCodeString; [SerializeField] private Texture2D QRResult; const int QRSize = 256; private Vector2 width_height = new Vector2(QRSize, QRSize); [Serializable] public class QRResultEvent : UnityEvent { }; [SerializeField] public QRResultEvent qrcodeEvent = new QRResultEvent(); List results = new List(); private void Update() { lock (results) { foreach (var r in results) { if (qrcodeEvent != null) { qrcodeEvent.Invoke(r); } } results.Clear(); } } public void Encode() { Color32[] color32 = useEncode(QRCodeString, (int)width_height.x, (int)width_height.y); if(QRResult == null) QRResult = new Texture2D((int)width_height.x, (int)width_height.y); QRResult.SetPixels32(color32); QRResult.Apply(); if(qrcodeEvent != null) qrcodeEvent.Invoke(QRResult); } private Color32[] useEncode(string textForEncoding, int width, int height) { Debug.Log("Encode QR code: " + textForEncoding); BarcodeWriter writer = new BarcodeWriter { Format = BarcodeFormat.QR_CODE, Options = new QrCodeEncodingOptions { Height = height, Width = width } }; return writer.Write(textForEncoding); } }