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 object2 { 0, 0 };

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

mi.Invoke(textureImporter, args);

int width = (int)args0;

int height = (int)args1;

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;

}

}

}

相关推荐
玖玥拾4 小时前
Unity 3D 笔记(十四)Unity/C# Socket 网络笔记3
服务器·网络·unity·c#
Python私教5 小时前
Godot图片怎么导入?PNG、压缩、Mipmap与Reimport一次讲清
游戏引擎·godot
玖玥拾6 小时前
Unity 3D 笔记(十七)Unity/C# Socket 网络笔记6
服务器·网络·unity·c#
寒水馨7 小时前
Linux下载、安装godot-4.7.1-stable(附安装包Godot_v4.7.1-stable_linux.x86_64.zip)
linux·游戏引擎·godot·游戏开发·2d游戏·3d游戏·godot engine
寒水馨10 小时前
macOS下载、安装godot-4.7.1-stable(附安装包Godot_v4.7.1-stable_macos.universal.zip)
macos·3d·游戏引擎·godot·跨平台·游戏开发·2d
电子云与长程纠缠1 天前
UE中使用TGuardValue与TInlineComponentArray数据结构
开发语言·数据结构·学习·ue5·游戏引擎
玖玥拾1 天前
Unity 3D 笔记(十二)Unity/C# Socket 网络笔记1
网络·unity·c#
玖玥拾1 天前
Unity 3D 笔记(十五)Unity/C# Socket 网络笔记4
服务器·网络·unity·c#
xcLeigh2 天前
Unity基础:创建你的第一个游戏物体——Cube、Sphere与基本3D物体
游戏·3d·unity·教程
ellis19702 天前
C#异常相关关键字:Exceptions,throw,try,catch,finally
unity·c#