我正在尝试使用 ZXing.Net 生成 QR 码,起初我遇到了.Save()
由于错误 CS1061 而无法正常工作的问题。所以,我放弃了这个想法,然后我尝试保存.Write()
为图像,然后统一渲染,但 Unity 返回错误:
Cannot implicitly convert type 'UnityEngine.Color32[]' to 'UnityEngine.Sprite'
我尝试使用他们用作解决方案的此处Sprite.Create()
的答案,但转换了 Texture2D 而不是 Color32[ ] 但我无法确认代码是否对我有用,因为代码返回错误:
The type or namespace name 'Image' could not be found
正如我所说,我无法确定代码是否真的有效。我不知道是什么导致了namespace
错误,因为我使用的脚本位于图像 UI 下。
这些是我正在使用的代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using ZXing;
using ZXing.QrCode;
using System.Drawing;
public class SampleScript : MonoBehaviour
{
public Texture2D myTexture;
Sprite mySprite;
Image myImage;
void Main()
{
var qrWriter = new BarcodeWriter();
qrWriter.Format = BarcodeFormat.QR_CODE;
this.gameObject.GetComponent<SpriteRenderer>().sprite = qrWriter.Write("text");
}
public void FooBar()
{
mySprite = Sprite.Create(myTexture, new Rect(0.0f, 0.0f, myTexture.width, myTexture.height), new Vector2(0.5f, 0.5f), 100.0f);
myImage.sprite = mySprite;
}
void Start()
{
FooBar();
Main();
}
我还没有测试过这段代码,因为在运行之前必须先解决错误。