- Home /
Compressing in OnPostprocessTexture yields "Not yet compressed"
I'm trying to create an editor script that can take a selected texture of any compression setting, manipulate it, and then compress it again. This becomes a problem since I have to set the selected texture to a format that can be edited (SetPixels), like RGBA32, etc.
The result works, but Unity tells me that the texture is "Not yet compressed" when I later select it. It IS however compressed when I look at its size when using it on the scene.
Does anyone have an explanation for this? Is it just the Unity Editor thats plain wrong? Or am I doing something wrong?
This is a pseudo-version of my editor extension:
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public static class TextureManiplator {
public static List<Job> Jobs = new List<Job>();
[System.Serializable]
public class Job {
public string TargetPath;
public Color TargetColor;
public TextureImporterSettings OriginalTextureSettings;
public TextureFormat OriginalTextureFormat;
}
public static void CreateJob(Texture2D texture, Color color) {
// Create and save job
var j = new Job();
j.TargetPath = AssetDatabase.GetAssetPath(texture);
j.TargetColor = color;
j.OriginalTextureFormat = texture.format;
Jobs.Add(j);
AssetDatabase.ImportAsset(AssetDatabase.GetAssetPath(texture)); // Force texture to reimport
}
[MenuItem("Texture Manipulator/Red")]
public static void Menu_Red() {
foreach (Texture2D texture in Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets)) {
CreateJob(texture, Color.red);
}
}
class Postprocessing : AssetPostprocessor {
void OnPreprocessTexture() {
foreach(TextureManiplator.Job j in TextureManiplator.Jobs) {
if(j.TargetPath == assetPath) {
var importer = assetImporter as TextureImporter;
// Save original settings
j.OriginalTextureSettings = new TextureImporterSettings();
importer.ReadTextureSettings(j.OriginalTextureSettings);
// Make sure the texture is readable
importer.textureFormat = TextureImporterFormat.ARGB32;
importer.isReadable = true;
}
}
}
void OnPostprocessTexture (Texture2D texture) {
foreach(TextureManiplator.Job j in TextureManiplator.Jobs) {
if(j.TargetPath == assetPath) {
// Mess with colors (This is not what I'm actually doing, but its not important
for (var m = 0; m < texture.mipmapCount; m++) {
var c = texture.GetPixels(m);
for (var i = 0 ;i < c.Length; i++) {
c = j.TargetColor;
}
texture.SetPixels(c, m);
}
// Restore original settings
var importer = assetImporter as TextureImporter;
importer.SetTextureSettings(j.OriginalTextureSettings);
// Apply and Compress texture
texture.Apply(false, j.OriginalTextureSettings.readable);
EditorUtility.CompressTexture(texture, j.OriginalTextureFormat, j.OriginalTextureSettings.compressionQuality);
TextureManiplator.Jobs.Remove(j); //Remove job
return;
}
}
}
}
}
I am currently dealing with this now in unity 4.6. Was a solution found?
Answer by Tutanhomon · May 02, 2014 at 12:32 PM
you must go to preferences and check "Compress assets on import" checkbox, this will cause assets reimport and all textures will be comressed, showing their compressed size instead of "Not yet compressed" message.
Your answer
Follow this Question
Related Questions
Load image onto a Plane in the editor using C# 0 Answers
Graphics.DrawTexture in OnPostprocessTexture 1 Answer
texture2d field in a custom editor window 3 Answers
EditorWindow loses Texture2D reference on scene change: how to handle? 4 Answers
Make a PropertyField() for a Texture2D SerializedProperty looking like an ObjectField() 2 Answers