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. 2021-04-02:给定一个正方形或者长方形矩阵matrix,实现zigzag打印。[[0,1,2],[3,4,5],[6,7,8]]的打印顺序是0,1,3,6,4,2,5,7,8。

    2021-04-02:给定一个正方形或者长方形矩阵matrix,实现zigzag打印.[[0,1,2],[3,4,5],[6,7,8]]的打印顺序是0,1,3,6,4,2,5,7,8. 福大大 答案2 ...

  2. 2021-05-18:Nim博弈。给定一个正数数组arr,先手和后手每次可以选择在一个位置拿走若干值, 值要大于0,但是要小于该处的剩余。谁最先拿空arr,谁赢。根据arr,返回谁赢 。

    2021-05-18:Nim博弈.给定一个正数数组arr,先手和后手每次可以选择在一个位置拿走若干值, 值要大于0,但是要小于该处的剩余.谁最先拿空arr,谁赢.根据arr,返回谁赢 . 福大大 答案 ...

  3. Selenium - 浏览器配置(3) - 沙盒模式/不打开浏览器运行

    Selenium - 浏览器配置 沙盒模式 需要在Linux环境中运行,但不需要打开浏览器进行UI测试,可以加载selenium的无痕模式: 如果使用沙盒模式运行selenium,则部分seleniu ...

  4. vue工程内下载路由

    一.路由Router Vue Router 是Vue.js官方的路由管理器.它和Vue.js的核心深度集成,加速构建单页面应用.包含功能有: 1 嵌套的路由/视图表 2 模块化的.基于组件的路由配置 ...

  5. Shiro 授权绕过 (CVE-2022-32532)

    Shiro 授权绕过 (CVE-2022-32532) 一.产品简介 Apache Shiro是一个强大且易用的Java安全框架,执行身份验证.授权.密码和会话管理. 1.9.1 之前的 Apache ...

  6. APP中RN页面渲染流程-ReactNative源码分析

    在APP启动后,RN框架开始启动.等RN框架启动后,就开始进行RN页面渲染了. RN页面原生侧页面渲染的主要逻辑实现是在RCTUIManager和RCTShadowView完成的. 通过看UIMana ...

  7. 【编程日记】搭建python开发环境

    0.相关确定 0.1确定操作系统 Python是一种跨平台的编程语言,这意味着它能够运行在所有主要的操作系统中.然而,在不同的操作系统(Windows/Mac/Linux)中,安装Python的方法存 ...

  8. Spring Boot 自动配置一篇概览

    一.什么是自动配置 bean 自动配置类通过添加 @AutoConfiguration 注解实现. 因为 @AutoConfiguration 注解本身是以 @Configuration 注解的,所以 ...

  9. Bash 编程

    原文:https://seankross.com/the-unix-workbench/bash-programming.html[1] 数学 创建math.sh: #!/usr/bin/env ba ...

  10. 【QCustomPlot】使用方法(动态库方式)

    说明 使用 QCustomPlot 绘图库辅助开发时整理的学习笔记.同系列文章目录可见 <绘图库 QCustomPlot 学习笔记>目录.本篇介绍 QCustomPlot 的一种使用方法, ...