First we need to create CompositeGenerator. You can do it through the context menu
Or through drop down menu
Now we need to select source texture
Let’s set compression settings to extreme values
Note that Png Quality set to 1 and not 0. 0 means single solid color, where we’d prefer at least two: full transparency and full opacity.
After pressing “Generate” button you’ll see several changes:
- Folder “Resources” has been generated beside of Generator object.
- Preview became available.
- Reference on Last Generated composite texture appeared.
If you click on Last Generated reference you’ll see the location of all the generated files.
Now let’s create object with sprite that is a part of source texture
and hit play button
You should notice compression artifacts on the sprite. Also you should notice that original sprite was replaced with composite sprite and that there is a new object on the scene - “SpriteReplacer”, which in fact causes such change.
But as soon as you exit play mode everything restores. So you can work with native sprites and set up compression independently. Of course it only works with objects that not instantiated manually. How to work with CT at runtime see docs.
Disable unused alpha channel for background images
Or in contra remove RGB component from glyphs and fonts.
One bit png alpha + jpeg RGB is a classic combination
but feel free to experiment. For example, if there is a lot of smooth/gradient transparency in the texture, Jpg+jpg can be a choice.
Note that in Png Color Reduction mode with quality level lower than 5, CT uses quantization algorithm that helps to extremely reduce number of colors with admissible result.
On every build CT adds SpriteReplacer object to every scene. On start SpriteReplacer tries to find SpriteRenderer components and UI.Image components and replace sprites on their respective CT analogs.
Of course it only works with objects that not instantiated manually. How to work with CT at runtime see further.
After you press “Generate” button, CompositeGenerator generates multiple files
in relative path /Resources/Composites. You can check their location by clicking “Last Generated” field.
These files are:
<Name>CMPA - alpha component file
<Name>CMPRGB - RGB component file
<Name>CMP - Compound asset. It contains several objects: CompositeTexture and CompositeSprites
CompositeTexture is a scriptable object that contains all the settings for texture generation and composes components at runtime.
You can access generated texture at runtime with such code
CompositeSprite is a scriptable object, composite analog of a regular sprite. Contains all the settings for sprite generation at runtime.
To access generated sprite at runtime you can use code like this
Not too difficult as well.
Q: Why CompositeGenerator and CompositeTexture are separate objects?
A: Composite generators are heavy utility objects that used only for generation of CompositeTextures and component files inside the editor. CompositeTexture on the other hand contains actual data needed for runtime texture generation. Note that CompositeTexture objects are located in the Resources folder, that means they are always included in the build. Note also that CompositeTexture objects don’t have back references to the CompositeGenerator objects, that means Unity strips heavy generators as unused assets during the build.