chip-game/Assets/Scripts/QrMaker.cs
2026-01-12 09:36:17 +03:00

35 lines
1.2 KiB
C#

using System.IO;
using UnityEngine;
using QRCoder;
public static class QrMaker
{
// Возвращает Texture2D для UI/материалов
public static Texture2D MakeTexture(string text, int pixelsPerModule = 1,
bool drawQuietZones = true)
{
using var gen = new QRCodeGenerator();
using var data = gen.CreateQrCode(text, QRCodeGenerator.ECCLevel.M);
var png = new PngByteQRCode(data);
// Генерим PNG-байты без System.Drawing
byte[] bytes = png.GetGraphic(pixelsPerModule);
var tex = new Texture2D(2, 2, 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;
}*/
}