Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is Application.SetBuildTags meant to be used for?

Discussion in 'Scripting' started by Xarbrough, Aug 16, 2019.

  1. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I've stumbled across this API: Application.SetBuildTags

    What is this being used for by Unity or what can users use this for?

    I've did a quick test and found, that build tags set during PreprocessBuild are cleared during the build process and they are also not persisted into the player, so I don't really understand what this can be used for. At first I thought this might be a nice way to add metadata to a build if the data were saved to the built player, but apparently it's nothing more than a static variable. Or is there anything hidden where these tags can be used?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
  3. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Yes, at least in my test. I set the tags during the preprocessbuild callback and checked gettags during Start in the built player, but the array was empty. As a sanity check I also checked during the build process, where the array is being set correctly, but appears to be reset/cleared sometime during the build process.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    "A boog!? You've found a @#$@#$ boog!!"

    Seriously, it might be a bug, but since I've never used those calls I can't confirm. Maybe someone else here will chime in.

    If it is indeed a bug, file it! Unity does fix stuff all the time based on bug reports.
     
  5. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    Yea, I'm asking here in the forums because the documentation is pretty unclear and I couldn't find any example of the API being used in public Github repositories or elsewhere on the web. If nobody else answers here, I'll report an issue to clear things up and post the answer.
     
    Kurt-Dekker likes this.
  6. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    748
    This would be a good idea to store things like branch/changesetid etc and be able to access them on startup.

    Any more info on how to set them and have them persist in build?
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    Assuming the pre-process build step works (works for me!), put a script in that calls your source control to find what the latest changeset is, writes it to a file, then updates the AssetDatabase.

    Here's my build date/time script for reference:

    BuildDate.cs:

    Code (csharp):
    1. using UnityEngine;
    2.  
    3. #if UNITY_EDITOR
    4. using UnityEditor;
    5. using UnityEditor.Build;
    6. #endif
    7.  
    8. public class BuildDate : MonoBehaviour
    9.  
    10. #if UNITY_EDITOR
    11. , IPreprocessBuild
    12. #endif
    13.  
    14. {
    15.     public TextAsset BuildDateTextAsset;
    16.  
    17.     public string s_BuildDate
    18.     {
    19.         get
    20.         {
    21.             return BuildDateTextAsset.text;
    22.         }
    23.     }
    24.  
    25. #if UNITY_EDITOR
    26.     public int callbackOrder { get { return 0; } }
    27.  
    28.     public void OnPreprocessBuild(BuildTarget target, string path)
    29.     {
    30.         Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + target + " at path " + path);
    31.  
    32.         string builddate = System.DateTime.Now.ToString("yyyyMMdd_hhmm");
    33.         Debug.Log("builddate:" + builddate);
    34.  
    35.         string outfile = "Assets/0zeroscene/BuildDateTextFile.txt";
    36.  
    37.         Debug.Log("path = '" + outfile + "'");
    38.  
    39.         System.IO.File.WriteAllText(outfile, builddate + "\n");
    40.  
    41.         AssetDatabase.Refresh();
    42.     }
    43. #endif
    44. }
    My first scene has a GameObject with this on it, and the BuildDateTextFile.txt asset dragged into the public TextAsset field, making it available at runtime.
     
  8. trooper

    trooper

    Joined:
    Aug 21, 2009
    Posts:
    748
    @Kurt-Dekker Thanks for the help but my use case is a little specific.

    We want to be able to have some build information to submit to our bug reporting tool at startup before anything significant happens (using RuntimeInitializeOnLoad), some of these methods run in async and we can't guarantee we are on the main thread when we want the information.

    Ideally a non-async, thread safe method is best so we've had to go with generating a class that hardcodes key/value pairs from a dictionary we fill up during build time. It's ugly but good enough.

    GetBuildTags/SetBuildTags seems named exactly for this use case but doesn't seem to do anything.
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    Ugly schmugly... that's pretty much exactly what I would do if I needed that info at IPL. Glad you got a solution!

    Weird that the build tags thing seems inoperative. I wish we could summon a Unity Build System Guru(tm) on demand to ask if this is a bug or we're just misreading the purpose of the API.
     
  10. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I’ve sent it to QA and now waiting for the reply. ;)

    Btw, my current solution for these kind of things was generating a static class during the build. Previously, my team was also using ScriptableObjects placed in “preloaded assets”, but it can be a pain to either revert changes to this asset from source control or to keep it up to date.
     
  11. Xarbrough

    Xarbrough

    Joined:
    Dec 11, 2014
    Posts:
    1,188
    I received a reply from QA and they said that the documentation was not fully descriptive. SetBuildTags is meant to be used with the Analytics Library package and does not persist any data into the build. The reply did not elaborate on what these tags are used for in Analytics, but that the recommended approach to persist data into the build was writing a custom file.

    Looks like I’m back to searching for a solution for how I can generate data during the build process and persist it to the player without having to use an asset in the project. For example: I’d like to generate build metadata and inject it into the build or make it available cross-platform for access in the player without having to use an asset in the project.
     
    Peter77 likes this.
  12. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,522
    I would just create a placeholder asset, tell source control to permanently ignore it (and the meta file), then anybody on your team (or your CI system) can build it and change it without making undue source control noise.