Unity优化相关-Texture

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Le_eG/article/details/81290706

修改所有Texture的MipMaps项,分别采用Directory、Selection以及GUID的方式获取Texture。

版本:5.3.6f1

using UnityEngine;
using UnityEditor;
using System.IO;

public class TextureOptimization : MonoBehaviour {
    //LOG信息保存路径
    static string LogPath()
    {
        string log_path = Application.dataPath + "/Log";
        if (!Directory.Exists(log_path))
        {
            Directory.CreateDirectory(log_path);
        }
        return log_path;
    }
    //对指定目录下的所有贴图进行修改
    [MenuItem("TextureOptimization/AllPath")]
    static void TextureOptimizationAllPath()
    {
        string head_path = "Assets/";
        string full_path = "";
        int texture_num = 0;
        string log_path = string.Format("{0}/{1}", LogPath(), "TextureOptimization.txt");
        StreamWriter stream_writer = new StreamWriter(log_path);
        if (Directory.Exists(head_path))
        {
            DirectoryInfo direction = new DirectoryInfo(head_path);
            FileInfo[] all_files = direction.GetFiles("*", SearchOption.AllDirectories);
            for(int i = 0; i < all_files.Length; i++)
            {
                EditorUtility.DisplayProgressBar("贴图MipMaps优化", string.Format("进度:{0} / {1}", i, all_files.Length), (float)i / all_files.Length);
                full_path = all_files[i].FullName.Substring(all_files[i].FullName.IndexOf("Assets"));
                TextureImporter texture = AssetImporter.GetAtPath(full_path) as TextureImporter;
                if (texture != null)
                {
                    if (texture.mipmapEnabled)
                    {
                        texture.mipmapEnabled = false;
                        texture.SaveAndReimport();
                        stream_writer.WriteLine(full_path);
                        ++texture_num;
                    }
                }
            }
            stream_writer.WriteLine("修改贴图数:" + texture_num);
            stream_writer.Close();
            EditorUtility.ClearProgressBar();
        }
    }
    //对当前选中文件夹及其所有子文件贴图进行修改
    [MenuItem("TextureOptimization/SelectedPath")]
    static void TextureOptimizationSelectedPath()
    {
        Object[] textures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
        string texture_path = "";
        string log_path = string.Format("{0}/{1}", LogPath(), "TextureOptimization.txt");
        StreamWriter stream_writer = new StreamWriter(log_path);
        int texture_num = 0;
        for (int i = 0; i < textures.Length; i++)
        {
            EditorUtility.DisplayProgressBar("贴图MipMaps优化", string.Format("进度:{0} / {1}", i, textures.Length), (float)i / textures.Length);
            texture_path = AssetDatabase.GetAssetPath(textures[i]);
            TextureImporter texture = TextureImporter.GetAtPath(texture_path) as TextureImporter;
            if (texture != null)
            {
                if (texture.mipmapEnabled)
                {
                    texture.mipmapEnabled = false;
                    texture.SaveAndReimport();
                    stream_writer.WriteLine(texture_path);
                    ++texture_num;
                }
            }
        }
        stream_writer.WriteLine("修改贴图数:" + texture_num);
        stream_writer.Close();
        EditorUtility.ClearProgressBar();
    }
    //用GUID查找所有Texture
    [MenuItem("TextureOptimization/AllPathByGUID")]
    static void TextureOptimizationAllPathByGUID()
    {
        string root_path = "Assets";
        string[] guid = AssetDatabase.FindAssets("t:Texture", new string[] { root_path });
        string texture_path = "";
        string log_path = string.Format("{0}/{1}", LogPath(), "TextureOptimization.txt");
        StreamWriter stream_writer = new StreamWriter(log_path);
        int texture_num = 0;
        for (int i = 0; i < guid.Length; i++)
        {
            EditorUtility.DisplayProgressBar("贴图MipMaps优化", string.Format("进度:{0} / {1}", i, guid.Length), (float)i / guid.Length);
            texture_path = AssetDatabase.GUIDToAssetPath(guid[i]);
            TextureImporter texture = TextureImporter.GetAtPath(texture_path) as TextureImporter;
            if (texture != null)
            {
                if (texture.mipmapEnabled)
                {
                    texture.mipmapEnabled = false;
                    texture.SaveAndReimport();
                    stream_writer.WriteLine(texture_path);
                    ++texture_num;
                }
            }
        }
        stream_writer.WriteLine("修改贴图数:" + texture_num);
        stream_writer.Close();
        EditorUtility.ClearProgressBar();
    }
}

猜你喜欢

转载自blog.csdn.net/Le_eG/article/details/81290706