Unity 检查图片资源

using UnityEditor;

using System.Collections.Generic;

using UnityEngine;

using System.IO;

using System;

using System.Reflection;

public class EditorTool : Editor

{

[MenuItem("Tools/Resource/SearchWarningResource")]

public static void SearchResource()

{

string path = Application.dataPath;

string[] dirs = Directory.GetDirectories(path);

SearchFile(dirs);

}

static public void SearchFile(string[] path)

{

foreach (string item in path)

{

string[] getFile = Directory.GetFiles(item);

foreach (string file in getFile)

{

FileInfo fileInfo = new FileInfo(file);

if (fileInfo.Extension.Equals(".png", StringComparison.CurrentCultureIgnoreCase) || fileInfo.Extension.Equals(".jpg", StringComparison.CurrentCultureIgnoreCase))

{

string _file = file.Replace("\\", "/");

_file = _file.Substring(_file.IndexOf("Assets"));

TextureImporter textureImporter = AssetImporter.GetAtPath(_file) as TextureImporter;

if (textureImporter == null)

return;

AssetDatabase.ImportAsset(_file);

object[] args = new object[2] { 0, 0 };

MethodInfo mi = typeof(TextureImporter).GetMethod("GetWidthAndHeight", BindingFlags.NonPublic | BindingFlags.Instance);

mi.Invoke(textureImporter, args);

int width = (int)args[0];

int height = (int)args[1];

if (width > 2048)

{

Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(_file);

long size = GetTextureFileSize(texture, _file);

//long ySize = GetTextureRuntimeMemorySize(texture);

Debug.LogError(string.Format("警告,width:{0},height:{1},占用内存:{2}M,图片资源{3}", width, height, size / 1048576, fileInfo.FullName));

}

else if (height > 2048)

{

Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(_file);

long size = GetTextureFileSize(texture, _file);

//long ySize = GetTextureRuntimeMemorySize(texture);

Debug.LogError(string.Format("警告,width:{0},height:{1},占用内存:{2}M,图片资源{3}", width, height, size / 1048576, fileInfo.FullName));

}

}

}

string[] getFoder = Directory.GetDirectories(item);

SearchFile(getFoder);

}

}

public static long GetTextureFileSize(Texture2D texture, string _file)

{

try

{

long fileSize = 0;

// 使用反射获取UnityEditor.TextureUtil类的Type

Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");

// 使用反射获取UnityEditor.TextureUtil类的GetStorageMemorySizeLong方法

MethodInfo getStorageMemorySizeLongMethod = textureUtilType.GetMethod("GetStorageMemorySizeLong", BindingFlags.Static | BindingFlags.Public);

// 调用GetStorageMemorySizeLong方法获取存储内存大小

fileSize = (long)getStorageMemorySizeLongMethod.Invoke(null, new object[] { texture });

return fileSize;

}

catch (Exception e)

{

Debug.LogError("GetTextureFileSize error:" + _file);

return 0;

}

}

public static long GetTextureRuntimeMemorySize(Texture2D texture)

{

try

{

long memorySize;

// 使用反射获取UnityEditor.TextureUtil类的Type

Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");

// 使用反射获取UnityEditor.TextureUtil类的GetRuntimeMemorySizeLong方法

MethodInfo getRuntimeMemorySizeLongMethod = textureUtilType.GetMethod("GetRuntimeMemorySizeLong", BindingFlags.Static | BindingFlags.Public);

// 调用GetRuntimeMemorySizeLong方法获取运行时内存大小

memorySize = (long)getRuntimeMemorySizeLongMethod.Invoke(null, new object[] { texture });

return memorySize;

}

catch (Exception e)

{

Debug.LogError("GetTextureRuntimeMemorySize error:" + e);

return 0;

}

}

}

相关推荐
咩咩觉主1 小时前
尽量通俗易懂地概述.Net && U nity跨语言/跨平台相关知识
unity·c#·.net·.netcore
墨笺染尘缘2 小时前
Unity——对RectTransform进行操作
ui·unity·c#·游戏引擎
AgilityBaby3 小时前
FairyGUI和Unity联动(入门篇)
unity·游戏引擎
这不比博人传燃?5 小时前
传奇996_19——龙岭总结
游戏引擎
一步一个foot-print15 小时前
C# unity 星期几 年月日控制
unity·c#
erxij16 小时前
【游戏引擎之路】登神长阶(十三)——Vulkan教程:讲个笑话:离开舒适区
c++·经验分享·游戏·3d·游戏引擎
无敌最俊朗@21 小时前
unity3d————Sprite(精灵图片)
学习·游戏·unity·c#·游戏引擎
皮皮陶1 天前
Unity WebGL交互通信
unity·交互·webgl
程序员正茂1 天前
PICO+Unity MR空间网格
unity·mr·pico
程序员正茂1 天前
PICO+Unity MR空间锚点
unity·pico·空间锚点