Unity 如何获取Texture 的内存大小

在Unity中,要获取Texture的内存文件大小,可以使用UnityEditor.TextureUtil类中的一些函数。这些函数提供了获取存储内存大小和运行时内存大小的方法。由于UnityEditor.TextureUtil是一个内部类,我们需要使用反射来访问它。

步骤

  1. 导入UnityEditor命名空间和System.Reflection命名空间:
using UnityEditor;
using System.Reflection;
  1. 创建一个函数来获取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;
}
  1. 创建一个函数来获取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 的内存大小的更多相关文章

  1. 获取JAVA对象占用的内存大小

    介绍两种获取JAVA对象内存大小的方法. 第一种:Instrumentation 简介: 使用java.lang.instrument 的Instrumentation来获取一个对象的内存大小.利用I ...

  2. unity texture 占用内存大小对比

    打包多种类型的项目,空项目和10张放在Resources文件夹中的图为比较案例.以下是比较数据. IPHONE: 1.空项目----空间占用量42.3MB----IPA大小10MB 2.10张1200 ...

  3. Android中一张图片加载后所占用内存大小的获取与测试

    Android程序中一旦加载的图片比较多,就有可能出现Out of Memory而导致程序崩溃.这个一方面是因为Android系统本身对于每个单独的进程有内存大小的限制(有16M,64M,128M,2 ...

  4. iOS 获取内存大小使用情况(进度条显示)

    一.获取设备内存大小方法 //返回存储内存占用比例 - (NSString *)getFreeDiskspaceRate{ float totalSpace; .f; NSError *error = ...

  5. linux下获取硬盘、内存、U盘大小及使用大小

    /* * 获取硬盘大小;内存大小;usb大小 */ #ifndef SYSINFOGET_H #define SYSINFOGET_H #include <stdio.h> //磁盘信息 ...

  6. C#获取电脑型号、系统版本、内存大小、硬盘大小、CPU信息

    摘要 有时需要获取电脑的相关信息.这时可以通过调用windows api的方式,进行获取. 方法 可以通过在powershell中 通过下面的命令进行查询,然后可以通过c#调用获取需要的信息. gwm ...

  7. 获取 iOS APP 内存占用的大小

    当我们想去获取 iOS 应用的占用内存时,通常我们能找到的方法是这样的,用 resident_size:   #import <mach/mach.h> - (int64_t)memory ...

  8. C# 利用WMI对象获取物理内存和可用内存大小

    下面的代码演示的是使用WMI对象可获取取物理内存和可用内存大小,在使用WMI对象前,先要添加对System.Management的引用,然后就可以调用WMI对象,代码如下: //获取总物理内存大小 M ...

  9. .net 获取CPU频率 内存 磁盘大小,域名 端口 虚拟目录等

    CPU个数: @Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS") CPU类型: @Environment.Get ...

  10. 如何获取一个Java对象所占内存大小

    新建一个maven工程 我们先在IDEA中新建一个名为ObjectSizeFetcherAgent的maven工程,如下图: 在maven项目中的pom.xml中新增一个打jar包的插件,如下: &l ...

随机推荐

  1. 2022-03-24:你被请来给一个要举办高尔夫比赛的树林砍树,树林由一个 m x n 的矩阵表示, 在这个矩阵中: 0 表示障碍,无法触碰 1 表示地面,可以行走 比 1 大的数 表示有树的单元格,

    2022-03-24:你被请来给一个要举办高尔夫比赛的树林砍树,树林由一个 m x n 的矩阵表示, 在这个矩阵中: 0 表示障碍,无法触碰 1 表示地面,可以行走 比 1 大的数 表示有树的单元格, ...

  2. Accurate SerDes LineRate

    测试现象:(单板A板载7A series,单板B板载7K series) 1. 单板A板载2个type C接口,通过线缆自环,GTP near-end loopback, OK. 2. 2块单板A,使 ...

  3. AIGC持续火爆大模型争相推出,庞大市场造就算力供应模式演变

    本图由AI生成 黄仁勋说的AI发展迎来iPhone时刻,对NVIDIA有什么影响? 文/王吉伟 近期的AIGC领域仍旧火爆异常. 但火的不只是AIGC应用,还有巨头之间的AI竞赛,以及接连不断上新的A ...

  4. Python异步编程之web框架异步vs同步 无IO任务压测对比

    前言 在python编程中,通过协程实现的异步编程号称能够提高IO密集型任务的并发量.本系列比较web服务器同步框架和异步框架的性能差异,包括无IO接口和常见IO操作,如文件.mysql.redis等 ...

  5. SpringMVC 后台从前端获取单个参数

    1.编写web.xml(模板) 2.springmvc配置文件 3.编写对应数据库字段的pojo实体类 @Data @AllArgsConstructor @NoArgsConstructor pub ...

  6. 驱动开发:内核PE结构VA与FOA转换

    本章将继续探索内核中解析PE文件的相关内容,PE文件中FOA与VA,RVA之间的转换也是很重要的,所谓的FOA是文件中的地址,VA则是内存装入后的虚拟地址,RVA是内存基址与当前地址的相对偏移,本章还 ...

  7. 《Just For Fun》:学习即游戏

    <Just For Fun>:学习即游戏 最近读完了 Linus 的自传<Just For Fun>,一直想写点东西,但始终苦于工作繁忙,无暇思考该从何写起.技术上自然不用废话 ...

  8. GitHub 私有仓库完全免费且不限制协作人数

    GitHub is now free for teams GitHub CEO Nat Friedman 在 2020.04.14 宣布已面向全体 GitHub 用户和团队提供不限制协作人数的私有仓库 ...

  9. RALB负载均衡算法的应用

    一.背景 搜索推荐算法架构为京东集团所有的搜索推荐业务提供服务,实时返回处理结果给上游.部门各子系统已经实现了基于CPU的自适应限流,但是Client端对Server端的调用依然是RR轮询的方式,没有 ...

  10. Supervisor启动并管理Celery相关进程

    Supervisor启动并管理Celery相关进程 关于celery在运行过程中, 默认情况下是无法在关机以后自动重启的.所以我们一般开发中会使用supervisor进程监控来对celery程序进行运 ...