Texture2D是在UnityEngine命名空间中的,用来设置NGUI的UITexture的mainTexture属性显示图片,网络下载图片格式等。
一、保存为Png文件
web = UnityWebRequest.GetTexture(codeUrl); yield return web.Send(); Texture2D t = DownloadHandlerTexture.GetContent(web); web.Dispose(); string BaseCodePath = System.IO.Path.Combine(Application.persistentDataPath, "qrcode"); if (t != null) { byte[] bts = t.EncodeToPNG();//主要在 string PathName = BaseCodePath; if (!Directory.Exists(PathName)) { Directory.CreateDirectory(PathName); } File.WriteAllBytes(PathName + "/" + userId.ToString() + ".png", bts); }
二、加载图片为Texture2D
string BaseCodePath = System.IO.Path.Combine(Application.persistentDataPath, "qrcode"); string filePath = BaseCodePath + "/" + userId.ToString() + ".png"; if (File.Exists(filePath)) { byte[] bts = File.ReadAllBytes(filePath); Texture2D t = new Texture2D(500, 500);//宽和高 t.LoadImage(bts);//主要在这里 Code.mainTexture = t; }