# Unity 如何獲取Texture 的記憶體大小

来源:https://www.cnblogs.com/alianblank/archive/2023/08/17/17639067.html
-Advertisement-
Play Games

# Unity 如何獲取Texture 的記憶體大小 在Unity中,要獲取Texture的記憶體文件大小,可以使用UnityEditor.TextureUtil類中的一些函數。這些函數提供了獲取存儲記憶體大小和運行時記憶體大小的方法。由於UnityEditor.TextureUtil是一個內部類,我們需要 ...


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類型。

__EOF__

本文作者 Blank
本文鏈接
版權聲明:本博客所有文章除特別聲明外,均採用 BY-NC-SA 許可協議。轉載請註明出處!
聲援博主:如果您覺得文章對您有幫助,可以點擊文章右下角 推薦 一下。您的鼓勵是博主的最大動力!

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • # 【狂神說Java】Java零基礎學習筆記-異常 ## 異常01:Error和Exception ### 什麼是異常 - 實際工作中,遇到的情況不可能是非常完美的。比如:你寫的某個模塊,用戶輸入不一定符合你的要求、你的程式要打開某個文件,這個文件可能不存在或者文件格式不對,你要讀取資料庫的數據,數 ...
  • ### 1. json.load(json_data)與json.dump(python_data) json.load()用來將讀取json文件,json.dump()用來將數據寫入json文件 ### 2. json.loads()與json.dumps() - json.dumps 將 Pyt ...
  • 使用python爬蟲爬取數據的時候,經常會遇到一些網站的反爬蟲措施,一般就是針對於headers中的User-Agent,如果沒有對headers進行設置,User-Agent會聲明自己是python腳本,而如果網站有反爬蟲的想法的話,必然會拒絕這樣的連接。 而修改headers可以將自己的爬蟲腳本 ...
  • C++ STL(Standard Template Library)是C++標準庫中的一個重要組成部分,提供了豐富的模板函數和容器,用於處理各種數據結構和演算法。在STL中,排序、算數和集合演算法是常用的功能,可以幫助我們對數據進行排序、統計、查找以及集合操作等。STL提供的這些演算法,能夠滿足各種數據處... ...
  • 為衡量個人能力水平自創的一套評分機制,根據時間、代碼行數、基礎理論三個變數生成。使用lua語言編寫,輸出成三個markdown表格。 ...
  • > 本文以一個通過正常註冊攔截器流程註冊攔截器失敗的實際場景,來帶領大家閱讀源碼,體會Spring的HandlerInterceptor攔截器整個工作流程 ### 簡單認識 org.springframework.web.servlet.HandlerInterceptor是Spring框架中的一個 ...
  • # 【狂神說Java】Java零基礎學習筆記-面向對象 ## 面向對象01:什麼是面向對象 ### 面向過程&面向對象 - 面向過程思想 - 步驟清晰簡單,第一步做什麼,第二步做什麼.... - 面對過程適合處理一些較為簡單的問題 - 面向對象思想 - 物以類聚,**分類**的思維模式,思考問題首先 ...
  • **即時編譯器優化技術一覽:** ![](https://img2023.cnblogs.com/blog/3256961/202308/3256961-20230816153001309-163136082.png) ![](https://img2023.cnblogs.com/blog/325 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...