Unity中创建二维码

再来一篇,希望对人有帮助。

源地址:http://www.cnblogs.com/jenke/p/4718104.html


using UnityEngine;
using ZXing;
using ZXing.QrCode;

 1 private static Color32[] Encode(string textForEncoding, int width, int height)
 2     {
 3         var writer = new BarcodeWriter
 4         {
 5             Format = BarcodeFormat.QR_CODE,
 6             Options = new QrCodeEncodingOptions
 7             {
 8                 Height = height,
 9                 Width = width
10             }
11         };
12         return writer.Write(textForEncoding);
13     }

如上面代码所示,dll给我们提供了一个方法,这个方法需要我们传人一段字符串和需要生成的色块的大小,他对应就会给我们生成一个二维码色块。当然有了色块,我们就可以和容易应用在Unity中了。


 1     public Texture2D encoded;//生成出来的二维码图片
 2     public Texture2D encoded1;//剪切后的二维码图片
 3     string url = "www.baidu.com";//要生成的二维码字符串
 4 void Start()
 5     {
 6         encoded = new Texture2D(256, 256);//创建一张图
 7         UpdateQrCode(url);
 8     }
 9 public void UpdateQrCode(string QRSting)
10     {
11         if (QRSting == null || QRSting == "")
12         {
13             Debug.LogWarning(QRSting+"  ");
14             QRSting = "www.baidu.com";
15         }
16         string textForEncoding = QRSting;
17         if (textForEncoding != "")
18         {
19             Color32[] color = Encode(textForEncoding, encoded.width, encoded.height);
20             encoded.SetPixels32(color);
21             encoded.Apply();
22         }
23         encoded1 = new Texture2D(222, 222);//创建目标图片大小
24         encoded1.SetPixels(encoded.GetPixels(17, 17, 222, 222));//获取对应的色块并设置在新的图片上
25         encoded1.Apply();
26         transform.GetComponentInChildren<UITexture>().mainTexture = encoded1;//设置在Unity组件上
27     }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值