My basic question is: Can I use Texture2D.Compress() on iOS at runtime even though the docs state Compress uses DXT, which iOS doesn’t support. Unfortunately I cannot test because I don’t have a Mac right now so I cannot generate an iOS build…
-so I was hoping someone who does would know…
Longer Explanation: I am making something where users will generate their own textures at run time and then send them to other users of the app.
I am looking into more performant ways of doing this, using the new JsonUtility, than the standard method of EncodeToPNG () > upload to server > download from server > decode from PNG because decoding the png to a Texture2D seems to be really slow.
So, with a specialized class that holds the data from GetRawTextureData() I can use JsonUtility.ToJson to save that to a file, and generating a Texture2D from the data in that file is virtually instant. The problem is that the file is much bigger (even zipped its about 3x the size) and this is obviously bad for the user downloading the file.
However calling ‘Compress(true)’ on the texture before GetRawTextureData and then saving using the method above results in a file that is much smaller and still seems to be fast. And this works fine on Desktop and Android.
BUT I notice in the documentation that Texture2D.Compress() method uses DXT which is not supported by iOS.
What is not clear is whether you can still call the ‘Compress’ method on iOS, whether it will actually do anything, and what will happen when an iOS user downloads a raw data file that has been compressed and the app attempts to use the data to SetRawTextureData in a new Texture2D?
Does anyone who does generally build for iOS have any ideas?
Actually I think I answered my own question. I just found the files generated on Android (Samsung Galaxy S5) and they actually dont appear to be getting compressed after all. So I guess Texture2D.Compress maybe doesn’t do anything on Android or iOS…
I’m sorry, I fail spectacularly! I will reduce (or increase?) caffeine consumption immediately.
To make up for my fail, I have conducted the test for you on an iPod Touch 5th Generation.
I found that on iOS the size of the image does NOT change. However, it does change in the editor, even when build target is IOS.
Below is my testing script. Let me know if I misunderstood your steps:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class CompressDXT : MonoBehaviour
{
public Texture2D texture;
public Text output;
void Start ()
{
output.text = "Output log:\n\n";
texture = Instantiate<Texture2D>(texture);
byte[] precomp = texture.GetRawTextureData();
output.text += System.String.Format ( "Precomp: {0} bytes\n\n", precomp.Length);
texture.Compress (false);
byte[] postcomp = texture.GetRawTextureData();
output.text += System.String.Format ( "Postcomp: {0} bytes\n\n", postcomp.Length);
}
}
In the docs, they say this: “If the graphics card does not support compression or the texture is already in compressed format, then Compress will do nothing.”
I am currently using Unity 5.2.4f1. I am applying the Texture2D.Compress(true)
to a texture I am loading from a local file and it has definitely reduced the size of the loaded
file a lot.
I think the problem with your codes is that after you apply the Texture2d.Compress(bool);
you need to call Texture2D.Apply();
If you don’t do this it will not have any effect in memory.
Please try it out and let me know. I am glad if it is of help to you guys.
PVRTC encoding is very CPU-intensive; even if it worked it would be not particularly usable. Much slower than encoding/decoding PNG, and attempting to avoid that was the point of this topic. (Although PNG encoding is actually not very slow.)
It’s an old post, but there is some recent changes on that point in Unity 2021
Texture2D.Compress is now working on many more platforms, including Android and iOS.
According to the documentation: “On Android, iOS and tvOS, this will compress the texture to the ETC/EAC family of formats.”