Search Unity

How to change the texture format in assetbundles.

Discussion in 'Editor & General Support' started by Mandoz, Sep 5, 2015.

  1. Mandoz

    Mandoz

    Joined:
    Aug 30, 2013
    Posts:
    9
    Hi guys,

    I tried to change the texture format of assetbundles to use proper textures for each graphic devices. (ETC, DXT, PVRTC, ATC). So I followed the steps in "http://docs.unity3d.com/460/Documentation/Manual/BuildingAssetBundles.html"

    However Binary data of texture(PVRTC)bundle is equal to others(RTC, ATC,DXT)

    Here is the function I used to change the texture format during building assetbundle.


    Code (CSharp):
    1. void OnPreprocessTexture()
    2.     {
    3.         if (TGDistributeSystemWindow.IsNowBuilding == false)
    4.         {
    5.             return;
    6.         }
    7.         TextureImporter textureImporter = assetImporter as TextureImporter;
    8.         TextureImporterFormat newFormat = TextureImporterFormat.Alpha8;
    9.         TextureImporterFormat OriTexFormat = TextureImporterFormat.Alpha8;
    10.  
    11.         if (textureFormat == TextureImporterFormat.ETC_RGB4)
    12.         {
    13.             if (textureImporter.DoesSourceTextureHaveAlpha() == false)
    14.             {
    15.                 newFormat = TextureImporterFormat.ETC_RGB4;
    16.             }
    17.             else
    18.             {
    19.                 newFormat = TextureImporterFormat.RGBA16;
    20.             }
    21.         }
    22.         else if (textureFormat == TextureImporterFormat.PVRTC_RGB4)
    23.         {
    24.             if (textureImporter.DoesSourceTextureHaveAlpha() == false)
    25.             {
    26.                 newFormat = TextureImporterFormat.PVRTC_RGB4;
    27.             }
    28.             else
    29.             {
    30.                 newFormat = TextureImporterFormat.PVRTC_RGBA4;
    31.             }
    32.         }
    33.         else if (textureFormat == TextureImporterFormat.ATC_RGB4)
    34.         {
    35.             if (textureImporter.DoesSourceTextureHaveAlpha() == false)
    36.             {
    37.                 newFormat = TextureImporterFormat.ATC_RGB4;
    38.             }
    39.             else
    40.             {
    41.                 newFormat = TextureImporterFormat.RGBA16;
    42.             }
    43.         }
    44.         else if (textureFormat == TextureImporterFormat.DXT5)
    45.         {
    46.             if (textureImporter.DoesSourceTextureHaveAlpha() == false)
    47.             {
    48.                 newFormat = TextureImporterFormat.DXT1;
    49.             }
    50.             else if(textureImporter.assetPath.Contains("ExportResources/UI/") && TGDistributeSystemWindow.CurBuildTarget == BuildTarget.StandaloneWindows)
    51.             {
    52.                 newFormat = TextureImporterFormat.RGBA32;
    53.             }
    54.             else
    55.             {
    56.                 newFormat = TextureImporterFormat.DXT5;
    57.             }
    58.            
    59.  
    60.         }
    61.  
    62.         //textureImporter.textureFormat = newFormat;
    63.         int maxtexSize;
    64.         textureImporter.textureType = TextureImporterType.Advanced;
    65.         textureImporter.generateMipsInLinearSpace = false;
    66.  
    67.         if (textureImporter.assetPath.Contains("UI/"))
    68.         {
    69.             textureImporter.filterMode = FilterMode.Bilinear;
    70.         }
    71.         else
    72.         {
    73.             textureImporter.filterMode = FilterMode.Bilinear;
    74.         }
    75.  
    76.         textureImporter.GetPlatformTextureSettings(TGDistributeSystemWindow.BuildingTarget.ToString(), out maxtexSize, out OriTexFormat);
    77.  
    78.         if (newFormat != OriTexFormat)
    79.         {
    80.             textureImporter.SetPlatformTextureSettings(TGDistributeSystemWindow.BuildingTarget.ToString(), maxtexSize, newFormat);
    81.         }
    82.     }
     
  2. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Unless I'm missing something you are trying to do, it sounds like you are setting them for each device correct? (iOS, android, win, etc). Is this correct?

    If so, you can skip the format changes in code, set them in the build setting panel by device, the just switch platform for each target platform and build. ( in different locations.)
     
  3. Mandoz

    Mandoz

    Joined:
    Aug 30, 2013
    Posts:
    9
    Hi Zombie,

    Thanks for your reply.

    Sorry, I should gave more information to you.
    I'm trying to set them for each graphic vendors, not for os.
    FYI : each android devices use different texture format related to which graphic card they use.
     
  4. fffMalzbier

    fffMalzbier

    Joined:
    Jun 14, 2011
    Posts:
    3,276
    For android builds you can set to overwrite the Texture compression in the build dialog.
    I did not test if that affects the asset bundles but i would guess so.