unity中使用www加载网络图片为纹理时,通常会遇到内存占用太高问题。
一般我们会用pvr纹理打包成unity3d格式文件,然后使用www动态加载。
Texture2D.LoadImage 只支持png 或 jpg格式的图片。
如果想直接下载并load pvr格式的文件呢?
使用 Texture2D.LoadRawTextureData
但是不能直接 LoadRawTextureData(www.byte)
因为LoadRawTextureData 要求的参数是只包含RGB 的数据。
我们应该这样 :
WWW www = new WWW("file://test.pvr");
yield return www;
int headerSize = 52;
byte[] buffer = new byte[www.size - headerSize];
System.Buffer.BlockCopy(www.bytes, headerSize, buffer, 0, www.size - headerSize);
Texture2D tex = new Texture2D(512,256,TextureFormat.PVRTC_RGBA4,false,true);
tex.LoadRawTextureData(buffer);
tex.Apply();
因为pvr文件的前52个字节包含的是文件的文件头,要把他去掉。
OK!