# 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 ...
随机推荐
- 2021-10-10:杨辉三角 II。给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行。在「杨辉三角」中,每个数是它左上方和右上方的数的和。力扣119。
2021-10-10:杨辉三角 II.给定一个非负索引 rowIndex,返回「杨辉三角」的第 rowIndex 行.在「杨辉三角」中,每个数是它左上方和右上方的数的和.力扣119. 福大大 答案20 ...
- Github疯传!谷歌师兄的LeetCode刷题笔记开源了!
有小伙伴私聊我说刚开始刷LeetCode的时候,感到很吃力,刷题效率很低.我以前刷题的时候也遇到这个问题,直到后来看到这个谷歌师兄总结的刷题笔记,发现LeetCode刷题都是套路呀,掌握这些套路之后, ...
- 使用 Easysearch 还原 Elasticsearch 快照数据
本文主要验证 Elasticsearch 快照在 Easysearch 中进行数据恢复. 准备测试数据 索引 别名 模版 生命周期策略 创建快照 PUT /_snapshot/my_backup { ...
- CSS3学习记录之loading动画
loading动画就是在加载一些网页内容的时候呈现出来的小动画,记录一下学到的几种loading动画: 效果:http://39.105.101.122/myhtml/CSS/Loading/load ...
- 用AI技术实现自动化的社交媒体广告投放,提高广告效果和收益
目录 1. 引言 2. 技术原理及概念 2.1 基本概念解释 随着社交媒体的普及,广告投放已经成为了广告行业的重要一环.在过去的几年中,社交媒体广告投放的效果和收益都得到了显著提高,但同时也存在着一些 ...
- 记一次字符串末尾空白丢失的排查 → MySQL 是会玩的!
开心一刻 今天答应准时回家和老婆一起吃晚饭,但临时有事加了会班,回家晚了点 回到家,本以为老婆会很生气,但老婆却立即从厨房端出了热着的饭菜 老婆:还没吃饭吧,去洗下,来吃饭吧 我洗好,坐下吃饭,内心感 ...
- Mysql基础篇(一)之DDL、DML、DQL、DCL
一. SQL语句分类 SQL语句,根据其功能,主要分为四类:DDL.DML.DQL.DCL. 分类 全称 说明 DDL Data Definition Language 数据定义语言,用来定义数据对象 ...
- Spring MVC 前后台传递json格式数据 Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported
若使用默认的json转换器,则需要如下包: <mvc:annotation-driven /> 报错如下: Content type 'application/x-www-form-url ...
- 即构发布 LCEP 低代码互动平台产品 RoomKit,实现互动房间0代码搭建
2月5日,全球云通讯服务商ZEGO即构科技发布低代码互动平台 LCEP(Low-code Engagement Platform)产品 RoomKit,支持1V1在线课堂.小班课.大班课.视频会议.视 ...
- ubuntu发行版内核源码下载
Ubuntu 发行版linux内核在哪里? 内核安装包:http://archive.ubuntu.com/ubuntu/pool/main/l/linux/ 内核源码:https://git.lau ...