Unity 模型压缩

模型压缩

using UnityEngine;
using UnityEditor;
using System.IO;

using System.Collections.Generic;

//using Babybus.Framework.Serialization;
using System.Text;

public class MoodelCheck
{

    [MenuItem("检查模型/一键压缩模型的mesh")]
    static void 一键压缩所有模型的mesh()
    {

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = 0;
        foreach (var guid in AssetDatabase.FindAssets("t:Model"))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
            if (textureImporter == null)
                continue;

            //int maxsize = 0;
            //TextureImporterFormat textureImporterFormat;
            //string androidPlatform = "Android";
            if (textureImporter.meshCompression != ModelImporterMeshCompression.Off)
            {
                textureImporter.meshCompression = ModelImporterMeshCompression.Off;
                textureImporter.SaveAndReimport();
                //Debug.Log(assetPath);
                count++;
            }
        }
        Debug.Log("压缩了:" + count + "个模型");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    [MenuItem("检查模型/选择文件夹压缩模型mesh")]
    static void 选择文件夹压缩模型mesh()
    {
        if(Selection.objects.Length!=1)
        {
            Debug.LogError("选择一个文件夹");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
        if(!Directory.Exists(path))
        {
            Debug.Log(path);
            return;
        }

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = 0;
        string []filePath = GetFilePath(Selection.objects);
        foreach (var guid in AssetDatabase.FindAssets("t:Model", filePath))
        {

            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
            if (textureImporter == null)
                continue;

            //int maxsize = 0;
            //TextureImporterFormat textureImporterFormat;
            //string androidPlatform = "Android";

            if (textureImporter.meshCompression != ModelImporterMeshCompression.Off)
            {
                textureImporter.meshCompression = ModelImporterMeshCompression.Off;
                textureImporter.SaveAndReimport();
                count++;
            }
        }
        Debug.Log("压缩了:" + count + "个模型");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    [MenuItem("检查模型/去除文件夹内模型的readWrite")]
    static void 去除文件夹内模型的()
    {
        if (Selection.objects.Length != 1)
        {
            Debug.LogError("选择一个文件夹");
            return;
        }
        string path = AssetDatabase.GetAssetPath(Selection.objects[0]);
        if (!Directory.Exists(path))
        {
            Debug.Log(path);
            return;
        }

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

            //int maxsize = 0;
            //TextureImporterFormat textureImporterFormat;
            //string androidPlatform = "Android";
            if (textureImporter.isReadable)
            {
                textureImporter.isReadable = false;
                textureImporter.SaveAndReimport();
                count++;
            }
        }
        Debug.Log("压缩了:" + count + "个模型");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
    public static string[] GetFilePath(Object[] targets)
    {
        var folders = new List<string>();
        for (int i = 0; i < targets.Length; i++)
        {
            string assetPath = AssetDatabase.GetAssetPath(targets[i]);
            if (Directory.Exists(assetPath))
                folders.Add(assetPath);
        }
        return folders.ToArray();
    }
    [MenuItem("检查模型/一键压缩模型的动画为Optimal")]
    static void 一键压缩模型的动画()
    {

        AssetDatabase.StartAssetEditing();
        var assets = new List<string>();
        int count = 0;
        foreach (var guid in AssetDatabase.FindAssets("t:Model"))
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var textureImporter = AssetImporter.GetAtPath(assetPath) as ModelImporter;
            if (textureImporter == null)
                continue;

            //int maxsize = 0;
            //TextureImporterFormat textureImporterFormat;
            //string androidPlatform = "Android";
            if (textureImporter.animationCompression != ModelImporterAnimationCompression.Optimal)
            {
                textureImporter.animationCompression = ModelImporterAnimationCompression.Optimal;
                textureImporter.SaveAndReimport();
                count++;
            }
        }
        Debug.Log("压缩了:" + count + "个动画");
        AssetDatabase.StopAssetEditing();
        AssetDatabase.Refresh();
    }
}

你可能感兴趣的:(Unity3D)