How to change the texture format in assetbundles.

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 “Unity - Manual: Building AssetBundles

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.

void OnPreprocessTexture()
    {
        if (TGDistributeSystemWindow.IsNowBuilding == false)
        {
            return;
        }
        TextureImporter textureImporter = assetImporter as TextureImporter;
        TextureImporterFormat newFormat = TextureImporterFormat.Alpha8;
        TextureImporterFormat OriTexFormat = TextureImporterFormat.Alpha8;

        if (textureFormat == TextureImporterFormat.ETC_RGB4)
        {
            if (textureImporter.DoesSourceTextureHaveAlpha() == false)
            {
                newFormat = TextureImporterFormat.ETC_RGB4;
            }
            else
            {
                newFormat = TextureImporterFormat.RGBA16;
            }
        }
        else if (textureFormat == TextureImporterFormat.PVRTC_RGB4)
        {
            if (textureImporter.DoesSourceTextureHaveAlpha() == false)
            {
                newFormat = TextureImporterFormat.PVRTC_RGB4;
            }
            else
            {
                newFormat = TextureImporterFormat.PVRTC_RGBA4;
            }
        }
        else if (textureFormat == TextureImporterFormat.ATC_RGB4)
        {
            if (textureImporter.DoesSourceTextureHaveAlpha() == false)
            {
                newFormat = TextureImporterFormat.ATC_RGB4;
            }
            else
            {
                newFormat = TextureImporterFormat.RGBA16;
            }
        }
        else if (textureFormat == TextureImporterFormat.DXT5)
        {
            if (textureImporter.DoesSourceTextureHaveAlpha() == false)
            {
                newFormat = TextureImporterFormat.DXT1;
            }
            else if(textureImporter.assetPath.Contains("ExportResources/UI/") && TGDistributeSystemWindow.CurBuildTarget == BuildTarget.StandaloneWindows)
            {
                newFormat = TextureImporterFormat.RGBA32;
            }
            else
            {
                newFormat = TextureImporterFormat.DXT5;
            }
           

        }

        //textureImporter.textureFormat = newFormat;
        int maxtexSize;
        textureImporter.textureType = TextureImporterType.Advanced;
        textureImporter.generateMipsInLinearSpace = false;

        if (textureImporter.assetPath.Contains("UI/"))
        {
            textureImporter.filterMode = FilterMode.Bilinear;
        }
        else
        {
            textureImporter.filterMode = FilterMode.Bilinear;
        }

        textureImporter.GetPlatformTextureSettings(TGDistributeSystemWindow.BuildingTarget.ToString(), out maxtexSize, out OriTexFormat);

        if (newFormat != OriTexFormat)
        {
            textureImporter.SetPlatformTextureSettings(TGDistributeSystemWindow.BuildingTarget.ToString(), maxtexSize, newFormat);
        }
    }

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.)

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.

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.