Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Get build number

Discussion in 'Unity Build Automation' started by Hiago_AD, Jul 3, 2023.

  1. Hiago_AD

    Hiago_AD

    Joined:
    Oct 5, 2021
    Posts:
    20
    Is there a way to get the build number on compile time? I'm thinking in showing on internal builds to differentiate them
     
    julienkay likes this.
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,077
    Yes, the cloud build automatically generates a 'cloud manifest' file and adds it to your build. Here's some documentation on that: https://docs.unity3d.com/560/Documentation/Manual/UnityCloudBuildManifest.html

    Here's some example code I wrote to display it on a GUI canvas, you will probably have to tweak it to work with your requirements:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using MiniJSON;
    5.  
    6. using TMPro;
    7.  
    8. public class versionNumber : MonoBehaviour
    9. {
    10.     [SerializeField]
    11.     private TextMeshProUGUI _text = null;
    12.  
    13.     private void OnEnable()
    14.     {
    15.         if (_text != null)
    16.         {
    17.             var manifest = (TextAsset)Resources.Load("UnityCloudBuildManifest.json");
    18.             if (manifest != null)
    19.             {
    20.                 var manifestDict = Json.Deserialize(manifest.text) as Dictionary<string, object>;
    21.  
    22.                 string target = string.Empty;
    23.                 string build = string.Empty;
    24.                 string commit = string.Empty;
    25.  
    26.                 if (manifestDict["cloudBuildTargetName"] != null)
    27.                 {
    28.                     target = manifestDict["cloudBuildTargetName"].ToString();
    29.                 }
    30.  
    31.                 if (manifestDict["buildNumber"] != null)
    32.                 {
    33.                     build = manifestDict["buildNumber"].ToString();
    34.                 }
    35.  
    36.                 if (manifestDict["scmCommitId"] != null)
    37.                 {
    38.                     commit = manifestDict["scmCommitId"].ToString();
    39.                 }
    40.  
    41.                 _text.SetText(string.Format("{0} 0.{1}.{2}", target, build, commit));
    42.             }
    43.             else
    44.             {
    45.                 if (Application.isEditor)
    46.                 {
    47.                     _text.enabled = false;
    48.                 }
    49.                 else
    50.                 {
    51.                     _text.SetText(string.Format("Editor build"));
    52.                 }
    53.             }
    54.         }
    55.  
    56.     }
    57. }
    58.  
    Here is another link I found on github that is probably more up to date (and doesn't require MiniJSON)

    https://github.com/RosaryMala/armok...15c3bb500/Assets/Plugins/BuildManifest.cs#L24
     
    Last edited: Jul 5, 2023
    julienkay likes this.
  3. Hiago_AD

    Hiago_AD

    Joined:
    Oct 5, 2021
    Posts:
    20
    This exact solution did not worked for me, because for some odd reason the json solutions that I tried (Unity's one and DOTNet) did not parsed correctly even tho the text seemed fine.

    But, this link that you sent gave me the insight, its a little bit outdated (https://docs.unity.com/devops/en/manual/build-automation/environment-variables this one is newer), and I got it at compile time. Thank you!
     
    tonemcbride likes this.