借鉴大神的源码的基础上创造而成,此处积累备份
https://www.cnblogs.com/leesymbol/p/7600452.html
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections;
using System.Collections.Generic;
public class TextureImportChanging : EditorWindow {
enum MaxSize {
Size_32 = 32,
Size_64 = 64,
Size_128 = 128,
Size_256 = 256,
Size_512 = 512,
Size_1024 = 1024,
Size_2048 = 2048,
Size_4096 = 4096,
Size_8192 = 8192,
}
// ----------------------------------------------------------------------------
TextureImporterType textureType = TextureImporterType.Sprite;
TextureImporterFormat textureFormat = TextureImporterFormat.Automatic;
MaxSize textureSize = MaxSize.Size_1024;
TextureImporterCompression textureCompression = TextureImporterCompression.Uncompressed;
bool ifAllowsAlphaSplitting = true;
bool ifMipmapEnabled = false;
float secs = 10.0f;
double startVal = 0;
float progress = 0f;
static TextureImportChanging window;
[@MenuItem("Tools/Texture Settings")]
private static void Init() {
Rect wr = new Rect(0, 0, 400, 400);
window = (TextureImportChanging)EditorWindow.GetWindowWithRect(typeof(TextureImportChanging), wr, false, "图片格式设置");
window.Show();
}
private void OnGUI() {
EditorGUILayout.Space();
EditorGUILayout.HelpBox("设置选中图片或选中路径下的图片属性", MessageType.Info);
EditorGUILayout.Space();
textureType = (TextureImporterType)EditorGUILayout.EnumPopup("类型:", textureType);
textureFormat = (TextureImporterFormat)EditorGUILayout.EnumPopup("格式:", textureFormat);
textureSize = (MaxSize)EditorGUILayout.EnumPopup("尺寸:", textureSize);
textureCompression = (TextureImporterCompression)EditorGUILayout.EnumPopup("压缩:", textureCompression);
ifAllowsAlphaSplitting = EditorGUILayout.Toggle("是否允许透明分离:", ifAllowsAlphaSplitting);
ifMipmapEnabled = EditorGUILayout.Toggle("是否允许Mipmap:", ifMipmapEnabled);
EditorGUILayout.Space();
if (GUILayout.Button("设置")) {
TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();
t.allowsAlphaSplitting = ifAllowsAlphaSplitting;
t.format = textureFormat;
t.maxTextureSize = (int)textureSize;
t.textureCompression = textureCompression;
SelectedChangeTextureFormatSettings(t, textureType,GetSelectedTextures());
}
//if (GUILayout.Button("一键设置(Textures目录)")) {
// TextureImporterPlatformSettings t = new TextureImporterPlatformSettings();
// t.allowsAlphaSplitting = ifAllowsAlphaSplitting;
// t.format = textureFormat;
// t.maxTextureSize = (int)textureSize;
// t.textureCompression = textureCompression;
// SelectedChangeTextureFormatSettings(t, textureType,GetTexture());
//}
}
static void SelectedChangeTextureFormatSettings(TextureImporterPlatformSettings _t, TextureImporterType _type,Object[] arr) {
Object[] textures = arr;
if (window == null)
Init();
if (textures != null) {
if (textures.Length < 1) {
window.ShowNotification(new GUIContent("找不到图片!"));
return;
}
} else {
window.ShowNotification(new GUIContent("请选中图片或路径!"));
return;
}
Selection.objects = new Object[0];
int i = 0;
foreach (Texture2D texture in textures) {
string path = AssetDatabase.GetAssetPath(texture);
//Debug.Log("path: " + path);
string[] pathArr = path.Split('/');
TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
textureImporter.textureType = _type;
var platFormat = textureImporter.GetDefaultPlatformTextureSettings();
platFormat.format = _t.format;
textureImporter.maxTextureSize = _t.maxTextureSize;
textureImporter.allowAlphaSplitting = _t.allowsAlphaSplitting;
textureImporter.wrapMode = TextureWrapMode.Clamp;
textureImporter.filterMode = FilterMode.Bilinear;
textureImporter.spritePackingTag = pathArr[pathArr.Length - 2];
ShowProgress((float)i / (float)textures.Length, textures.Length, i);
i++;
AssetDatabase.ImportAsset(path);
}
AssetDatabase.Refresh();
EditorUtility.ClearProgressBar();
textures = null;
}
public static void ShowProgress(float val, int total, int cur) {
EditorUtility.DisplayProgressBar("设置图片中...", string.Format("请稍等({0}/{1}) ", cur, total), val);
}
static Object[] GetSelectedTextures() {
return Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
}
static Object[] GetTexture() {
List
导入时自动设置图片格式代码
using UnityEngine;
using UnityEditor;
using System.Collections;
class TextureModifier : AssetPostprocessor {
private static string m_Textures_ = "Assets/Textures/";
void OnPreprocessTexture() {
if (!assetPath.StartsWith(m_Textures_,System.StringComparison.Ordinal))
return;
TextureImporterFormat textureFormat = TextureImporterFormat.Automatic;
var textureImporter = (assetImporter as TextureImporter);
string path = assetPath;
string[] pathArr = path.Split('/');
textureImporter.textureType = TextureImporterType.Sprite;
TextureImporterPlatformSettings platFormat = textureImporter.GetDefaultPlatformTextureSettings();
platFormat.format = textureFormat;
textureImporter.maxTextureSize = 1024;
textureImporter.allowAlphaSplitting = false;
textureImporter.wrapMode = TextureWrapMode.Clamp;
textureImporter.filterMode = FilterMode.Bilinear;
textureImporter.spritePackingTag = pathArr[pathArr.Length - 2];
}
/*
void OnPostprocessTexture(Texture2D texture) {
var texw = texture.width;
var texh = texture.height;
var pixels = texture.GetPixels();
var offs = 0;
var k1Per31 = 1.0f / 31.0f;
var k1Per32 = 1.0f / 32.0f;
var k5Per32 = 5.0f / 32.0f;
var k11Per32 = 11.0f / 32.0f;
var k15Per32 = 15.0f / 32.0f;
var k1Per63 = 1.0f / 63.0f;
var k3Per64 = 3.0f / 64.0f;
var k11Per64 = 11.0f / 64.0f;
var k21Per64 = 21.0f / 64.0f;
var k29Per64 = 29.0f / 64.0f;
var k_r = 32; //R&B压缩到5位,所以取2的5次方
var k_g = 64; //G压缩到6位,所以取2的6次方
for (var y = 0; y < texh; y++) {
for (var x = 0; x < texw; x++) {
float r = pixels[offs].r;
float g = pixels[offs].g;
float b = pixels[offs].b;
var r2 = Mathf.Clamp01(Mathf.Floor(r * k_r) * k1Per31);
var g2 = Mathf.Clamp01(Mathf.Floor(g * k_g) * k1Per63);
var b2 = Mathf.Clamp01(Mathf.Floor(b * k_r) * k1Per31);
var re = r - r2;
var ge = g - g2;
var be = b - b2;
var n1 = offs + 1;
var n2 = offs + texw - 1;
var n3 = offs + texw;
var n4 = offs + texw + 1;
if (x < texw - 1) {
pixels[n1].r += re * k15Per32;
pixels[n1].g += ge * k29Per64;
pixels[n1].b += be * k15Per32;
}
if (y < texh - 1) {
pixels[n3].r += re * k11Per32;
pixels[n3].g += ge * k21Per64;
pixels[n3].b += be * k11Per32;
if (x > 0) {
pixels[n2].r += re * k5Per32;
pixels[n2].g += ge * k11Per64;
pixels[n2].b += be * k5Per32;
}
if (x < texw - 1) {
pixels[n4].r += re * k1Per32;
pixels[n4].g += ge * k3Per64;
pixels[n4].b += be * k1Per32;
}
}
pixels[offs].r = r2;
pixels[offs].g = g2;
pixels[offs].b = b2;
offs++;
}
}
texture.SetPixels(pixels);
EditorUtility.CompressTexture(texture, TextureFormat.RGB565, TextureCompressionQuality.Best);
}
*/
}