前情提要:
最近在做动态图集,方案:在需要某格式图集的时候,初始化1张Texture,并将同格式的放到这个格式对应的Texture中去。
由于我们组件用的都是基于Image,故使用CopyTexture
,并踩了个坑,这个API(https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Graphics.CopyTexture.html
),etc2及一些压缩格式都不能调用。如果把这些项目中的etc2图重新变回rgba,那包体中图片的大小估计就得升到4倍…故舍弃了此动态图集方案,fallback到之前的按文件夹图集。
按文件夹(按功能)图集的问题是,有的图集在common中,有的图集在main中,会造成不同图集图片的穿插而导致dc高。
坑记录:
CopyTexture warning etc2
Graphics.CopyTexture with a region will not copy readable texture data for compressed formats (source texture format 47)
https://forum.unity.com/threads/graphics-copytexture-prints-out-warnings-even-though-it-should-not.431720/
https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Graphics.CopyTexture.html
ConvertTexture
当中想到,如果etc2无法复制,是否可以runtime转成rgba放到图集中呢,结果仍然是卡住了。
realSrc.SetPixels(srcTex.GetPixels());
r/w问题 需要图片勾上r/w,代价内存翻倍,舍之。UnityException: Texture 'xxx' is not readable, the texture memory can not be accessed from scripts.
Graphics.ConvertTexture
似乎没有成功? 转换了再copy仍然会报那个copy的warningLoadImage
1
2var imgBytes = srcTex.GetRawTextureData();
realSrc.LoadImage(imgBytes);Unity会报错:
loadrawtexturedata: not enough data provided (will result in overread).
猜测原因还是出来的btyes其实还是etc2压完的bytes,无法直接复制到rgba图片。
可能的解决方案
1 自己去写etc2 -> bytes or etc2 -> rgba 的方法 跟下面文章做的事情相似
https://forum.unity.com/threads/loading-crunched-textures-at-runtime-in-2017-3.556192/
2 尝试用Texture2D.PackTextures
更新:这个也8行
https://docs.unity3d.com/2018.4/Documentation/ScriptReference/Texture2D.PackTextures.html
ref
Texture源码:
https://github.com/Unity-Technologies/UnityCsReference/blob/2018.4/Runtime/Export/Texture.cs
https://github.com/Unity-Technologies/UnityCsReference/blob/2018.4/Runtime/Export/Texture.bindings.cs
Unity图片import格式Manual:
https://docs.unity3d.com/Manual/class-TextureImporterOverride.html