通过Unity3d创建二维码

在如今信息化发展飞速的世界中,二维码 也越来越火,
大街小巷随处可见 ”扫一扫“,
当然游戏里面加入二维码也不是什么稀罕事了 ,
言归正传,到底如何实现?
那让我们一起看看吧
1.下载ZXing.Net.0.14.0.0,下载地址为http://zxingnet.codeplex.com/
 

2.解压下好的文件。
 

3.打开,然后找到其中的unity文件夹并打开。
 

4.将文件夹内的zxing.unity.dll,放到的工程内即可。
 

5.代码编写。
代码 QR_Code
[C#]  纯文本查看  复制代码
?
 
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using UnityEngine;
using System.Collections;
using ZXing;
using ZXing.QrCode;
 
public class QR_Code: MonoBehaviour
{
public Texture2D encoded;
public string Lastresult;
void Start ()
{
encoded = new Texture2D(256, 256);
Lastresult = "http://www.google.com" ; //自己的地址 ,测试用的谷歌
}
private static 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);
}
void Update ()
{
var textForEncoding = Lastresult;
if (textForEncoding != null )
{
var color32 = Encode(textForEncoding, encoded.width, encoded.height);
encoded.SetPixels32(color32);
encoded.Apply();
}
}
void OnGUI()
{
GUI.DrawTexture( new Rect(100, 100,256,256), encoded);
}
}


6.代码写好以后,将脚本挂在一个空物体上、运行、即可生成二维码。
运行效果如下:
 



E~DE61M0N1NC00R8KBJQH30.jpg
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值