通過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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章