Skip to content

Commit bbcff3b

Browse files
committed
chore: create bundle version synchronizer script
1 parent 8e47374 commit bbcff3b

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

Assets/Editor/SyncBundleVersion.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#if UNITY_EDITOR
2+
using UnityEditor;
3+
using UnityEditor.Build;
4+
using UnityEditor.Build.Reporting;
5+
using System.IO;
6+
7+
public class SyncBundleVersionPreprocess : IPreprocessBuildWithReport
8+
{
9+
/// <summary>
10+
/// Determines the order in which build preprocessors are called.
11+
/// 0 means default/early execution.
12+
/// </summary>
13+
public int callbackOrder => 0;
14+
15+
/// <summary>
16+
/// Keeps Unity's internal application version (Application.version)
17+
/// in sync with CI/CD-managed versions.
18+
/// </summary>
19+
public void OnPreprocessBuild(BuildReport report)
20+
{
21+
var versionPath = Path.Combine(Directory.GetCurrentDirectory(), "version.txt");
22+
if (File.Exists(versionPath))
23+
{
24+
// Read the file and trim extra whitespace/newlines
25+
var v = File.ReadAllText(versionPath).Trim();
26+
27+
// Update if the file is not empty and differs from current bundleVersion
28+
if (!string.IsNullOrEmpty(v) && UnityEditor.PlayerSettings.bundleVersion != v)
29+
{
30+
UnityEditor.PlayerSettings.bundleVersion = v; // Update Unity's app version
31+
32+
AssetDatabase.SaveAssets();
33+
UnityEngine.Debug.Log($"[Build] Synced PlayerSettings.bundleVersion -> {v}");
34+
}
35+
}
36+
else
37+
{
38+
UnityEngine.Debug.LogWarning("[Build] version.txt not found; bundleVersion not changed.");
39+
}
40+
}
41+
}
42+
#endif

Assets/Editor/SyncBundleVersion.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/version.txt.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)