1.常用的生成二維碼網址 https://cli.im/
2.上官網下載二維碼插件 http://zxingnet.codeplex.com/
3.將下載的插件中zxing.unity.dll文件放入Unity工程中Plugins文件夾下
4.將下面腳本掛載在場景的游戲物體身上,創建兩個RawImage用於顯示攝像頭畫面與生成的二維碼,根據需要修改參數(如果報錯,有可能是你的電腦沒有攝像頭)
1 /************************************************* 2 * 項目名稱:Unity實現啟用攝像頭掃描與生成二維碼 3 * 腳本創建人:魔卡 4 * 腳本創建時間:2017.12.20 5 * 腳本功能:二維碼識別生成控制類 6 * ***********************************************/ 7 using System.Collections; 8 using System.Collections.Generic; 9 using UnityEngine; 10 using UnityEngine.UI; 11 using ZXing; 12 using ZXing.QrCode; 13 14 //二維碼識別生成控制類 15 public class QRCode : MonoBehaviour 16 { 17 #region 掃描二維碼 18 //定義一個用於存儲調用電腦或手機攝像頭畫面的RawImage 19 public RawImage m_cameraTexture; 20 21 //攝像頭實時顯示的畫面 22 private WebCamTexture m_webCameraTexture; 23 24 //申請一個讀取二維碼的變量 25 private BarcodeReader m_barcodeRender=new BarcodeReader(); 26 27 //多久檢索一次二維碼 28 private float m_delayTime = 3f; 29 #endregion 30 31 #region 生成二維碼 32 //用於顯示生成的二維碼RawImage 33 public RawImage m_QRCode; 34 35 //申請一個寫二維碼的變量 36 private BarcodeWriter m_barcodeWriter; 37 #endregion 38 39 40 #region 掃描二維碼 41 void Start () 42 { 43 //調用攝像頭並將畫面顯示在屏幕RawImage上 44 WebCamDevice[] tDevices = WebCamTexture.devices; //獲取所有攝像頭 45 string tDeviceName = tDevices[0].name; //獲取第一個攝像頭,用第一個攝像頭的畫面生成圖片信息 46 m_webCameraTexture = new WebCamTexture(tDeviceName, 400, 300);//名字,寬,高 47 m_cameraTexture.texture = m_webCameraTexture; //賦值圖片信息 48 m_webCameraTexture.Play(); //開始實時顯示 49 50 InvokeRepeating("CheckQRCode", 0, m_delayTime); 51 } 52 /// <summary> 53 /// 檢索二維碼方法 54 /// </summary> 55 void CheckQRCode() 56 { 57 //存儲攝像頭畫面信息貼圖轉換的顏色數組 58 Color32[] m_colorData= m_webCameraTexture.GetPixels32(); 59 60 //將畫面中的二維碼信息檢索出來 61 var tResult= m_barcodeRender.Decode(m_colorData, m_webCameraTexture.width, m_webCameraTexture.height); 62 63 if (tResult != null) 64 { 65 Debug.Log(tResult.Text); 66 } 67 } 68 #endregion 69 70 #region 傳遞字符串生成二維碼 71 void Update() 72 { 73 if (Input.GetKeyDown(KeyCode.Escape)) 74 { 75 //在這種寫法中 寬高必須256 否則報錯 76 ShowQRCode("魔卡先生", 256, 256); 77 } 78 } 79 /// <summary> 80 /// 顯示繪制的二維碼 81 /// </summary> 82 /// <param name="s_formatStr">掃碼信息</param> 83 /// <param name="s_width">碼寬</param> 84 /// <param name="s_height">碼高</param> 85 void ShowQRCode(string s_str,int s_width,int s_height) 86 { 87 //定義Texture2D並且填充 88 Texture2D tTexture = new Texture2D(s_width, s_height); 89 90 //繪制相對應的貼圖紋理 91 tTexture.SetPixels32(GeneQRCode(s_str, s_width, s_height)); 92 93 tTexture.Apply(); 94 95 //賦值貼圖 96 m_QRCode.texture = tTexture; 97 } 98 /// <summary> 99 /// 返回對應顏色數組 100 /// </summary> 101 /// <param name="s_formatStr">掃碼信息</param> 102 /// <param name="s_width">碼寬</param> 103 /// <param name="s_height">碼高</param> 104 Color32 [] GeneQRCode(string s_formatStr,int s_width,int s_height) 105 { 106 //設置中文編碼格式,否則中文不支持 107 QrCodeEncodingOptions tOptions = new QrCodeEncodingOptions(); 108 tOptions.CharacterSet = "UTF-8"; 109 //設置寬高 110 tOptions.Width = s_width; 111 tOptions.Height = s_height; 112 //設置二維碼距離邊緣的空白距離 113 tOptions.Margin = 3; 114 115 //重置申請寫二維碼變量類 (參數為:碼格式(二維碼、條形碼...) 編碼格式(支持的編碼格式) ) 116 m_barcodeWriter = new BarcodeWriter{Format =BarcodeFormat.QR_CODE ,Options =tOptions }; 117 118 //將咱們需要隱藏在碼后面的信息賦值上 119 return m_barcodeWriter.Write(s_formatStr); 120 } 121 #endregion 122 123 }
5.生成的效果圖如下:

