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;

}

}

}

相关推荐
躺下睡觉~6 小时前
Unity-Transform类-父子关系
java·unity·游戏引擎
躺下睡觉~6 小时前
Unity-Transform类-缩放和看向
unity·游戏引擎
君莫愁。9 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
咩咩觉主9 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
蓝裕安12 小时前
伪工厂模式制造敌人
开发语言·unity·游戏引擎
谢泽浩16 小时前
Unity 给模型贴上照片
unity·游戏引擎
z2014z16 小时前
Unity Resource System 优化笔记
unity·游戏引擎
王维志17 小时前
Unity 高亮插件HighlightPlus介绍
unity·游戏引擎
zaizai100718 小时前
我的demo保卫萝卜中的技术要点
unity
菌菌巧乐兹19 小时前
Unity 百度AI实现无绿幕拍照抠像功能(详解版)
人工智能·百度·unity