从服务器加载时使 Texture2D 在 Unity 中可读
Making a Texture2D readable in Unity when loading from server
我正在使用 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);
}
加载后我想将它缓存到一个文件中,所以我这样做:
byte[] pic = tex.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/art/" + tag + ".png", pic);
此时我得到异常:
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.
我在想我需要以某种方式让它变得可读。我用谷歌搜索了它,但我得到的唯一答案是如何让它通过编辑器可读。
您可以存储来自 UnityWebRequest 的数据。你是什么东西,我对那个目的有点无用。您将结果转换为纹理以将纹理转换回字节 [].
byte[] bytes = www.uploadHandler.data;
File.WriteAllBytes(Application.persistentDataPath + "/art" + tag, data);
我想这应该可以。存储原始字节数组时不需要 .png 扩展名。然后您将获取数组并从中创建纹理:
byte [] bytes = File.ReadAllBytes(Application.persistentDataPath + "/art" + tag);
Texture2D texture = new Texture2D(4,4,TextureFormat.RGBA32, false);
texture.LoadImage(bytes);
只是想指出 UnityWebRequest.GetTexture 可以采用两个参数:url 和一个布尔值来决定纹理是否可读:)
https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.GetTexture.html
所以在使用DownloadHandlerTexture.GetContent获取请求结果中的纹理之前。只需调用 UnityWebRequest.GetTexture(url, false);所以下载的纹理变得可读。
PS:注意,在Unity 5.6之前,bool nonReadable被取反了。他们应该从 5.6 开始修复它(根据发行说明)。
为了使您的纹理可读:
- 5.6 之前:
UnityWebRequest.GetTexture(url, 真)
- 5.6 之后:
UnityWebRequest.GetTexture(url, 假)
我正在使用 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);
}
加载后我想将它缓存到一个文件中,所以我这样做:
byte[] pic = tex.EncodeToPNG();
File.WriteAllBytes(Application.persistentDataPath + "/art/" + tag + ".png", pic);
此时我得到异常:
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.
我在想我需要以某种方式让它变得可读。我用谷歌搜索了它,但我得到的唯一答案是如何让它通过编辑器可读。
您可以存储来自 UnityWebRequest 的数据。你是什么东西,我对那个目的有点无用。您将结果转换为纹理以将纹理转换回字节 [].
byte[] bytes = www.uploadHandler.data;
File.WriteAllBytes(Application.persistentDataPath + "/art" + tag, data);
我想这应该可以。存储原始字节数组时不需要 .png 扩展名。然后您将获取数组并从中创建纹理:
byte [] bytes = File.ReadAllBytes(Application.persistentDataPath + "/art" + tag);
Texture2D texture = new Texture2D(4,4,TextureFormat.RGBA32, false);
texture.LoadImage(bytes);
只是想指出 UnityWebRequest.GetTexture 可以采用两个参数:url 和一个布尔值来决定纹理是否可读:) https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.GetTexture.html
所以在使用DownloadHandlerTexture.GetContent获取请求结果中的纹理之前。只需调用 UnityWebRequest.GetTexture(url, false);所以下载的纹理变得可读。
PS:注意,在Unity 5.6之前,bool nonReadable被取反了。他们应该从 5.6 开始修复它(根据发行说明)。
为了使您的纹理可读:
- 5.6 之前: UnityWebRequest.GetTexture(url, 真)
- 5.6 之后: UnityWebRequest.GetTexture(url, 假)