Unity创建二维码

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);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值