unityエディタバッチ処理ピクチャフォーマットunity 2017バージョン


大神のソースコードを参考にして作成し、バックアップを蓄積します.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 list = new List();
        List<string> paths = new List<string>();
        paths.Clear();
        list.Clear();
        string targetPath = Application.dataPath + "/Textures/Begin/";
        if (Directory.Exists(targetPath)) {
            foreach (string path in Directory.GetFiles(targetPath,"*.png", SearchOption.AllDirectories)) {
                paths.Add(path);
                Debug.Log( " = " + path);

                Object oj = AssetDatabase.LoadAssetAtPath("Textures/Begin/bg_input.png", typeof(Object));
                list.Add(oj);
            }
        }    

        return list.ToArray();
    }
    void OnInspectorUpdate() {
        Repaint();
    }

}

インポート時に画像フォーマットコードを自動的に設定する
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);
    }
*/
}