How can we change the global texture compression type (DXT/ASTC/ETC2) programatically

I want to change the global webgl texture compression setting that is available in the editor in the BuildSettings window, but I can’t seem to find the option in EditorBuildSetting to do it programatically. My goal is to set it to a different compression types on one of my UnityCloudBuild configs. I am talking about this option here:


Thanks!

Hi!

Here’s a script I wrote that builds a project twice, each time with a different texture compression target

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Diagnostics;
using System.IO;

public class comboBuild
{
    [MenuItem("Custom Menu/Show WebGL Subtarget")]
    private static void NewMenuOption()
    {
        UnityEngine.Debug.Log(EditorUserBuildSettings.webGLBuildSubtarget);
    }
   
    [MenuItem("Custom Menu/Combo Build")]
    public static void BuildGame ()
    {
        // Get filename.
       // string path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", "");
       
        string path = System.IO.Directory.GetCurrentDirectory() + "/Builds";
        if (!Directory.Exists(path)) {
            Directory.CreateDirectory(path);
        }
        string dxtBuildName = "WebGL_DXT";
        string astcBuildName = "WebGL_ASTC";

        string dxtBuildPath = path + "/" + dxtBuildName;
        string astcBuildPath = path + "/" + astcBuildName;
        string[] levels = new string[] {"Assets/Scenes/scene_0.unity"};

        // Build player.
        EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.DXT;
        BuildPipeline.BuildPlayer(levels, dxtBuildPath, BuildTarget.WebGL, BuildOptions.None);
        EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;
        BuildPipeline.BuildPlayer(levels, astcBuildPath, BuildTarget.WebGL, BuildOptions.None);
     
    }
}

Please let me know if this helps and if you run into any issues using it :slight_smile:

4 Likes

Thank you for this!
I’ve tried locally and it works fine. In UnityCloudBuild if I just switch to the EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC; at Pre-build, that build doesn’t seem to be affected by this setting. I’m a bit reluctant to try and run BuildPipeline.BuildPlayer() at Pre-build in Cloud build as this seems to be a method for something else; Plus the paths in cloud are a bit obscured and it will also double the build time as it will do the normal build and the “pre-build” that I try to sneak in.
So, any ideas on how to setup a Unity Cloud Build config to use ASTC and another that uses DXT. They both use the same unique git repo.

@bogdan-serbanescu_1

You can use a Pre-Export Method that will run before the build in order to change the player settings you need (I’ve attached a screenshot), so you could have a method like this:

public static void ChangeTextureCompression ()
    {    
        EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.DXT;
    }

and put its name in that field.
EDIT: the value in the field will look something like:
Namespace.Class.StaticMethod

Since you want to have use both compression formats, you could have two build targets, each one with a Pre-Build Method like the one above.

I hope that helps!

Hi guys,

I tried this code and it seems ok in the Build Settings window

EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;

but when i call this :

BuildPipeline.BuildPlayer(levels, astcBuildPath, BuildTarget.WebGL, BuildOptions.None);

It goes back to “Use default format (DXT)” while building and I don’t know why.
I make only one build and I set webGLBuildSubtarget only to ASTC.

Any idea ?

Your suggestion seems to be working. The issues I encountered when trying this seem to be related to something else entirely. Thank you!

For me it worked to define the webGLBuildSubtarget with ASTC in the build options, I think this should work for you as well :slight_smile:

BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
// This is the important line
buildPlayerOptions.subtarget = (int)WebGLTextureSubtarget.ASTC;
EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;
buildPlayerOptions.scenes = scenes;
buildPlayerOptions.target = buildTarget;
buildPlayerOptions.locationPathName = filePath;

BuildPipeline.BuildPlayer(buildPlayerOptions);
1 Like