How to get the memory size of Texture
In Unity, to get the memory file size of Texture, you can use some functions in the class. These functions provide methods to get the storage memory size and runtime memory size. Since it is an inner class, we need to use reflection to access it.
step
- Import the UnityEditor namespace and namespace:
using UnityEditor; using ;
- Create a function to get the memory file size of Texture:
public static long GetTextureFileSize(Texture2D texture) { long fileSize = 0; // Use reflection to get the class's Type Type textureUtilType = typeof(TextureImporter).(""); // GetStorageMemorySizeLong method of class using reflection MethodInfo getStorageMemorySizeLongMethod = ("GetStorageMemorySizeLong", | ); // Call GetStorageMemorySizeLong method to get the storage memory size fileSize = (long)(null, new object[] { texture }); return fileSize; }
- Create a function to get the runtime memory size of Texture:
public static long GetTextureRuntimeMemorySize(Texture2D texture) { long memorySize = 0; // Use reflection to get the class's Type Type textureUtilType = typeof(TextureImporter).(""); // GetRuntimeMemorySizeLong method of class using reflection MethodInfo getRuntimeMemorySizeLongMethod = ("GetRuntimeMemorySizeLong", | ); // Call GetRuntimeMemorySizeLong method to get runtime memory size memorySize = (long)(null, new object[] { texture }); return memorySize; }
Sample code
Example 1: Get the storage memory size of Texture
using UnityEngine; using UnityEditor; using ; public class TextureSizeExample : MonoBehaviour { [SerializeField] private Texture2D texture; private void Start() { long fileSize = GetTextureFileSize(texture); ("Texture File Size: " + fileSize + " bytes"); } private static long GetTextureFileSize(Texture2D texture) { long fileSize = 0; Type textureUtilType = typeof(TextureImporter).(""); MethodInfo getStorageMemorySizeLongMethod = ("GetStorageMemorySizeLong", | ); fileSize = (long)(null, new object[] { texture }); return fileSize; } }
Example 2: Get the runtime memory size of Texture
using UnityEngine; using UnityEditor; using ; public class TextureSizeExample : MonoBehaviour { [SerializeField] private Texture2D texture; private void Start() { long memorySize = GetTextureRuntimeMemorySize(texture); ("Texture Runtime Memory Size: " + memorySize + " bytes"); } private static long GetTextureRuntimeMemorySize(Texture2D texture) { long memorySize = 0; Type textureUtilType = typeof(TextureImporter).(""); MethodInfo getRuntimeMemorySizeLongMethod = ("GetRuntimeMemorySizeLong", | ); memorySize = (long)(null, new object[] { texture }); return memorySize; } }
Example 3: Get both the storage memory size and the runtime memory size of Texture
using UnityEngine; using UnityEditor; using ; public class TextureSizeExample : MonoBehaviour { [SerializeField] private Texture2D texture; private void Start() { long fileSize = GetTextureFileSize(texture); long memorySize = GetTextureRuntimeMemorySize(texture); ("Texture File Size: " + fileSize + " bytes"); ("Texture Runtime Memory Size: " + memorySize + " bytes"); } private static long GetTextureFileSize(Texture2D texture) { long fileSize = 0; Type textureUtilType = typeof(TextureImporter).(""); MethodInfo getStorageMemorySizeLongMethod = ("GetStorageMemorySizeLong", | ); fileSize = (long)(null, new object[] { texture }); return fileSize; } private static long GetTextureRuntimeMemorySize(Texture2D texture) { long memorySize = 0; Type textureUtilType = typeof(TextureImporter).(""); MethodInfo getRuntimeMemorySizeLongMethod = ("GetRuntimeMemorySizeLong", | ); memorySize = (long)(null, new object[] { texture }); return memorySize; } }
Things to note
- Make sure that the UnityEditor namespace and namespace have been imported before using reflection access classes.
- When using reflection, you need to use | to get a static public method.
- In the example code, we use a variable of the Texture2D type to represent Texture. You can modify the code according to the actual situation to adapt to different Texture types.
The above is the detailed explanation of how Unity gets Texture memory size method. For more information about Unity getting Texture memory size, please follow my other related articles!