LoginSignup
2
2

More than 5 years have passed since last update.

Unity上でのテクスチャサイズ一覧取得

Posted at

■Editor上でのテクスチャサイズが知りたかったのでEditor拡張作りました。
フィルタ返れば何でもいけるはず


using UnityEngine;
using UnityEditor;
using System.Reflection;
using System;

public static class CalcTextureSize
{

    private static Type mType;
    private static MethodInfo mGetTextureFormat;
    private static MethodInfo mCountMipmaps;
    private static MethodInfo mGetStorageMemorySize;
    private static MethodInfo mHasAlphaTextureFormat;
    private static MethodInfo mIsCompressedTextureFormat;
    private static MethodInfo mIsNonPowerOfTwo;
    private static MethodInfo mGetGLWidth;
    private static MethodInfo mGetGLHeight;

    static void TextureInitial()
    {
        string typeName = "UnityEditor.TextureUtil";
        mType = System.Reflection.Assembly.Load("UnityEditor.dll").GetType(typeName);

        if (mType == null)
        {
            return;
        }

        mCountMipmaps = mType.GetMethod("CountMipmaps", BindingFlags.Static | BindingFlags.Public);
        mGetStorageMemorySize = mType.GetMethod("GetStorageMemorySize", BindingFlags.Static | BindingFlags.Public);
        mGetTextureFormat = mType.GetMethod("GetTextureFormat", BindingFlags.Static | BindingFlags.Public);
        mGetGLHeight = mType.GetMethod("GetGLHeight", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(Texture) }, null);
        mGetGLWidth = mType.GetMethod("GetGLWidth", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(Texture) }, null);
        mHasAlphaTextureFormat = mType.GetMethod("HasAlphaTextureFormat", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(TextureFormat) }, null);
        mIsCompressedTextureFormat = mType.GetMethod("IsCompressedTextureFormat", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(TextureFormat) }, null);
        mIsNonPowerOfTwo = mType.GetMethod("IsNonPowerOfTwo", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(Texture2D) }, null);
    }


    [MenuItem("Texture/Check")]
    public static void TextureCheck()
    {
        TextureInitial();

        string filter = "t:Texture2D";

        string[] guids = AssetDatabase.FindAssets(filter);

        System.Text.Encoding utf8Enc = System.Text.Encoding.GetEncoding("utf-8");

        System.IO.StreamWriter writer = new System.IO.StreamWriter("FileSize.csv", false, utf8Enc);
        writer.WriteLine("パス,サイズ,フォーマット");

        for (int i = 0; i < guids.Length; i++)
        {
            string g = guids[i];
            string path = AssetDatabase.GUIDToAssetPath(g);

            //EditorUtility.DisplayProgressBar("Find", "", (float)i / guids.Length);

            Texture2D tex = AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D;
            if (tex)
            {

                UnityEngine.Debug.Log("path=" + path);
                UnityEngine.Debug.Log("size=" + GetStorageMemorySize(tex));
                UnityEngine.Debug.Log("format=" + tex.format.ToString());

                UnityEngine.Debug.Log("path=" + path + ", size=" + GetStorageMemorySize(tex) + ", format=" + tex.format.ToString());
                writer.WriteLine(path + "," + GetStorageMemorySize(tex) + "," + tex.format.ToString());
            }
        }

        //EditorUtility.ClearProgressBar();
        writer.Close();

        Debug.Log("End");
    }


    public static int GetStorageMemorySize(Texture texture)
    {
        if (mGetStorageMemorySize == null)
        {
            return 0;
        }

        var args = new object[] { texture };
        int size = (int)mGetStorageMemorySize.Invoke(null, args);
        return size;
    }
}

■参考URL
http://tsubakit1.hateblo.jp/entry/2015/09/23/134913
http://baba-s.hatenablog.com/entry/2015/09/16/100000
http://tsubakit1.hateblo.jp/entry/2016/11/25/235315

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2