Search Unity

Resolved Automated build with localized textures and minimal build size - how to handle texture references

Discussion in 'Scripting' started by uwdlg, Apr 19, 2021.

  1. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Hi,

    I would like to make my custom build script only include localized textures of the locale selected when starting the build. The editor scripting part is not the problem but how to handle localized textures in general in this case. I read the documentation of Unity's localization package, but the project doesn't seem stable and actively maintained and I got the impression that all localized content is included in builds to allow for changing the language at runtime. I explicitly don't want that last part, because my target platform is WebGL and the language selection happens before the build is downloaded so a smaller locale-specific build can be delivered.
    My current idea is to have three texture folders (assuming two languages):
    • one folder each for the textures that differ for the two languages
    • a normal texture folder containing all textures (duplicates from one language for the locale-specific ones) - I would reference these directly, e.g. within Material inspectors in the editor.
    Whenever I want to make a new build, my build script would first check which language is selected and replace the locale-specific textures in the general texture folder with the corresponding ones from the folder of that language and subsequently trigger the "normal" build process. Of course this would overwrite the textures displayed in the editor every time, but I could live with that, given that I only have a handful of localized textures and exclusively use my build script EditorWindow to build the project (meaning I won't accidentally make a build with the "wrong" textures).

    This is the least "invasive" way I can think of (not requiring changing the texture references in some way), but I'm not 100% sure if it would work or if there's a "better" way, maybe even an existing solution (the localization package?) which already supports something like that. So if anyone reading this has experience with something like that or a simpler idea I would love to hear about it.
     
  2. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    EDIT: not reliable, see next post!

    (For others interested in this: I implemented it similarly to how I planned, but instead of overwriting the texture asset files (which leads to GUID problems and lost references), I instead replace only their contents with
    Graphics.CopyTexture(Texture src, Texture dst);
    , leaving the GUIDs alone. This is not permanent, re-importing the textures which had their data replaced this way will revert them back to the way they were before, but the build shows the replaced textures so it works for me. Also, in contrast to other ways of replacing texture data I tried, the textures don't need to be marked as readable for this which is a plus.)
     
    Last edited: Aug 2, 2022
  3. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    Now that I got to the point of actually using what I wrote about above I found it to be unreliable. It seems that the temporarily overwritten textures are sometimes reverted before the relevant part of the build process.
    I now actually overwrite the textures using
    File.Copy(..., true);
    , and then replace them again with what I want to show in the editor right after a build. This works reliably and I also use it for localized Audio Clips. Since this applies to only a handful of files I'm happy with this.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    The "reference" way to do this today is probably with Addressables.

    But if it actually...

    I'd just make all builds include all files. :)
     
  5. uwdlg

    uwdlg

    Joined:
    Jan 16, 2017
    Posts:
    150
    I agree with both your points. I didn't want to include all files because this is for WebGL and my web dev friend is very keen on saving a few megabytes. I don't have a good excuse for not using Adressables other than having not used them before and having been skeptical of the maturity the last time I started looking into it (long ago).
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    Bleevit or not, pretty sure with WebGL only the file(s) used by the running program would download... Unity does various bundling to be sure, as in each file isn't a single download, but I bet if you grouped it by loadable prefab (eg, loaded NOT by reference but rather by Resources.Load<T> within a specific folder for each variant, I bet only the part requested would come down.