当前位置:   article > 正文

Unity图集处理小工具:批量设置图集的MaxSize及压缩格式_unity 动态调整图集尺寸

unity 动态调整图集尺寸

如题一个编辑器小工具,主要在于Editor接口的使用和判断一张图片有无alpha通道,下面直接上代码:

  #region atlas deal

    public static int CompressQuality = 50;

    public static float halveRate = 0.5f;

    [MenuItem("Tools/HalveAtlas")]
    public static void HalveAtlas()
    {
        UnityEngine.Object[] objects = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Assets); //获取选择文件夹
        for (int i = 0; i < objects.Length; i++)
        {
            string dirPath = AssetDatabase.GetAssetPath(objects[i]).Replace("\\", "/");
            if (!Directory.Exists(dirPath))
            {
                EditorUtility.DisplayDialog("错误", "选择正确文件夹!", "好的");
                continue;
            }
            HalveSprite(dirPath);
        }
    }

    private static void HalveSprite(string dirPath)
    {
        string[] files = Directory.GetFiles(dirPath, "*.*", SearchOption.AllDirectories);
        for (int i = 0; i < files.Length; i++)
        {
            string filePath = files[i];
            filePath = filePath.Replace("\\", "/");

            if (filePath.EndsWith(".png") || filePath.EndsWith(".jpg"))
            {
            //筛选出png和jpg图片
                EditorUtility.DisplayProgressBar("处理中>>>", filePath, (float)i / (float)files.Length);

                TextureImporter textureImporter = AssetImporter.GetAtPath(filePath) as TextureImporter;
                if (textureImporter == null)
                    return;
                //判断图片有无alpha通道,有默认格式设置成:RGBA16;无默认格式设置成:RGB16
                textureImporter.textureFormat = TextureImporterFormat.AutomaticTruecolor;
                AssetDatabase.ImportAsset(filePath);

                Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(filePath);
                int textureSize = Math.Max(texture.height, texture.width);

                TextureImporterFormat defaultTextureFormat = TextureImporterFormat.RGB16;
                if (texture.format == TextureFormat.RGB24)
                {
                    //no alpha
                    defaultTextureFormat = TextureImporterFormat.RGB16;
                }
                else
                {
                    defaultTextureFormat = TextureImporterFormat.RGBA16;
                }

                TextureImporterSettings settings = new TextureImporterSettings();
                textureImporter.ReadTextureSettings(settings);

                textureImporter.textureType = TextureImporterType.Advanced;
                int defaultMaxTextureSize = settings.maxTextureSize;
                defaultMaxTextureSize = Math.Min(textureSize, defaultMaxTextureSize);
                defaultMaxTextureSize = (int)(defaultMaxTextureSize * halveRate);
                settings.textureFormat = defaultTextureFormat;
                settings.maxTextureSize = GetValidSize(defaultMaxTextureSize);

                int androidMaxTextureSize = 0;
                TextureImporterFormat androidTextureFormat = UnityEditor.TextureImporterFormat.ETC_RGB4;
                bool isAndroidOverWrite = textureImporter.GetPlatformTextureSettings("Android", out androidMaxTextureSize, out androidTextureFormat);
                if (true == isAndroidOverWrite)
                {
                    androidMaxTextureSize = Math.Min(textureSize, androidMaxTextureSize);
                    androidMaxTextureSize = (int)(androidMaxTextureSize * halveRate);
                    textureImporter.SetPlatformTextureSettings("Android", GetValidSize(androidMaxTextureSize), androidTextureFormat, CompressQuality);
                }

                int iphoneMaxTextureSize = 0;
                TextureImporterFormat iphoneTextureFormat = UnityEditor.TextureImporterFormat.PVRTC_RGBA4;
                bool isIphoneOverWrite = textureImporter.GetPlatformTextureSettings("iPhone", out iphoneMaxTextureSize, out iphoneTextureFormat);
                if (true == isIphoneOverWrite)
                {
                    iphoneMaxTextureSize = Math.Min(textureSize, iphoneMaxTextureSize);
                    iphoneMaxTextureSize = (int)(iphoneMaxTextureSize * halveRate);
                    textureImporter.SetPlatformTextureSettings("iPhone", GetValidSize(iphoneMaxTextureSize), iphoneTextureFormat, CompressQuality);
                }

                textureImporter.SetTextureSettings(settings);
                AssetDatabase.SaveAssets();
                DoAssetReimport(filePath, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);
            }
        }

        EditorUtility.ClearProgressBar();
        EditorUtility.DisplayDialog("成功", "处理完成!", "好的");
    }

    private static int GetValidSize(int size)
    {
        int result = 0;
        if (size <= 48)
        {
            result = 32;
        }
        else if (size <= 96)
        {
            result = 64;
        }
        else if (size <= 192)
        {
            result = 128;
        }
        else if (size <= 384)
        {
            result = 256;
        }
        else if (size <= 768)
        {
            result = 512;
        }
        else if (size <= 1536)
        {
            result = 1024;
        }
        else if (size <= 3072)
        {
            result = 2048;
        }

        return result;
    }

    public static void DoAssetReimport(string path, ImportAssetOptions options)
    {
        try
        {
            AssetDatabase.StartAssetEditing();
            AssetDatabase.ImportAsset(path, options);
        }
        finally
        {
            AssetDatabase.StopAssetEditing();
        }
    }

    #endregion
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/125537
推荐阅读
相关标签
  

闽ICP备14008679号