直接上代码:
需要导入插件: ZXing
using ZXing;
using UnityEngine;
using ZXing.QrCode;
using UnityEngine.UI;
public class QRCode : MonoBehaviour
{
public RawImage qrcode;
void Start()
{
qrcode.texture = GenerateQR("Hello World!");
}
public Texture2D GenerateQR(string text)
{
var encoded = new Texture2D(256, 256);
var color32 = Encode(text, encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
return encoded;
}
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);
}
}