# 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-01-13:很多列的数据,任意一列组合查询,mysql能做到,但是上亿的数据量做不到了,查的时候非常慢。我们需要一个引擎来支持它。这个引擎你有了解过吗?
福哥答案2021-01-13:[答案来自此链接:](https://www.zhihu.com/question/439121902)数据库存储设计一般分为行存储还有列存储.行存储一般每一行的数据通过 ...
- 2022-05-26:void add(int L, int R, int C)代表在arr[L...R]上每个数加C, int get(int L, int R)代表查询arr[L...R]上的累加
2022-05-26:void add(int L, int R, int C)代表在arr[L-R]上每个数加C, int get(int L, int R)代表查询arr[L-R]上的累加和, 假 ...
- 2021-02-21:手写代码:高性能路由,也就是一个字符串和多个匹配串进行模糊匹配。一个数组arr里是["*a*","moonfdd"],字符串"moonfdd"能匹配到,理由是arr里有。字符串"xayy"也能匹配到,理由是arr里的"*a*",第1个星对应"x",第2个星对应"yy"。
2021-02-21:手写代码:高性能路由,也就是一个字符串和多个匹配串进行模糊匹配.一个数组arr里是["a","moonfdd"],字符串"moo ...
- 【汇编】DOS系统功能调用(INT 21H)
前言 最近又听了听汇编的课程,发现代码里的MOV xxxxx INT 21H,老师都是一句话带过,而不讲讲其中的原因(也可能前面讲了我没有听QAQ). 顺便夸一下老师,老师懒省事录的视频画质已经成功从 ...
- 泰裤辣!!!手摸手教学,如何训练一个你的专属AI歌姬~
最近在做AIGC的项目,不过是与图片相关的,现在的模型效果可比前几年图片替换效果好多了.之前尝试过用 faceswap 工具来进行人脸替换的,具体可以参看下我之前的这篇文章:https://blog. ...
- ChatGPT玩法(三):AI玩转PPT
前言 在线免费体验ChatGpt:https://www.topgpt.one 作为许多职场人士的必备工具,PPT制作一直是一个琐碎而费时的任务.但最近我发现了一个非常有用的工具网站,它可以通过人工智 ...
- 「AntV」L7地理可视化:从入门到实践
1. 前言 这是一篇由浅入深的AntV L7的学习笔记总结,记述了从了解到使用的一些过程 本文所使用的数据(包括数据处理过程)和代码均有详细描述,所有案例均可复现,甚至大部分代码可直接使用 如果喜欢分 ...
- 记一次 .NET 某旅行社审批系统 崩溃分析
一:背景 1. 讲故事 前些天有位朋友找到我,说他的程序跑着跑着就崩溃了,让我看下怎么回事,其实没怎么回事,抓它的 crash dump 就好,具体怎么抓也是被问到的一个高频问题,这里再补一下链接: ...
- UE5打包SDK未正确安装的问题
正文 Windows(笔者之前用的电脑是windows10,最新电脑使用的是windows11)下UE5打包项目的需要安装Visual Studio. 而且安装的时候需要选择上C++ 游戏开发相关模块 ...
- 规则引擎 ice
目录 项目介绍 服务安装 创建数据库(MySQL) 下载安装 服务(启动.停止.重启) 打开后台 Client接入(Spring Boot) 示例 添加配置 新增 ICE liteflow 更适应我们 ...