当前位置:   article > 正文

Unity内存优化MaxTextureSize_unity3d android texture maxsize error

unity3d android texture maxsize error

只是简单的把图片的maxTextureSize设置成它本身的最大值还大的下一个


using BehaviorDesigner.Runtime.Tasks.Unity.UnityBoxCollider;
using DG.Tweening.Plugins.Core.PathCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using UnityEditor;

public class PictureCompress: Editor
{
    

    [MenuItem("UITools/选中的文件内图片压缩")]
    static void PictureCompressFunc()
    {
        string[] assetGUIDArray = Selection.assetGUIDs;

        string assetPath = AssetDatabase.GUIDToAssetPath(assetGUIDArray[0]);


        string imgtype = "*.BMP|*.JPG|*.GIF|*.PNG";
        string[] ImageType = imgtype.Split('|');
        for (int i = 0; i < ImageType.Length; i++)
        {

            string[] dirs = Directory.GetFiles(assetPath, ImageType[i], SearchOption.AllDirectories);
            for (int j = 0; j < dirs.Length; j++)
            {
                var path = dirs[j];
                TextureImporter textureImporter = AssetImporter.GetAtPath(path) as TextureImporter;
                int width = 0;
                int height = 0;
                GetTextureOriginalSize(textureImporter, out width, out height);
                if (width > textureImporter.maxTextureSize || height > textureImporter.maxTextureSize) 
                {
                    continue;
                }
                else
                {
                    SetSize(width, height, textureImporter);
                }
            }
        }
        MyLog.DevLog("执行完毕!");
    }

    private static void SetSize(int width, int height, TextureImporter textureImporter)
    {
        int max = width > height ? width : height;
        int[] size_r = { 32, 64, 128, 256, 512, 1024, 2048 };
        TextureImporterPlatformSettings settingAndroid = textureImporter.GetPlatformTextureSettings("Android");
        settingAndroid.overridden = true;
        if (settingAndroid.maxTextureSize < max)
            return;
        for (int i = 0; i < size_r.Length; i++)
        {
            if (size_r[i] > max)
            {
                settingAndroid.maxTextureSize = size_r[i];
                //MyLog.DevLog("settingAndroid.maxTextureSize11111111", settingAndroid.maxTextureSize);
                textureImporter.SetPlatformTextureSettings(settingAndroid);
                return;
            }
        }
        settingAndroid.maxTextureSize = size_r[size_r.Length - 1];
        textureImporter.SetPlatformTextureSettings(settingAndroid);
        //MyLog.DevLog("settingAndroid.maxTextureSize2222222", settingAndroid.maxTextureSize);
    }

    public static void GetTextureOriginalSize(TextureImporter ti, out int width, out int height)
    {
        if (ti == null)
        {
            width = 0;
            height = 0;
            return;
        }

        object[] args = new object[2] { 0, 0 };
        MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);
        mi.Invoke(ti, args);

        width = (int)args[0];
        height = (int)args[1];
    }


}

  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/826949
推荐阅读
相关标签
  

闽ICP备14008679号