51 lines
1.5 KiB
C#
51 lines
1.5 KiB
C#
using System;
|
|
using System.Globalization;
|
|
using UnityEngine;
|
|
#if UNITY_EDITOR
|
|
using UnityEditor;
|
|
using UnityEditor.Build;
|
|
using UnityEditor.Build.Reporting;
|
|
#endif
|
|
|
|
namespace DanieleMarotta.RiversongCodeShowcase
|
|
{
|
|
#if UNITY_EDITOR
|
|
public class BuildVersionPreprocessor : IPreprocessBuildWithReport
|
|
{
|
|
private const string AssetPath = "Assets/_Project/Config/BuildVersion.asset";
|
|
|
|
public int callbackOrder => 0;
|
|
|
|
public void OnPreprocessBuild(BuildReport report)
|
|
{
|
|
var buildVersion = AssetDatabase.LoadAssetAtPath<BuildVersionAsset>(AssetPath);
|
|
|
|
if (!buildVersion)
|
|
throw new BuildFailedException($"Missing build version asset at '{AssetPath}'.");
|
|
|
|
var today = DateTime.Now;
|
|
var dayKey = today.ToString("yyyyMMdd", CultureInfo.InvariantCulture);
|
|
|
|
if (string.Equals(buildVersion.LastBuildDate, dayKey, StringComparison.Ordinal))
|
|
buildVersion.LastBuildCounter++;
|
|
else
|
|
{
|
|
buildVersion.LastBuildDate = dayKey;
|
|
buildVersion.LastBuildCounter = 1;
|
|
}
|
|
|
|
buildVersion.CurrentVersion = string.Format(
|
|
CultureInfo.InvariantCulture,
|
|
"{0:00}.{1:00}.{2:0000}.{3}",
|
|
today.Month,
|
|
today.Day,
|
|
today.Year,
|
|
buildVersion.LastBuildCounter);
|
|
|
|
EditorUtility.SetDirty(buildVersion);
|
|
AssetDatabase.SaveAssets();
|
|
}
|
|
}
|
|
#endif
|
|
}
|