# Unity 如何获取Texture 的内存大小
Unity 如何获取Texture 的内存大小
在Unity中,要获取Texture的内存文件大小,可以使用UnityEditor.TextureUtil类中的一些函数。这些函数提供了获取存储内存大小和运行时内存大小的方法。由于UnityEditor.TextureUtil是一个内部类,我们需要使用反射来访问它。
步骤
- 导入UnityEditor命名空间和System.Reflection命名空间:
using UnityEditor;
using System.Reflection;
- 创建一个函数来获取Texture的内存文件大小:
public static long GetTextureFileSize(Texture2D texture)
{
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;
}
- 创建一个函数来获取Texture的运行时内存大小:
public static long GetTextureRuntimeMemorySize(Texture2D texture)
{
long memorySize = 0;
// 使用反射获取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;
}
示例代码
示例 1:获取Texture的存储内存大小
using UnityEngine;
using UnityEditor;
using System.Reflection;
public class TextureSizeExample : MonoBehaviour
{
[SerializeField]
private Texture2D texture;
private void Start()
{
long fileSize = GetTextureFileSize(texture);
Debug.Log("Texture File Size: " + fileSize + " bytes");
}
private static long GetTextureFileSize(Texture2D texture)
{
long fileSize = 0;
Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");
MethodInfo getStorageMemorySizeLongMethod = textureUtilType.GetMethod("GetStorageMemorySizeLong", BindingFlags.Static | BindingFlags.Public);
fileSize = (long)getStorageMemorySizeLongMethod.Invoke(null, new object[] { texture });
return fileSize;
}
}
示例 2:获取Texture的运行时内存大小
using UnityEngine;
using UnityEditor;
using System.Reflection;
public class TextureSizeExample : MonoBehaviour
{
[SerializeField]
private Texture2D texture;
private void Start()
{
long memorySize = GetTextureRuntimeMemorySize(texture);
Debug.Log("Texture Runtime Memory Size: " + memorySize + " bytes");
}
private static long GetTextureRuntimeMemorySize(Texture2D texture)
{
long memorySize = 0;
Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");
MethodInfo getRuntimeMemorySizeLongMethod = textureUtilType.GetMethod("GetRuntimeMemorySizeLong", BindingFlags.Static | BindingFlags.Public);
memorySize = (long)getRuntimeMemorySizeLongMethod.Invoke(null, new object[] { texture });
return memorySize;
}
}
示例 3:同时获取Texture的存储内存大小和运行时内存大小
using UnityEngine;
using UnityEditor;
using System.Reflection;
public class TextureSizeExample : MonoBehaviour
{
[SerializeField]
private Texture2D texture;
private void Start()
{
long fileSize = GetTextureFileSize(texture);
long memorySize = GetTextureRuntimeMemorySize(texture);
Debug.Log("Texture File Size: " + fileSize + " bytes");
Debug.Log("Texture Runtime Memory Size: " + memorySize + " bytes");
}
private static long GetTextureFileSize(Texture2D texture)
{
long fileSize = 0;
Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");
MethodInfo getStorageMemorySizeLongMethod = textureUtilType.GetMethod("GetStorageMemorySizeLong", BindingFlags.Static | BindingFlags.Public);
fileSize = (long)getStorageMemorySizeLongMethod.Invoke(null, new object[] { texture });
return fileSize;
}
private static long GetTextureRuntimeMemorySize(Texture2D texture)
{
long memorySize = 0;
Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");
MethodInfo getRuntimeMemorySizeLongMethod = textureUtilType.GetMethod("GetRuntimeMemorySizeLong", BindingFlags.Static | BindingFlags.Public);
memorySize = (long)getRuntimeMemorySizeLongMethod.Invoke(null, new object[] { texture });
return memorySize;
}
}
注意事项
- 确保在使用反射访问UnityEditor.TextureUtil类之前,已经导入了UnityEditor命名空间和System.Reflection命名空间。
- 使用反射时,需要使用BindingFlags.Static | BindingFlags.Public来获取静态公共方法。
- 在示例代码中,我们使用了Texture2D类型的变量来表示Texture,你可以根据实际情况修改代码以适应不同的Texture类型。
# Unity 如何获取Texture 的内存大小的更多相关文章
- 获取JAVA对象占用的内存大小
介绍两种获取JAVA对象内存大小的方法. 第一种:Instrumentation 简介: 使用java.lang.instrument 的Instrumentation来获取一个对象的内存大小.利用I ...
- unity texture 占用内存大小对比
打包多种类型的项目,空项目和10张放在Resources文件夹中的图为比较案例.以下是比较数据. IPHONE: 1.空项目----空间占用量42.3MB----IPA大小10MB 2.10张1200 ...
- Android中一张图片加载后所占用内存大小的获取与测试
Android程序中一旦加载的图片比较多,就有可能出现Out of Memory而导致程序崩溃.这个一方面是因为Android系统本身对于每个单独的进程有内存大小的限制(有16M,64M,128M,2 ...
- iOS 获取内存大小使用情况(进度条显示)
一.获取设备内存大小方法 //返回存储内存占用比例 - (NSString *)getFreeDiskspaceRate{ float totalSpace; .f; NSError *error = ...
- linux下获取硬盘、内存、U盘大小及使用大小
/* * 获取硬盘大小;内存大小;usb大小 */ #ifndef SYSINFOGET_H #define SYSINFOGET_H #include <stdio.h> //磁盘信息 ...
- C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息
摘要 有时需要获取电脑的相关信息.这时可以通过调用windows api的方式,进行获取. 方法 可以通过在powershell中 通过下面的命令进行查询,然后可以通过c#调用获取需要的信息. gwm ...
- 获取 iOS APP 内存占用的大小
当我们想去获取 iOS 应用的占用内存时,通常我们能找到的方法是这样的,用 resident_size: #import <mach/mach.h> - (int64_t)memory ...
- C# 利用WMI对象获取物理内存和可用内存大小
下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下: //获取总物理内存大小 M ...
- .net 获取CPU频率 内存 磁盘大小,域名 端口 虚拟目录等
CPU个数: @Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS") CPU类型: @Environment.Get ...
- 如何获取一个Java对象所占内存大小
新建一个maven工程 我们先在IDEA中新建一个名为ObjectSizeFetcherAgent的maven工程,如下图: 在maven项目中的pom.xml中新增一个打jar包的插件,如下: &l ...
随机推荐
- 2022-04-01:有n个人,m个任务,任务之间有依赖记录在int[][] depends里。 比如: depends[i] = [a, b],表示a任务依赖b任务的完成, 其中 0 <= a <
2022-04-01:有n个人,m个任务,任务之间有依赖记录在int[][] depends里. 比如: depends[i] = [a, b],表示a任务依赖b任务的完成, 其中 0 <= a ...
- Springboot通过谷歌Kaptcha 组件,生成图形验证码
图形验证码属于老生常谈了,具体细节这里就不说了.生成图形验证码的办法非常多,今天讲解一种通过Kaptcha组件快速生成图形验证码的方法.Kaptcha是谷歌开源的一款简单实用的图形验证码组件.我个人推 ...
- dnu
背景 作为一个喜欢搬运 YouTube 视频的网友,我发现将视频下载下来再上传到 B 站十分繁琐,因此我决定开发一个小工具,能够方便快捷地将 YouTube 视频下载并上传至 B 站,以节省我的时间和 ...
- 如何在 macOS 上进行 STM32 开发 All In One
如何在 macOS 上进行 STM32 开发 All In One 网上好多 STM32 的教学视频的开发环境都是基于 Windows 系统的,对 macOS 用户直接劝退了 solutions 在 ...
- Java 泛型:理解和应用
概述 泛型是一种将类型参数化的动态机制,使用得到的话,可以从以下的方面提升的你的程序: 安全性:使用泛型可以使代码更加安全可靠,因为泛型提供了编译时的类型检查,使得编译器能够在编译阶段捕捉到类型错误. ...
- 源代码管理工具——Git
Git是一个开源的分布式版本控制系统,用于管理软件开发中的版本控制和协作.通过Git,开发人员可以记录文件的修改历史.协作开发,以及在多个分支上进行实验性开发.Git已成为现代软件开发中不可或缺的工具 ...
- 算法基础(一):串匹配问题(BF,KMP算法)
好家伙,学算法, 这篇看完,如果没有学会KMP算法,麻烦给我点踩 希望你能拿起纸和笔,一边阅读一边思考,看完这篇文章大概需要(20分钟的时间) 我们学这个算法是为了解决串匹配的问题 那什么是串匹配 ...
- 深入浅出MySQL事务
Photo by Lukas Hartmann from Pexels 辞职这段时间以来看见了很多工作之外的东西,我认为这是值得的.同时也有时间和机会来好好整理所学所想,准备开启下一段旅途. 事务的定 ...
- LeetCode 双周赛 107(2023/06/24)滑动窗口与离散化
本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 和 [BaguTree Pro] 知识星球提问. 往期回顾:LeetCode 单周赛第 348 场 · 数位 DP ...
- Python 一大坑,配置文件中字典引用问题(拷贝)。
大坑 +1 python 配置文件中字典引用问题 最近在开发系统时发现一个传奇的BUG, 用户未登录就可进入系统内,而且含有真实身份信息. 此问题困扰多时,反复debug.由于找不到问题原因,复现具有 ...