├── .gitignore ├── Editor └── BuildDataEditor.cs ├── ExampleBuildDataDisplay.cs ├── LICENSE ├── README.md └── Utilities └── BuildData.cs /.gitignore: -------------------------------------------------------------------------------- 1 | # This .gitignore file should be placed at the root of your Unity project directory 2 | # 3 | # Get latest from https://github.com/github/gitignore/blob/master/Unity.gitignore 4 | # 5 | /[Ll]ibrary/ 6 | /[Tt]emp/ 7 | /[Oo]bj/ 8 | /[Bb]uild/ 9 | /[Bb]uilds/ 10 | /[Ll]ogs/ 11 | /[Mm]emoryCaptures/ 12 | 13 | # Asset meta data should only be ignored when the corresponding asset is also ignored 14 | !/[Aa]ssets/**/*.meta 15 | 16 | # Uncomment this line if you wish to ignore the asset store tools plugin 17 | # /[Aa]ssets/AssetStoreTools* 18 | 19 | # Autogenerated Jetbrains Rider plugin 20 | [Aa]ssets/Plugins/Editor/JetBrains* 21 | 22 | # Visual Studio cache directory 23 | .vs/ 24 | 25 | # Gradle cache directory 26 | .gradle/ 27 | 28 | # Autogenerated VS/MD/Consulo solution and project files 29 | ExportedObj/ 30 | .consulo/ 31 | *.csproj 32 | *.unityproj 33 | *.sln 34 | *.suo 35 | *.tmp 36 | *.user 37 | *.userprefs 38 | *.pidb 39 | *.booproj 40 | *.svd 41 | *.pdb 42 | *.mdb 43 | *.opendb 44 | *.VC.db 45 | 46 | # Unity3D generated meta files 47 | *.pidb.meta 48 | *.pdb.meta 49 | *.mdb.meta 50 | 51 | # Unity3D generated file on crash reports 52 | sysinfo.txt 53 | 54 | # Builds 55 | *.apk 56 | *.unitypackage 57 | 58 | # Crashlytics generated file 59 | crashlytics-build.properties 60 | 61 | -------------------------------------------------------------------------------- /Editor/BuildDataEditor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.IO; 4 | using UnityEditor; 5 | using UnityEditor.Build; 6 | using UnityEditor.Build.Reporting; 7 | 8 | [CustomEditor(typeof(BuildData))] 9 | public class BuildDataEditor : Editor { 10 | 11 | // this is where the temporary build data asset will go, this will be created 12 | // when a build runs and deleted immediately after 13 | internal const string BuildDataPath = "Assets/Resources/BuildData.asset"; 14 | 15 | public static void UpdateData() { 16 | 17 | // i only build on windows/osx and these paths work well there, adjust as needed 18 | #if UNITY_EDITOR_WIN 19 | const string gitPath = "C:\\Program Files\\Git\\bin\\git.exe"; 20 | #else 21 | const string gitPath = "git"; 22 | #endif 23 | 24 | // now we run git in the project folder to get the current commit description 25 | // (this usually ends up being the current tag plus a commit hash) 26 | 27 | var gitInfo = new ProcessStartInfo { 28 | CreateNoWindow = true, 29 | RedirectStandardError = true, 30 | RedirectStandardOutput = true, 31 | FileName = gitPath, 32 | UseShellExecute = false, 33 | }; 34 | 35 | var gitProcess = new Process(); 36 | gitInfo.Arguments = "describe --tags --always"; 37 | gitInfo.WorkingDirectory = Directory.GetCurrentDirectory(); 38 | 39 | gitProcess.StartInfo = gitInfo; 40 | gitProcess.Start(); 41 | 42 | var stdout = gitProcess.StandardOutput.ReadToEnd(); 43 | 44 | gitProcess.WaitForExit(); 45 | gitProcess.Close(); 46 | 47 | // once we're done with git, let's make the data file 48 | 49 | var data = CreateInstance(); 50 | 51 | // parse out the relevant bits of the git call output 52 | data.git = stdout.Trim(); 53 | 54 | // set the build number (i use auto-incrementing build numbers that are set externally by my build server) 55 | #if UNITY_ANDROID 56 | data.buildNumber = PlayerSettings.Android.bundleVersionCode; 57 | #elif UNITY_IOS 58 | data.buildNumber = int.Parse(PlayerSettings.iOS.buildNumber); 59 | #else 60 | data.buildNumber = 0; 61 | #endif 62 | 63 | // timestamps, regular utc flavor, more or less ISO 8601 64 | data.timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm"); 65 | // and a unix timestamp too, for good measure 66 | data.timestampUnix = (DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds.ToString("F0"); 67 | 68 | // makes sure old version is deleted as to not make CreateAsset throw an error 69 | AssetDatabase.DeleteAsset(BuildDataPath); 70 | AssetDatabase.CreateAsset(data, BuildDataPath); 71 | AssetDatabase.SaveAssets(); 72 | } 73 | } 74 | 75 | 76 | // this makes sure the above code runs when a build runs 77 | internal class BuildDataPreprocessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport { 78 | public int callbackOrder => 2; 79 | 80 | public void OnPreprocessBuild(BuildReport report) { 81 | BuildDataEditor.UpdateData(); 82 | } 83 | 84 | public void OnPostprocessBuild(BuildReport report) { 85 | AssetDatabase.DeleteAsset(BuildDataEditor.BuildDataPath); 86 | } 87 | } 88 | 89 | -------------------------------------------------------------------------------- /ExampleBuildDataDisplay.cs: -------------------------------------------------------------------------------- 1 | using TMPro; 2 | using UnityEngine; 3 | 4 | [RequireComponent(typeof(TextMeshProUGUI))] 5 | public class ExampleBuildDataDisplay : MonoBehaviour { 6 | void Start() { 7 | var versionText = GetComponent(); 8 | if (versionText == null) return; 9 | versionText.text = "build info missing\ndate goes here"; 10 | 11 | // try to load build info, may not be present 12 | var data = Resources.Load("BuildData"); 13 | if (data == null) return; 14 | versionText.text = $"{data.version} #{data.buildNumber}\n{data.git}\n{data.timestamp}"; 15 | } 16 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Martin Jonasson 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # unity-builddata 2 | 3 | This is an example of how to grab git (or possibly other scm) data at build time and store it inside your game so you can easily verify which version was used to build it. 4 | 5 | _Note: Data is only embedded when a build is made, it will not be present in the editor!_ 6 | -------------------------------------------------------------------------------- /Utilities/BuildData.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | 3 | public class BuildData : ScriptableObject { 4 | public string git = ""; 5 | public int buildNumber = 0; 6 | public string version => Application.version; 7 | public string timestamp = ""; 8 | public string timestampUnix = ""; 9 | 10 | public static string platform => Application.platform.ToString(); 11 | 12 | public override string ToString() { 13 | return $"git: {git}\n" + 14 | $"buildNumber: {buildNumber}\n" + 15 | $"version: {version}\n" + 16 | $"timestamp: {timestamp}\n" + 17 | $"timestampUnix: {timestampUnix}\n" + 18 | $"platform: {platform}"; 19 | } 20 | } 21 | --------------------------------------------------------------------------------