将此函数添加到子类,以便在 Unity 压缩前收到有关 texture2D 已完成导入的通知。
你此时无法选择压缩格式。如果要根据文件名或纹理的其他属性更改压缩格式,则使用AssetPostprocessor.OnPreprocessTexture。
但是,如果你以这种方式修改 TextureImporter 设置,这对 Unity 当前正在导入的纹理没有影响,但会在 Unity 下次导入此纹理时应用。这会导致不可预测的结果。
using UnityEditor; using UnityEngine; using System.Collections;
// Postprocesses all textures that are placed in a folder // "invert color" to have their colors inverted. public class InvertColor : AssetPostprocessor { void OnPostprocessTexture(Texture2D texture) { // Only post process textures if they are in a folder // "invert color" or a sub folder of it. string lowerCaseAssetPath = assetPath.ToLower(); if (lowerCaseAssetPath.IndexOf("/invert color/") == -1) return;
for (int m = 0; m < texture.mipmapCount; m++) { Color[] c = texture.GetPixels(m);
for (int i = 0; i < c.Length; i++) { c[i].r = 1 - c[i].r; c[i].g = 1 - c[i].g; c[i].b = 1 - c[i].b; } texture.SetPixels(c, m); } // Instead of setting pixels for each mip map level, you can modify // the pixels in the highest mipmap then use texture.Apply(true); // to generate lower mip levels. } }