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;

}

}

}

相关推荐
向宇it13 小时前
【unity小技巧】unity 什么是反射?反射的作用?反射的使用场景?反射的缺点?常用的反射操作?反射常见示例
开发语言·游戏·unity·c#·游戏引擎
Heaphaestus,RC14 小时前
【Unity3D】获取 GameObject 的完整层级结构
unity·c#
芋芋qwq14 小时前
Unity UI射线检测 道具拖拽
ui·unity·游戏引擎
tealcwu15 小时前
【Unity服务】关于Unity LevelPlay的基本情况
unity·游戏引擎
大眼睛姑娘17 小时前
Unity3d场景童话梦幻卡通Q版城镇建筑植物山石3D模型游戏美术素材
unity·游戏美术
鹿野素材屋1 天前
Unity Dots下的动画合批工具:GPU ECS Animation Baker
unity·游戏引擎
小春熙子1 天前
Unity图形学之着色器之间传递参数
unity·游戏引擎·技术美术·着色器
虾球xz1 天前
游戏引擎学习第15天
学习·游戏引擎
Java Fans1 天前
在Unity中实现电梯升降功能的完整指南
unity·游戏引擎
GrimRaider2 天前
[Unity]TileMap开发,TileMap地图缝隙问题
unity·游戏引擎·tilemap