Version: 2021.1

AssetPostprocessor.OnPostprocessTexture(Texture2D)

切换到手册

描述

将此函数添加到一个子类中,以在纹理刚完成导入之前获取通知。

可以选择压缩纹理并保存到磁盘。

此时,选择压缩格式已为时已晚,不过仍可以 使用 texture.Compress 来压缩纹理。但不建议这么做,且在编辑器中也不会显示 压缩格式。如果需要根据文件名或纹理的其他属性更改压缩格式, 请使用 OnPreprocessTexture。

注意,应避免以这种方式修改 TextureImporter 设置,因为这对当前导入的纹理无效,但会在下次导入纹理时应用。这种行为具有不确定性,因此不建议使用。

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 mipmap level, you can also // modify only the pixels in the highest mip level. And then simply use // texture.Apply(true); to generate lower mip levels. } }