将此函数添加到子类中,以便在 Unity 压缩 Texture2DArray 之前,获得有关其导入完成的通知。
您无法在此处选择压缩格式。如果您想根据文件名或纹理的其他属性更改压缩格式,请使用 AssetPostprocessor.OnPreprocessTexture。
但是,如果您以这种方式修改 TextureImporter 设置,它对 Unity 当前正在导入的纹理没有任何影响,但它会在 Unity 下次导入此纹理时应用。这会导致不可预测的结果。
using UnityEditor; using UnityEngine; using System.Collections;
// Postprocesses all 2D texture arrays that are placed in a folder // "invert color" to have their colors inverted. public class InvertColor : AssetPostprocessor { void OnPostprocessTexture2DArray(Texture2DArray 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 slice = 0; slice < texture.depth; ++slice) { 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, slice, m); } }
// Instead of setting pixels for each mipmap level, you can modify // the pixels in the highest mipmap then use texture.Apply(true); // to generate lower mip levels. } }