国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Unity減少發(fā)布打包文件的體積(二)——設(shè)置WebGL發(fā)布時(shí)每張圖片的壓縮方式

這篇具有很好參考價(jià)值的文章主要介紹了Unity減少發(fā)布打包文件的體積(二)——設(shè)置WebGL發(fā)布時(shí)每張圖片的壓縮方式。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

一個(gè)項(xiàng)目在發(fā)布成WebGL后,其體積至關(guān)重要,體積太大,用戶加載會(huì)經(jīng)歷一個(gè)漫長的等待…輕則罵娘,重則用腳把電腦踢爛(扣質(zhì))…

那么如何減少發(fā)布后的體積呢,本文從圖片的壓縮開始入手。

前傳回顧:
Unity減少發(fā)布打包文件的體積(一)——獲取精靈圖片的信息限制它的大小

一、徒手設(shè)置每張圖片的壓縮方法

在assets文件夾里選中一個(gè)Image,在Inspector底部有一個(gè)各發(fā)布平臺(tái)的壓縮設(shè)置,如下圖中4的部分。
在此處設(shè)置壓縮格式時(shí),只針對發(fā)布時(shí)進(jìn)行壓縮,不會(huì)修改工程資源的原始文件,這樣如果你發(fā)布成exe時(shí),可以用高清的設(shè)置(而如果直接改了原始圖片,則發(fā)布成exe時(shí),畫質(zhì)被降低)。
unity webgl 圖片格式,unity,webgl,圖片壓縮

1、壓縮格式Format的說明(來源Claude.ai):

  • ASTC (Adaptive Scalable Texture Compression): 這是一個(gè)非常靈活的紋理壓縮格式,可以提供一系列不同的壓縮比,以適應(yīng)各種不同的應(yīng)用需求。ASTC由ARM開發(fā),旨在為各種不同的2D和3D內(nèi)容提供優(yōu)質(zhì)的可壓縮性能。它被廣泛用于移動(dòng)設(shè)備,特別是那些使用ARM集成電路的設(shè)備。

  • ETC (Ericsson Texture Compression): ETC是一種開放的、有損的紋理壓縮格式,使用在OpenGL和OpenGL ES。ETC1版本不支持alpha通道,而ETC2支持alpha通道。由于其廣泛的設(shè)備支持和合理的壓縮效果,ETC被廣泛應(yīng)用于移動(dòng)設(shè)備。

  • DXT (DirectXTex): 也被稱為S3TC,這是一種有損的紋理壓縮格式,主要應(yīng)用在DirectX場景。DXT格式為紋理提供5:1的壓縮比率且支持alpha通道,對于PC和一些游戲主機(jī)上的3D應(yīng)用,通常采用DXT格式。

  • BC (Block Compression): BC是一系列DirectX的一部分提供的紋理壓縮方法,這其中包括DXT。BC系列(包括BC1-BC7)是幾種特殊用途的格式,它們在紋理分辨率、細(xì)節(jié)和壓縮比率上提供多種選擇,但并非所有設(shè)備都支持。

  • EAC (Ericsson Alpha Compression): 這是ETC2壓縮方法的一部分,用于對alpha通道進(jìn)行壓縮。EAC提供了更好的顏色和alpha通道保真度,特別適合那些需要用到透明效果的游戲或者帶有alpha混合的特效。

2、MaxSize的計(jì)算

Unity中如果你不單獨(dú)設(shè)置,它的默認(rèn)值大概就是2048,你可以寫一個(gè)函數(shù),根據(jù)分辨力來計(jì)算maxSize的值。
unity webgl 圖片格式,unity,webgl,圖片壓縮

/// <summary>
/// 獲取圖片的分辨率,取分辨率中高寬的最大值,然后返回圖片的【MaxSize】
/// MaxSize的定義:assets->Image->【Texture2D ImportSettings】->【Override For WebGL】->【Max Size】 
/// 區(qū)間:16,32,64,128,256,512,1024,2048,4096,8192,16384
///
/// 舉例:圖片分辨率 = 12 * 24,那么圖片的MaxSize = 32
/// </summary>
/// <param name="texture"></param>
/// <returns></returns>
public static int GetMaxSize(Texture2D texture)
{
   //分辨率區(qū)間的預(yù)備
   var start = new List<int> { 0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 };
   var end = new List<int> { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 100000 };
   var zones = start.Zip(end, (item1, item2) => (startIdx: item1, endIdx: item2)).ToList();

   //取分辨率高寬的最大值
   var size = new List<int> { texture.width, texture.height }.Max();  //取【寬】【高】中的最大值

   //判斷所屬的區(qū)間
   var maxSize = zones.First(x => x.startIdx <= size && size <= x.endIdx).endIdx;
   //Debug.Log($"圖的分辨率 = {texture.width} * {texture.height} size = {size}, MaxSize = {maxSize}");
   return maxSize;
}

3、 Format的設(shè)置:

Unity面板上提供的各種壓縮算法:
unity webgl 圖片格式,unity,webgl,圖片壓縮
自己親自指揮親自部署時(shí),代碼中可選的壓縮格式:
unity webgl 圖片格式,unity,webgl,圖片壓縮

4、提問:所有的圖片能不能一鍵設(shè)置呢?

可以的,你可以自己寫一個(gè)編輯器腳本(俗稱插件),也可以讓AI幫你寫一個(gè)…然后自己慢慢改

二、一鍵設(shè)置所有圖片的Build時(shí)的壓縮選項(xiàng)

1、快捷菜單入口

右鍵全選圖片 -> 【設(shè)置發(fā)布WebGL時(shí)貼圖的壓縮格式】
unity webgl 圖片格式,unity,webgl,圖片壓縮

處理中…
unity webgl 圖片格式,unity,webgl,圖片壓縮
unity webgl 圖片格式,unity,webgl,圖片壓縮

2、壓縮效果

壓縮結(jié)束后,發(fā)現(xiàn)包的大小從230M減少到138兆。

3、實(shí)現(xiàn)的基本思路

  • (1)找到所有的全選物體
Object[] textures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);

注意:上述寫法容易爆內(nèi)存,假如我選取了2000多張圖,有的圖還是4k的,當(dāng)圖片加載的時(shí)候,內(nèi)存暴漲,然后就是程序崩潰,電腦死機(jī)。使用場景就是只能選取少量的圖片。

自動(dòng)獲取圖片并處理:

  • 獲取所有圖片的信息
string[] guids = AssetDatabase.FindAssets("t:texture2d");
  • 獲取圖片路徑
string path = AssetDatabase.GUIDToAssetPath(guid);//guid是guids的items
  • 通過路徑加載圖片
Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
  • 圖片設(shè)置
......
  • (2)單個(gè)物體的壓縮選項(xiàng)設(shè)定

      創(chuàng)建特定平臺(tái)壓縮實(shí)例
      勾選壓縮
      設(shè)置參數(shù)
      Apply
    
// 創(chuàng)建特定平臺(tái)壓縮實(shí)例
TextureImporterPlatformSettings platformSettings = new TextureImporterPlatformSettings();
platformSettings.overridden = true;
platformSettings.name = "WebGL";

// 設(shè)置為壓縮
platformSettings.textureCompression = TextureImporterCompression.Compressed;

// 設(shè)置壓縮格式
platformSettings.format = format;                                     //TextureImporterFormat.ASTC_12x12;
platformSettings.compressionQuality = compressionQuality;             //40
platformSettings.maxTextureSize = GetMaxSize(texture as Texture2D);   //32

//設(shè)置importSettings
TextureImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;
importer.SetPlatformTextureSettings(platformSettings);

//Apply 設(shè)置
importer.SetPlatformTextureSettings(platformSettings);

//保存資源
importer.SaveAndReimport();

三、附錄(代碼)

運(yùn)行方式,放到任何Editor文件里

1、選中圖片后右鍵菜單處理方式

using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEditor;

public class SetTextureCompression
{
    //****************************************參數(shù)設(shè)置區(qū)**********begin
    //TODO 做成EditWindow類型

    private static TextureImporterFormat format = TextureImporterFormat.ASTC_12x12;  //圖片壓縮格式
    private static int compressionQuality = 60;                                      //壓縮比例
    private static string platform = "WebGL";                                        //發(fā)布的平臺(tái) 

    //************************************************************end

    /// <summary>
    /// 設(shè)置貼圖在build時(shí)的壓縮選項(xiàng)
    /// </summary>
    [MenuItem("Assets/設(shè)置發(fā)布WebGL時(shí)貼圖的壓縮格式")]
    static void SetCompression()
    {
        int count = 0;

        Object[] textures = Selection.GetFiltered(typeof(Texture2D), SelectionMode.DeepAssets);
        if (textures.Length > 0)
        {
            foreach (Object texture in textures)
            {
                // 創(chuàng)建特定平臺(tái)壓縮實(shí)例
                TextureImporterPlatformSettings platformSettings = new TextureImporterPlatformSettings();
                platformSettings.overridden = true;
                platformSettings.name = platform;

                // 設(shè)置為壓縮
                platformSettings.textureCompression = TextureImporterCompression.Compressed;

                // 設(shè)置壓縮格式
                platformSettings.format = format;                                     //TextureImporterFormat.ASTC_12x12;
                platformSettings.compressionQuality = compressionQuality;             //40
                platformSettings.maxTextureSize = GetMaxSize(texture as Texture2D);   //32

                //設(shè)置importSettings
                TextureImporter importer = AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;
                importer.SetPlatformTextureSettings(platformSettings);

                //Apply 設(shè)置
                importer.SetPlatformTextureSettings(platformSettings);

                //保存資源
                importer.SaveAndReimport();

                count++;
            }
            //Debug.Log("Texture Compression Set!");
        }
        else
        {
            Debug.LogWarning("沒有選中圖片!");
        }
        Debug.Log($"一共處理了{count}張圖片!");
    }

    /// <summary>
    /// 獲取圖片的分辨率,取分辨率中高寬的最大值,然后返回圖片的【MaxSize】
    /// MaxSize的定義:assets->Image->【Texture2D ImportSettings】->【Override For WebGL】->【Max Size】 
    /// 區(qū)間:16,32,64,128,256,512,1024,2048,4096,8192,16384
    ///
    /// 舉例:圖片分辨率 = 12 * 24,那么圖片的MaxSize = 32
    /// </summary>
    /// <param name="texture"></param>
    /// <returns></returns>
    static int GetMaxSize(Texture2D texture)
    {
        //分辨率區(qū)間的預(yù)備
        var start = new List<int> { 0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 };
        var end = new List<int> { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 100000 };
        var zones = start.Zip(end, (item1, item2) => (startIdx: item1, endIdx: item2)).ToList();

        //取分辨率高寬的最大值
        var size = new List<int> { texture.width, texture.height }.Max();  //取【寬】【高】中的最大值

        //判斷所屬的區(qū)間
        var maxSize = zones
            .First(x => x.startIdx <= size && size <= x.endIdx)
            .endIdx;
        //Debug.Log($"圖的分辨率 = {texture.width} * {texture.height} size = {size}, MaxSize = {maxSize}");
        return maxSize;
    }
}

2、自動(dòng)獲取圖片一鍵處理方式

關(guān)鍵代碼【編輯器腳本使用】:文章來源地址http://www.zghlxwxcb.cn/news/detail-767240.html

using System;
using UnityEngine;
using UnityEditor;
using System.Linq;
using System.IO;
using System.Collections.Generic;
class Example : EditorWindow
{
#if UNITY_EDITOR
    [MenuItem("模型處理/查找項(xiàng)目中所有的texture 2d對象并壓縮")]
#endif
    static void FindAllTexture2D()
    {

        //****************************************參數(shù)設(shè)置區(qū)**********begin
        //TODO 做成EditWindow類型

        TextureImporterFormat format = TextureImporterFormat.ASTC_12x12; //圖片壓縮格式
        int compressionQuality = 60; //壓縮比例
        string platform = "WebGL"; //發(fā)布的平臺(tái) 

        //************************************************************end

        //查找工程文件中的所有精靈圖片
        string[] guids = AssetDatabase.FindAssets("t:texture2d");
        Debug.Log($"Found {guids.Length} Texture2d assets.");
        foreach (string guid in guids)
        {
            try
            {
                string path = AssetDatabase.GUIDToAssetPath(guid);

                Debug.Log($"{path}");
                // 使用AssetDatabase加載Texture2D
                Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(path);
                //Debug.Log($"{texture.name}");

                if (texture == null) continue;

                // 創(chuàng)建特定平臺(tái)壓縮實(shí)例
                TextureImporterPlatformSettings platformSettings = new TextureImporterPlatformSettings();
                platformSettings.overridden = true;
                platformSettings.name = platform;

                // 設(shè)置為壓縮
                platformSettings.textureCompression = TextureImporterCompression.Compressed;

                // 設(shè)置壓縮格式
                platformSettings.format = format; //TextureImporterFormat.ASTC_12x12;
                platformSettings.compressionQuality = compressionQuality; //40
                platformSettings.maxTextureSize = GetMaxSize(texture as Texture2D); //32

                //設(shè)置importSettings
                TextureImporter importer =
                    AssetImporter.GetAtPath(AssetDatabase.GetAssetPath(texture)) as TextureImporter;
                if (importer == null) continue;

                importer.SetPlatformTextureSettings(platformSettings);

                //Apply 設(shè)置
                importer.SetPlatformTextureSettings(platformSettings);

                //保存資源
                importer.SaveAndReimport();
            }
            catch (Exception ex)
            {
                Debug.Log( $" ~~~~~error~~~~~ 設(shè)置報(bào)錯(cuò):{ex.Message}");
            }
        }
    }

    /// <summary>
    /// 獲取圖片的分辨率,取分辨率中高寬的最大值,然后返回圖片的【MaxSize】
    /// MaxSize的定義:assets->Image->【Texture2D ImportSettings】->【Override For WebGL】->【Max Size】 
    /// 區(qū)間:16,32,64,128,256,512,1024,2048,4096,8192,16384
    ///
    /// 舉例:圖片分辨率 = 12 * 24,那么圖片的MaxSize = 32
    /// </summary>
    /// <param name="texture"></param>
    /// <returns></returns>
    static int GetMaxSize(Texture2D texture)
    {
        //分辨率區(qū)間的預(yù)備
        var start = new List<int> { 0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384 };
        var end = new List<int> { 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 100000 };
        var zones = start.Zip(end, (item1, item2) => (startIdx: item1, endIdx: item2)).ToList();

        //取分辨率高寬的最大值
        var size = new List<int> { texture.width, texture.height }.Max();  //取【寬】【高】中的最大值

        //判斷所屬的區(qū)間
        var maxSize = zones
            .First(x => x.startIdx <= size && size <= x.endIdx)
            .endIdx;
        //Debug.Log($"圖的分辨率 = {texture.width} * {texture.height} size = {size}, MaxSize = {maxSize}");
        return maxSize;
    }
}

到了這里,關(guān)于Unity減少發(fā)布打包文件的體積(二)——設(shè)置WebGL發(fā)布時(shí)每張圖片的壓縮方式的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • vue項(xiàng)目打包優(yōu)化:縮小體積productionSourceMap設(shè)置,使用cdn加速

    一、vue項(xiàng)目打包體積大優(yōu)化之productionSourceMap設(shè)置 1、productionSourceMap 的作用 productionSourceMap 在構(gòu)建時(shí)生成完整的 SourceMap 文件,默認(rèn)情況下開啟。生產(chǎn)環(huán)境中啟用 productionSourceMap 有助于開發(fā)者調(diào)試代碼,可以在瀏覽器的調(diào)試工具中查看到源文件中錯(cuò)誤的代碼位置,而不是編譯后

    2024年02月21日
    瀏覽(25)
  • Unity打包WebGL: 導(dǎo)入Vue

    Unity打包WebGL: 導(dǎo)入Vue

    1.1 任務(wù) 記錄將 Unity 項(xiàng)目打包成 WebGL ,并集成到 Vue 項(xiàng)目中的過程。 1.2 環(huán)境 Unity : 2021.3 Vue : 2 2.1 UI 界面 2.2 添加插件 構(gòu)建 WebGL 項(xiàng)目需要添加一個(gè) .jslib 文件,用于 Unity 腳本函數(shù)與 JavaScript 函數(shù)交互 詳細(xì)內(nèi)容參考:Unity(WebGL)與JS通訊2022最新姿勢 web.jslib 文件內(nèi)容 2.3 添加腳

    2024年02月11日
    瀏覽(18)
  • Unity打包WebGL的優(yōu)化常用操作?

    如果貼圖格式時(shí)2048,在不影響畫面效果的情況下,改成1024或者5#12,還可以縮小包體。 WebGL打包的時(shí)候分三種壓縮情況: gzip:比Brotli文件打,但打包快,http和https都支持 Brotli:壓縮格式最小,打包慢,只有谷歌和火狐支持。 Disabled:不壓縮。 直接打包一份不壓縮的版本,在

    2024年02月06日
    瀏覽(27)
  • unity WebGL 發(fā)布服務(wù)后出錯(cuò)

    解決辦法:需要在IIS中添加unity3d和unityweb兩個(gè)mime 1、電腦怎么添加mime 2、UNITY3D WEBGL IIS發(fā)布添加MIME類型 我在添加unity3d和unityweb兩個(gè)mime后 已經(jīng)解決此問題

    2024年02月11日
    瀏覽(20)
  • Unity WebGL發(fā)布頁面報(bào)錯(cuò)

    Unity WebGL發(fā)布頁面報(bào)錯(cuò)

    1、報(bào)錯(cuò)內(nèi)容 Unable to parse Build/test.framework.js.gz! This can happen if build compression was enabled but web server hosting the content was misconfigured to not serve the file with HTTP Response Header \\\"Content-Encoding: gzip\\\" present. Check browser Console and Devtools Network tab to debug. 2、報(bào)錯(cuò)頁面 解決方案,在Unity的WebGL?Player S

    2024年02月16日
    瀏覽(22)
  • unity發(fā)布WebGL遇到的坑(持續(xù)更新)

    unity發(fā)布WebGL遇到的坑(持續(xù)更新)

    1、unity默認(rèn)字體在網(wǎng)頁中不會(huì)顯示 解決方法:自己新導(dǎo)入一個(gè)字體,使用導(dǎo)入的字體 2、之前打過包并運(yùn)行過,后面又在unity中進(jìn)行了修改,重新打包,運(yùn)行發(fā)現(xiàn)還是修改之前的效果,雖然是新包, 解決方法:這是因?yàn)榫W(wǎng)頁中有緩存, 點(diǎn)擊瀏覽器右邊的三個(gè)點(diǎn),選擇設(shè)置–隱

    2024年02月12日
    瀏覽(23)
  • 將WebGL打包的unity項(xiàng)目部署至Vue中

    將WebGL打包的unity項(xiàng)目部署至Vue中

    創(chuàng)建一個(gè)空項(xiàng)目(或者直接使用現(xiàn)成的項(xiàng)目都可以)這里以該空項(xiàng)目為例子 注意: 如果你的unity項(xiàng)目中有文字,不需要使用unity默認(rèn)的字體,需要更改它的字體,否則在最后生成的頁面中會(huì)顯示不出來文字 好在你的windows在C盤自帶了字體,我這里使用的微軟雅黑來進(jìn)行了替換

    2024年02月02日
    瀏覽(23)
  • unity webgl開發(fā)踩坑——從開發(fā)、發(fā)布到優(yōu)化

    unity webgl開發(fā)踩坑——從開發(fā)、發(fā)布到優(yōu)化

    又是一個(gè)陽光明媚的早上,突然老板召集開會(huì)說要將一個(gè)android項(xiàng)目適配webgl,沒辦法趕緊用unity改一下踩踩坑;這里記錄一下這些天的踩坑過程。 使用unity2021.3.4f1c1,visual studio2019,visual studio code videoplayer僅適用于安卓和PC,如果有蘋果適配的需求的話,那就要用到這個(gè)插件:

    2024年02月02日
    瀏覽(17)
  • Unity發(fā)布webgl獲取瀏覽器的URL

    Unity發(fā)布webgl獲取瀏覽器的URL

    Unity發(fā)布webgl之后獲取瀏覽器的url 在unity中創(chuàng)建文件夾 Plugins ,然后添加添加文件 UnityGetBrowserURL.jslib 在Unity中添加代碼 場景布局 發(fā)布webgl 拷貝到nginx 運(yùn)行結(jié)果 在地址欄輸入 Ip:端口號(hào)/?serligblsdhroivbaelirbgvkersab , /? 后面是隨便打的字符,然后按下enter會(huì)刷新網(wǎng)頁并重新顯示URL,(

    2024年03月20日
    瀏覽(30)
  • Unity 發(fā)布WebGL、去Logo、網(wǎng)絡(luò)端通信 、本地運(yùn)行

    Unity 發(fā)布WebGL、去Logo、網(wǎng)絡(luò)端通信 、本地運(yùn)行

    以下內(nèi)容將和大家詳細(xì)分享 Unity 在 WebGL平臺(tái)的發(fā)布方法、 如何去除unity的Logo和加載界面、 WebGL與網(wǎng)絡(luò)端通信 、以及 如何在本地運(yùn)行html。 一、Unity在 WebGL平臺(tái)的發(fā)布方法 1、如下圖,選擇webgl平臺(tái),沒安裝的點(diǎn)擊下載安裝。 ?安裝后如圖。 ?選擇需要打包的場景,無特殊要求

    2024年02月06日
    瀏覽(31)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包