天天看點

Unity 圖檔修改

檢測圖檔,修改圖檔格式

using UnityEngine;
using UnityEditor;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
public class TextureCheck  {

    [MenuItem("檢查圖檔/檢查檔案夾貼圖不符合的")]
    static void 檢查是否有真彩貼圖()
    {
        if (Selection.objects.Length != )
        {
            Debug.LogError("選擇一個檔案夾");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[]);
        if (!Directory.Exists(path))
        {
            Debug.LogError("選擇一個檔案夾");
            return;
        }

       // StringBuilder text = new StringBuilder();
        List<string> imageList = new List<string>();
        List<string> errorList = new List<string>();

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        string[] filePath = GetFilePath(Selection.objects);
        foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter == null)
                continue;
            if (textureImporter.textureType == TextureImporterType.Sprite)
                continue;
            //檢視貼圖類型
            if (textureImporter.textureType == TextureImporterType.Image || textureImporter.textureType == TextureImporterType.Cursor)
            {
                Debug.Log("類型不符合:" + assetPath + ",現在類型是:" + textureImporter.textureType);
                errorList.Add("類型不符合: " + ",現在類型是:" + textureImporter.textureType);
                imageList.Add(assetPath);
                continue;
            }
            //檢視是否開啟了minmap
            if (textureImporter.mipmapEnabled)
            {
                Debug.Log( assetPath + ",該圖檔開啟了minmap");
                errorList.Add(",該圖檔開啟了minmap");
                imageList.Add(assetPath);
                continue;
            }
            //檢視壓縮格式
            int maxsize = ;
            TextureImporterFormat textureImporterFormat;
            string androidPlatform = "Android";
            if (textureImporter.GetPlatformTextureSettings(androidPlatform, out maxsize, out textureImporterFormat))
            {
                if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC_RGB4)//不帶alpha通道的
                {
                    Debug.Log("格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat);
                    imageList.Add(assetPath);
                    errorList.Add("格式不符合: " + ",現在格式是:" + textureImporterFormat);
                }
                if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC2_RGBA8)//帶alpha通道的
                {
                    Debug.Log("格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat);
                    imageList.Add(assetPath);
                    errorList.Add("格式不符合: " + ",現在格式是:" + textureImporterFormat);
                }
            }

            //檢視壓縮格式
            int maxsize2 = ;
            TextureImporterFormat textureImporterFormat2;
            string iosPlatform = "iPhone";
            if (textureImporter.GetPlatformTextureSettings(iosPlatform, out maxsize2, out textureImporterFormat2))
            {
                if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat2 != TextureImporterFormat.PVRTC_RGB4)//不帶alpha通道的
                {
                    Debug.Log("IOS格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat2);
                    imageList.Add(assetPath);
                    errorList.Add("IOS格式不符合: " + ",現在格式是:" + textureImporterFormat2);
                }
                if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat2 != TextureImporterFormat.PVRTC_RGBA4)//帶alpha通道的
                {
                    Debug.Log("IOS格式不符合:" + assetPath + ",現在格式是:" + textureImporterFormat2);
                    imageList.Add(assetPath);
                    errorList.Add("IOS格式不符合: " + ",現在格式是:" + textureImporterFormat2);
                }
            }

            //檢視是否是2的次方
            Texture target = AssetDatabase.LoadAssetAtPath<Object>(assetPath) as Texture;
            var type = Types.GetType("UnityEditor.TextureUtil", "UnityEditor.dll");

            MethodInfo[] methodInfos = type.GetMethods();
            foreach (var obj in methodInfos)
                Debug.Log(obj);

            MethodInfo methodInfo = type.GetMethod("IsNonPowerOfTwo", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
            string isNon = methodInfo.Invoke(null, new object[] { target }).ToString();
            if (isNon.Contains("True"))
            {
                imageList.Add(assetPath);
                errorList.Add( "圖檔大小不是2的次幂");
                Debug.Log(assetPath + ",圖檔大小不是2的次幂:" + methodInfo.Invoke(null, new object[] { target }));
            }
        }
        TextureCheckEditorWindow.imageList = imageList;
        TextureCheckEditorWindow.errorList = errorList;
        TextureCheckEditorWindow.ShowWindow();

        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
       // Debug.Log(text.ToString());
    }

    [MenuItem("檢查圖檔/轉換檔案夾内Android的圖檔為etc")]
    static void 轉換Android的圖檔為etc()
    {
        if (Selection.objects.Length != )
        {
            Debug.LogError("選擇一個檔案夾");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[]);
        if (!Directory.Exists(path))
        {
            Debug.Log(path);
            return;
        }

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = ;
        string[] filePath = GetFilePath(Selection.objects);
        foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter == null)
                continue;


            //處理壓縮格式為etc
            int maxsize = ;
            TextureImporterFormat textureImporterFormat;
            string androidPlatform = "Android";
            if (textureImporter.GetPlatformTextureSettings(androidPlatform, out maxsize, out textureImporterFormat))
            {
                if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC_RGB4)//不帶alpha通道的
                {

                    textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.ETC_RGB4);
                    textureImporter.SaveAndReimport();
                    count++;
                }
                if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.ETC2_RGBA8 )//帶alpha通道的
                {

                    textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.ETC2_RGBA8);
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }

            androidPlatform = "iPhone";
            if (textureImporter.GetPlatformTextureSettings(androidPlatform, out maxsize, out textureImporterFormat))
            {
                if (!textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.PVRTC_RGB4)//不帶alpha通道的
                {

                    textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.PVRTC_RGB4);
                    textureImporter.SaveAndReimport();
                    count++;
                }
                if (textureImporter.DoesSourceTextureHaveAlpha() && textureImporterFormat != TextureImporterFormat.PVRTC_RGBA4)//帶alpha通道的
                {

                    textureImporter.SetPlatformTextureSettings(androidPlatform, maxsize, TextureImporterFormat.PVRTC_RGBA4);
                    textureImporter.SaveAndReimport();
                    count++;
                }
            }
        }
        Debug.Log("壓縮了:" + count + "個圖檔");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    [MenuItem("檢查圖檔/檔案夾内圖檔改為Advanced")]
    static void 檔案夾内圖檔改為Advanced()
    {
        if (Selection.objects.Length != )
        {
            Debug.LogError("選擇一個檔案夾");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[]);
        if (!Directory.Exists(path))
        {
            Debug.Log(path);
            return;
        }

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = ;
        string[] filePath = GetFilePath(Selection.objects);
        foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter == null)
                continue;
            if(textureImporter.textureType==TextureImporterType.Sprite)
                continue;
            //去掉圖檔的mipmap
            if (textureImporter.textureType!=TextureImporterType.Advanced)
            {
                textureImporter.textureType = TextureImporterType.Advanced;
                textureImporter.SaveAndReimport();
                count++;
            }


        }
        Debug.Log("處理了:" + count + "個圖檔");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    [MenuItem("檢查圖檔/去除檔案夾内圖檔的read write")]
    static void 去除檔案夾内圖檔的readWrite()
    {
        if (Selection.objects.Length != )
        {
            Debug.LogError("選擇一個檔案夾");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[]);
        if (!Directory.Exists(path))
        {
            Debug.Log(path);
            return;
        }

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = ;
        string[] filePath = GetFilePath(Selection.objects);
        foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter == null)
                continue;
            if (textureImporter.isReadable)
            {
                textureImporter.isReadable = false;
                textureImporter.SaveAndReimport();
                count++;
            }


        }
        Debug.Log("處理了:" + count + "個圖檔");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    [MenuItem("檢查圖檔/去除檔案夾内圖檔的mipmap")]
    static void 去除檔案夾内圖檔的mipmap()
    {
        if (Selection.objects.Length != )
        {
            Debug.LogError("選擇一個檔案夾");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[]);
        if (!Directory.Exists(path))
        {
            Debug.Log(path);
            return;
        }

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = ;
        string[] filePath = GetFilePath(Selection.objects);
        foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter == null)
                continue;
            //去掉圖檔的mipmap
            if (textureImporter.mipmapEnabled)
            {
                textureImporter.mipmapEnabled = false;
                textureImporter.SaveAndReimport();
                count++;
            }


        }
        Debug.Log("處理了:" + count + "個圖檔");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    [MenuItem("檢查圖檔/檔案夾内圖檔大小改為2的次幂")]
    static void 檔案夾内圖檔大小改為的次幂()
    {
        if (Selection.objects.Length != )
        {
            Debug.LogError("選擇一個檔案夾");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[]);
        if (!Directory.Exists(path))
        {
            Debug.Log(path);
            return;
        }

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = ;
        string[] filePath = GetFilePath(Selection.objects);
        foreach (var guid in AssetDatabase.FindAssets("t:Texture", filePath))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
            if (textureImporter == null)
                continue;

                        //檢視是否是2的次方
            Texture target = AssetDatabase.LoadAssetAtPath<Object>(assetPath) as Texture;
            var type = Types.GetType("UnityEditor.TextureUtil", "UnityEditor.dll");

            //MethodInfo[] methodInfos = type.GetMethods();
            //foreach (var obj in methodInfos)
            //    Debug.Log(obj);

            MethodInfo methodInfo = type.GetMethod("IsNonPowerOfTwo", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
            string isNon = methodInfo.Invoke(null, new object[] { target }).ToString();
            if (isNon.Contains("True"))
            {
                textureImporter.npotScale = TextureImporterNPOTScale.ToNearest;
                textureImporter.SaveAndReimport();
                count++;
            }



        }
        Debug.Log("處理了:" + count + "個圖檔");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    public static string[] GetFilePath(Object[] targets)
    {
        var folders = new List<string>();
        for (int i = ; i < targets.Length; i++)
        {
            string assetPath = AssetDatabase.GetAssetPath(targets[i]);
            if (Directory.Exists(assetPath))
                folders.Add(assetPath);
        }
        return folders.ToArray();
    }
    private static void AllFuntion(TextureImporter textureImporter)
    {
#if UNITY_EDITOR
        var type = typeof(TextureImporterSettings).Assembly.GetType("UnityEditor.TextureImporterSettings");

        var info = type.GetMembers(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);

        //MethodInfo nowAddButton = info[0];
        foreach (var t in info)
        {

            DebugBuild.Log(t.Name);
            //foreach (ParameterInfo i in t.GetParameters())
            //{
            //    Debug.Log("方法名" + t.Name + ",屬性:" + i.ParameterType);
            //    if (t.Name == "GetImportWarnings")
            //        nowAddButton = t;
            //}
        }
        //foreach (ParameterInfo i in nowAddButton.GetParameters())
        //    Debug.Log("方法名" + nowAddButton.Name + ",屬性:" + i.ParameterType);
        //object[] trans = new object[] { 1,2 };
        //DebugBuild.Log(nowAddButton.Invoke(textureImporter, trans));
#endif
    }
}
           

繼續閱讀