2026-01-09 08:45:22 +03:00

42 lines
1.5 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System.IO;
using UnityEngine;
using QRCoder;
using Color = System.Drawing.Color;
public static class QrMaker
{
// Возвращает Texture2D для UI/материалов
public static Texture2D MakeTexture(string text,
int pixelsPerModule = 8,
bool drawQuietZones = true,
Color darkColor = default,
Color lightColor = default)
{
Color dark = darkColor == default ? Color.Black : darkColor;
Color light = lightColor == default ? Color.White : lightColor;
QRCodeGenerator gen = new();
QRCoder.QRCodeData data = gen.CreateQrCode(text, QRCodeGenerator.ECCLevel.M);
PngByteQRCode png = new(data);
// Генерим PNG-байты с указанными цветами
byte[] bytes = png.GetGraphic(pixelsPerModule, dark, light, drawQuietZones);
Texture2D tex = new(150, 150, TextureFormat.RGBA32, false);
tex.LoadImage(bytes); // раскодирует PNG → RGBA
tex.Apply(false, false);
return tex;
}
/*
// Сохранить как PNG-файл
public static string SavePng(string text, string filePath, int ppm = 8)
{
using var gen = new QRCodeGenerator();
using var data = gen.CreateQrCode(text, QRCodeGenerator.ECCLevel.M);
var png = new PngByteQRCode(data);
byte[] bytes = png.GetGraphic(ppm, "#000000", "#FFFFFF", true);
File.WriteAllBytes(filePath, bytes);
return filePath;
}*/
}