ToB企服应用市场:ToB评测及商务社交产业平台

标题: # Unity 如何获取Texture 的内存大小 [打印本页]

作者: 风雨同行    时间: 2023-8-29 16:37
标题: # Unity 如何获取Texture 的内存大小
Unity 如何获取Texture 的内存大小

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

  1. using UnityEditor;
  2. using System.Reflection;
复制代码
  1. public static long GetTextureFileSize(Texture2D texture)
  2. {
  3.     long fileSize = 0;
  4.     // 使用反射获取UnityEditor.TextureUtil类的Type
  5.     Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");
  6.     // 使用反射获取UnityEditor.TextureUtil类的GetStorageMemorySizeLong方法
  7.     MethodInfo getStorageMemorySizeLongMethod = textureUtilType.GetMethod("GetStorageMemorySizeLong", BindingFlags.Static | BindingFlags.Public);
  8.     // 调用GetStorageMemorySizeLong方法获取存储内存大小
  9.     fileSize = (long)getStorageMemorySizeLongMethod.Invoke(null, new object[] { texture });
  10.     return fileSize;
  11. }
复制代码
  1. public static long GetTextureRuntimeMemorySize(Texture2D texture)
  2. {
  3.     long memorySize = 0;
  4.     // 使用反射获取UnityEditor.TextureUtil类的Type
  5.     Type textureUtilType = typeof(TextureImporter).Assembly.GetType("UnityEditor.TextureUtil");
  6.     // 使用反射获取UnityEditor.TextureUtil类的GetRuntimeMemorySizeLong方法
  7.     MethodInfo getRuntimeMemorySizeLongMethod = textureUtilType.GetMethod("GetRuntimeMemorySizeLong", BindingFlags.Static | BindingFlags.Public);
  8.     // 调用GetRuntimeMemorySizeLong方法获取运行时内存大小
  9.     memorySize = (long)getRuntimeMemorySizeLongMethod.Invoke(null, new object[] { texture });
  10.     return memorySize;
  11. }
复制代码
示例代码

示例 1:获取Texture的存储内存大小
  1. using UnityEngine;using UnityEditor;
  2. 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的运行时内存大小
  1. using UnityEngine;using UnityEditor;
  2. 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的存储内存大小和运行时内存大小
  1. using UnityEngine;using UnityEditor;
  2. 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;    }}
复制代码
注意事项

            本文作者:                            Blank                            
        本文链接:        
        版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
        声援博主:如果您觉得文章对您有帮助,可以点击文章右下角            【                推荐】                    一下。您的鼓励是博主的最大动力!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4