简体   繁体   English

从服务器加载时,使Unity中的Texture2D可读

[英]Making a Texture2D readable in Unity when loading from server

I'm using DownloadHandlerTexture.GetContent to get my texture: 我正在使用DownloadHandlerTexture.GetContent来获取纹理:

www = UnityWebRequest.GetTexture("http://www.example.com/loadTex/?tag=" + tag);
www.SetRequestHeader("Accept", "image/*");
async = www.Send();
while (!async.isDone)
    yield return null;

if (www.isError) {
    Debug.Log(www.error);
} else {
    yield return null;
    tex = DownloadHandlerTexture.GetContent(www);
}

After loading I would like to cache it to a file so I do: 加载后,我想将其缓存到文件中,以便执行以下操作:

byte[] pic = tex.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/art/" + tag + ".png", pic);

At this point I get the exception: 在这一点上,我得到了例外:

UnityException: Texture '' is not readable, the texture memory can not be accessed from 
scripts. You can make the texture readable in the Texture Import Settings.

I'm thinking that I need to make it readable somehow. 我认为我需要以某种方式使其可读。 I googled it, but the only answers I get is to how to make it readable through the editor. 我用谷歌搜索,但是唯一的答案是如何通过编辑器使其可读。

Just wanted to point out that UnityWebRequest.GetTexture can take two parameters: the url and a bool to make the texture readable or not :) https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.GetTexture.html 只是想指出,UnityWebRequest.GetTexture可以采用两个参数:url和使纹理可读或不可读的布尔值:) https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.GetTexture.html

So before using DownloadHandlerTexture.GetContent to get the texture in the request result. 因此,在使用DownloadHandlerTexture.GetContent获取请求结果中的纹理之前。 Simply call UnityWebRequest.GetTexture(url, false); 只需调用UnityWebRequest.GetTexture(url,false); so the texture downloaded become readable. 因此下载的纹理变得可读。

PS: Be careful, before Unity 5.6, the bool nonReadable is inverted. PS:请注意,在Unity 5.6之前,布尔nonReadable已反转。 They should have fixed it starting 5.6 (according to release notes). 他们应该从5.6(根据发行说明)开始对其进行修复。

To make your texture readable: 使纹理可读:

  • Before 5.6: UnityWebRequest.GetTexture(url, true) 5.6之前的版本:UnityWebRequest.GetTexture(url,true)
  • After 5.6: UnityWebRequest.GetTexture(url, false) 5.6之后:UnityWebRequest.GetTexture(url,false)

You could store the data from UnityWebRequest. 您可以存储来自UnityWebRequest的数据。 What you are dong ia bit of useless for that purpose. 您为此付出的努力毫无用处。 You turn the result into a texture to convert the texture back to byte []. 您将结果转换为纹理,以将纹理转换回字节[]。

byte[] bytes = www.uploadHandler.data;
File.WriteAllBytes(Application.persistentDataPath + "/art" + tag, data);

I'd guess this should do. 我猜这应该做。 No need for the .png extension as you store a raw array of bytes. 存储原始字节数组时,无需.png扩展名。 You will then get the array and create a texture out of it: 然后,您将获得数组并从中创建纹理:

byte [] bytes = File.ReadAllBytes(Application.persistentDataPath + "/art" + tag); 
Texture2D texture = new Texture2D(4,4,TextureFormat.RGBA32, false);
texture.LoadImage(bytes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM