Unity创建二维码
1.引入Dll
下载Dll 密码:8djo
2.创建二维码
using UnityEngine;
using ZXing;
using ZXing.QrCode;
using System.IO;
public class CreateTexture : MonoBehaviour
{
void Start()
{
CreatQR_texture("https://www.jianshu.com/p/9b901f15b79e");
}
private Texture CreatQR_texture(string message)
{
var encoded = new Texture2D(256, 256);
if (message.Length > 1)
{
var color32 = Encode(message, encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
var buffer = encoded.EncodeToPNG();
using var fs = new FileStream(Application.dataPath + "/二维码.png",FileMode.Create);
fs.Write(buffer);
fs.Close();
#if UNITY_EDITOR
UnityEditor.AssetDatabase.Refresh();
#endif
}
else
{
Debug.Log("生成二维码失败");
}
return encoded;
}
private 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);
}
}