AssetPostprocessor.OnPostprocessTexture(Texture2D)

切换到手册

描述

Add this function in a subclass to get a notification when a texture has completed importing just before.

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

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

如果修改了纹理(如下例所示),则它需要处于可读状态。 必须从 Editor 的 Importer 设置中(勾选 Read/Write Enabled)或 OnPreprocessTexture 函数中 将 isReadable 标志设置为 True。如果纹理在运行时不必处于可读状态,请使用 texture.Apply(true, true) 来更新 Mipmap,并在运行时将纹理标记为不可读。

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