Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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

Discussion in 'Web' started by bogdan-serbanescu, Nov 17, 2021.

  1. bogdan-serbanescu

    bogdan-serbanescu

    Joined:
    Jun 14, 2017
    Posts:
    20
    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:
    upload_2021-11-17_18-21-4.png
    Thanks!
     

    Attached Files:

  2. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    278
    Hi!

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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using System.Diagnostics;
    6. using System.IO;
    7.  
    8. public class comboBuild
    9. {
    10.     [MenuItem("Custom Menu/Show WebGL Subtarget")]
    11.     private static void NewMenuOption()
    12.     {
    13.         UnityEngine.Debug.Log(EditorUserBuildSettings.webGLBuildSubtarget);
    14.     }
    15.    
    16.     [MenuItem("Custom Menu/Combo Build")]
    17.     public static void BuildGame ()
    18.     {
    19.         // Get filename.
    20.        // string path = EditorUtility.SaveFolderPanel("Choose Location of Built Game", "", "");
    21.        
    22.         string path = System.IO.Directory.GetCurrentDirectory() + "/Builds";
    23.         if (!Directory.Exists(path)) {
    24.             Directory.CreateDirectory(path);
    25.         }
    26.         string dxtBuildName = "WebGL_DXT";
    27.         string astcBuildName = "WebGL_ASTC";
    28.  
    29.         string dxtBuildPath = path + "/" + dxtBuildName;
    30.         string astcBuildPath = path + "/" + astcBuildName;
    31.         string[] levels = new string[] {"Assets/Scenes/scene_0.unity"};
    32.  
    33.         // Build player.
    34.         EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.DXT;
    35.         BuildPipeline.BuildPlayer(levels, dxtBuildPath, BuildTarget.WebGL, BuildOptions.None);
    36.         EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;
    37.         BuildPipeline.BuildPlayer(levels, astcBuildPath, BuildTarget.WebGL, BuildOptions.None);
    38.      
    39.     }
    40. }
    Please let me know if this helps and if you run into any issues using it :)
     
  3. bogdan-serbanescu

    bogdan-serbanescu

    Joined:
    Jun 14, 2017
    Posts:
    20
    Thank you for this!
    I've tried locally and it works fine. In UnityCloudBuild if I just switch to the
    Code (CSharp):
    1. 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.
     
  4. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    278
    @bogdan-serbanescu

    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:

    Code (CSharp):
    1. public static void ChangeTextureCompression ()
    2.     {    
    3.         EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.DXT;
    4.     }
    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!
     

    Attached Files:

    Last edited: Nov 19, 2021
  5. Nicolas_Begin

    Nicolas_Begin

    Joined:
    Oct 26, 2018
    Posts:
    5
    Hi guys,

    I tried this code and it seems ok in the Build Settings window
    Code (CSharp):
    1. EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;
    but when i call this :
    Code (CSharp):
    1. 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 ?
     
  6. bogdan-serbanescu

    bogdan-serbanescu

    Joined:
    Jun 14, 2017
    Posts:
    20
    Your suggestion seems to be working. The issues I encountered when trying this seem to be related to something else entirely. Thank you!
     
  7. Johannski

    Johannski

    Joined:
    Jan 25, 2014
    Posts:
    830
    For me it worked to define the webGLBuildSubtarget with ASTC in the build options, I think this should work for you as well :)

    Code (CSharp):
    1. BuildPlayerOptions buildPlayerOptions = new BuildPlayerOptions();
    2. // This is the important line
    3. buildPlayerOptions.subtarget = (int)WebGLTextureSubtarget.ASTC;
    4. EditorUserBuildSettings.webGLBuildSubtarget = WebGLTextureSubtarget.ASTC;
    5. buildPlayerOptions.scenes = scenes;
    6. buildPlayerOptions.target = buildTarget;
    7. buildPlayerOptions.locationPathName = filePath;
    8.  
    9. BuildPipeline.BuildPlayer(buildPlayerOptions);
     
    Last edited: Dec 5, 2022
    radiatoryang likes this.