Skip to content

Instantly share code, notes, and snippets.

@tayl0r
Created September 20, 2019 19:10
Show Gist options
  • Save tayl0r/e2c463baede1c3cc3270e5187592c451 to your computer and use it in GitHub Desktop.
Save tayl0r/e2c463baede1c3cc3270e5187592c451 to your computer and use it in GitHub Desktop.
#if !UNITY_EDITOR || FCLOG
using Debug = FC.Debug;
#else
using Debug = UnityEngine.Debug;
#endif
using UnityEditor;
using UnityEngine;
public class FCTextureImportProcessor : AssetPostprocessor {
// disabling this because it runs every time you Apply changes to a texture
//private void OnPreprocessTexture() {
// var imp = assetImporter as TextureImporter;
// SetCompressed(imp);
//}
[MenuItem("Assets/Import/Set Texture Compressed 4096", false, 10000)]
static void SetCompressedMenu4096() {
SetCompressedMenu(4096);
}
[MenuItem("Assets/Import/Set Texture Compressed 2048", false, 10000)]
static void SetCompressedMenu2048() {
SetCompressedMenu(2048);
}
[MenuItem("Assets/Import/Set Texture Uncompressed 4096", false, 10000)]
static void SetUnCompressedMenu4096() {
SetUnCompressedMenu(4096);
}
[MenuItem("Assets/Import/Set Texture Uncompressed 2048", false, 10000)]
static void SetUnCompressedMenu2048() {
SetUnCompressedMenu(2048);
}
static void SetCompressedMenu(int size) {
var objs = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
foreach (var obj in objs) {
var path = AssetDatabase.GetAssetPath(obj);
var imp = AssetImporter.GetAtPath(path);
var tImp = imp as TextureImporter;
SetCompressed(tImp, size);
tImp.SaveAndReimport();
}
}
static void SetUnCompressedMenu(int size) {
var objs = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
foreach (var obj in objs) {
var path = AssetDatabase.GetAssetPath(obj);
var imp = AssetImporter.GetAtPath(path);
var tImp = imp as TextureImporter;
SetUncompressed(tImp, size);
tImp.SaveAndReimport();
}
}
public static void SetCompressed(TextureImporter imp, int textureSize) {
imp.textureType = TextureImporterType.Sprite;
imp.mipmapEnabled = false;
imp.npotScale = TextureImporterNPOTScale.None;
imp.maxTextureSize = textureSize;
imp.textureCompression = TextureImporterCompression.CompressedHQ;
imp.crunchedCompression = true;
imp.compressionQuality = 100;
var android = imp.GetPlatformTextureSettings("Android");
android.overridden = true;
android.format = TextureImporterFormat.ETC2_RGBA8Crunched;
android.maxTextureSize = textureSize;
android.compressionQuality = 100;
android.crunchedCompression = true;
android.androidETC2FallbackOverride = AndroidETC2FallbackOverride.UseBuildSettings;
imp.SetPlatformTextureSettings(android);
var iphone = imp.GetPlatformTextureSettings("iPhone");
iphone.overridden = true;
iphone.format = TextureImporterFormat.ETC2_RGBA8Crunched;
iphone.maxTextureSize = textureSize;
iphone.compressionQuality = 100;
iphone.crunchedCompression = true;
imp.SetPlatformTextureSettings(iphone);
Debug.Log("FCTextureImportProcessor.SetCompressed: processed '" + imp.assetPath + "'.");
}
public static void SetUncompressed(TextureImporter imp, int textureSize) {
imp.textureType = TextureImporterType.Sprite;
imp.mipmapEnabled = false;
imp.npotScale = TextureImporterNPOTScale.None;
imp.maxTextureSize = textureSize;
imp.textureCompression = TextureImporterCompression.Uncompressed;
imp.ClearPlatformTextureSettings("Android");
imp.ClearPlatformTextureSettings("iPhone");
Debug.Log("FCTextureImportProcessor.SetUncompressed: processed '" + imp.assetPath + "'.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment