I could not find a fixes, so I made a custom texture importer using zip. (Zip files should be included to “StreamingAssets” folder for including to a build)
Maybe it will be helpfull.
public static async Task<Texture2D> ReadTextureFromFileAsync(string pathToFileWithoutExtension, bool linear = true, FilterMode filterMode = FilterMode.Bilinear, TextureWrapMode wrapMode = TextureWrapMode.Repeat)
{
Texture2D tex;
if (!File.Exists(pathToFileWithoutExtension + ".gz"))
{
Debug.LogError("Can't find the file: " + pathToFileWithoutExtension);
return null;
}
try
{
using (var fileStream = File.Open(pathToFileWithoutExtension + ".gz", FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (GZipStream gzip = new GZipStream(fileStream, CompressionMode.Decompress))
{
using (MemoryStream stream = new MemoryStream())
{
await gzip.CopyToAsync(stream);
var rawTextureDataWithInfo = new byte[stream.Length];
stream.Seek(0, SeekOrigin.Begin);
await stream.ReadAsync(rawTextureDataWithInfo, 0, rawTextureDataWithInfo.Length);
{
var format = (TextureFormat)BitConverter.ToInt32(rawTextureDataWithInfo, 0);
int width = BitConverter.ToInt32(rawTextureDataWithInfo, 4);
int height = BitConverter.ToInt32(rawTextureDataWithInfo, 8);
var rawTextureData = new byte[rawTextureDataWithInfo.Length - 12];
Array.Copy(rawTextureDataWithInfo, 12, rawTextureData, 0, rawTextureData.Length);
//var gFormat = GraphicsFormatUtility.GetGraphicsFormat(format, false);
tex = new Texture2D(width, height, format, false, true);
tex.filterMode = filterMode;
tex.wrapMode = wrapMode;
tex.LoadRawTextureData(rawTextureData);
tex.Apply();
}
}
}
fileStream.Close();
}
return tex;
}
catch (Exception e)
{
Debug.LogError("ReadTextureFromFileAsync error: " + e.Message);
return null;
}
}
public static void SaveTextureToFile(this Texture2D tex, string pathToFileWithoutExtension)
{
var directory = Path.GetDirectoryName(pathToFileWithoutExtension);
if (directory == null)
{
Debug.LogError("Can't find directory: " + pathToFileWithoutExtension);
return;
}
if (!Directory.Exists(directory)) Directory.CreateDirectory(directory);
using (var fileToCompress = File.Create(pathToFileWithoutExtension + ".gz"))
{
int w = tex.width;
int h = tex.height;
var rawTextureData = tex.GetRawTextureData();
var textureInfo = new List<byte>();
textureInfo.AddRange(BitConverter.GetBytes((uint)tex.format));
textureInfo.AddRange(BitConverter.GetBytes(w));
textureInfo.AddRange(BitConverter.GetBytes(h));
rawTextureData = Combine(textureInfo.ToArray(), rawTextureData);
using (GZipStream compressionStream = new GZipStream(fileToCompress, CompressionMode.Compress))
{
compressionStream.Write(rawTextureData, 0, rawTextureData.Length);
}
fileToCompress.Close();
}
}
public static void SaveRenderTextureToFile(this RenderTexture rt, string pathToFileWithoutExtension, TextureFormat targetFormat)
{
#if UNITY_EDITOR
if (rt == null) return;
var currentRT = RenderTexture.active;
RenderTexture.active = rt;
var tex = new Texture2D(rt.width, rt.height, TextureFormat.RGBAFloat, false, true);
tex.ReadPixels(new Rect(0, 0, rt.width, rt.height), 0, 0);
tex.Apply();
UnityEditor.EditorUtility.CompressTexture(tex, targetFormat, UnityEditor.TextureCompressionQuality.Best);
tex.SaveTextureToFile(pathToFileWithoutExtension);
RenderTexture.active = currentRT;
SafeDestroy(tex);
//SafeDestroy(tex2);
#endif
}
Also it may help too
https://forum.unity.com/threads/floating-point-textures.821553