ArkanoidTG/Assets/Scripts/Editor/BuildVersionIncrementer.cs

55 lines
1.8 KiB
C#

using UnityEngine;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using System.IO;
public class BuildVersionIncrementer : IPreprocessBuildWithReport
{
public int callbackOrder => 0;
private const string VERSION_FILE_PATH = "Assets/Resources/buildVersion.txt";
private const string RESOURCES_PATH = "Assets/Resources";
public void OnPreprocessBuild(BuildReport report)
{
// Создаем директорию Resources если её нет
if (!Directory.Exists(RESOURCES_PATH))
{
Directory.CreateDirectory(RESOURCES_PATH);
Debug.Log($"Created Resources directory at {RESOURCES_PATH}");
}
int currentBuild = 1;
// Читаем текущий номер сборки если файл существует
if (File.Exists(VERSION_FILE_PATH))
{
string versionStr = File.ReadAllText(VERSION_FILE_PATH);
if (int.TryParse(versionStr, out int savedBuild))
{
currentBuild = savedBuild;
Debug.Log($"Current build number: {currentBuild}");
}
else
{
Debug.LogWarning($"Failed to parse build number from file: {versionStr}");
}
}
else
{
Debug.Log("No previous build number found, starting from 1");
}
// Увеличиваем номер сборки
currentBuild++;
// Записываем новый номер
File.WriteAllText(VERSION_FILE_PATH, currentBuild.ToString());
Debug.Log($"Build number incremented to: {currentBuild}");
// Обновляем ассеты чтобы Unity подхватил изменения
AssetDatabase.Refresh();
}
}