目录
一种可能解决所有文件读取文件大小解决方法
Unity编辑器源码
https://github.com/Unity-Technologies/UnityCsReference
可尝试先找出想读取的文件Insperator面板的Editor脚本,例如:AnimationClipEditor。接着找到哪个变量是文件大小,找到后使用反射来进行读取,只适用于编辑器使用~
一、贴图大小
参考:Unity3D研究院编辑器之脚本获取资源内存和硬盘大小(二十五)
static string GetMemory(UnityEngine.Object obj)
{
if (obj == null)
{
Debug.Log("obj为空,无法获取硬盘大小");
return "";
}
Sprite sprite = obj as Sprite;
Texture texture = null;
if (sprite != null)
{
texture = sprite.texture;
}
if (texture == null)
{
texture = obj as Texture;
}
if (texture != null)
{
System.Type type = System.Reflection.Assembly.Load("UnityEditor.dll").GetType("UnityEditor.TextureUtil");
System.Reflection.MethodInfo methodInfo = type.GetMethod("GetStorageMemorySize", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public);
Debug.Log("内存占用:" + EditorUtility.FormatBytes(UnityEngine.Profiling.Profiler.GetRuntimeMemorySizeLong(texture)));
Debug.Log("硬盘占用:" + EditorUtility.FormatBytes((int)methodInfo.Invoke(null, new object[] { texture })));
return EditorUtility.FormatBytes((int)methodInfo.Invoke(null, new object[] { texture }));
}
return "";
}