BITKit/Packages/Editor/VersionUpdater.cs

69 lines
2.6 KiB
C#

using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEditor.Callbacks;
using System.IO;
using BITModel;
using Newtonsoft.Json;
namespace BITKit
{
public class BuildPostprocessor
{
[PostProcessBuild(1)]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
{
Package package = new();
FileInfo fileInfo = new(pathToBuiltProject);
var directoryName = Path.GetDirectoryName(pathToBuiltProject);
var packagePath = Path.Combine(directoryName, "Config.json");
package.AppName = Path.GetFileName(pathToBuiltProject);
package.DirectoryPath = directoryName;
package.Version = Application.version;
package.Name = Application.productName;
package.ProcessName = fileInfo.Name.Replace(fileInfo.Extension,string.Empty);
package.ProductName = Application.productName;
File.WriteAllText(packagePath, JsonConvert.SerializeObject(package, Formatting.Indented));
}
}
class BuildVersionProcessor : IPreprocessBuildWithReport
{
private bool autoIncreamentBuildVersion = true;
public int callbackOrder { get { return 0; } }
public void OnPreprocessBuild(BuildReport report)
{
//Debug.Log("MyCustomBuildProcessor.OnPreprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath);
if (autoIncreamentBuildVersion) { IncrementVersion(); }
}
[MenuItem("File/Manually Increment Build Version", priority = 1)]
public static void ButtonIncrementVersion()
{
Debug.Log("Button Increment Version called.");
IncrementVersion();
}
private static void IncrementVersion()
{
string versionCurrent = Application.version;
string[] versionParts = versionCurrent.Split('.');
if (versionParts != null && versionParts.Length > 0)
{
int versionIncremented = int.Parse(versionParts[versionParts.Length - 1]);
versionIncremented += 1;
versionParts[versionParts.Length - 1] = versionIncremented.ToString();
PlayerSettings.bundleVersion = string.Join(".", versionParts);
Debug.Log("Version: " + versionCurrent + " increased to: " + PlayerSettings.bundleVersion);
}
else
{
Debug.Log("Version has no data, check Unity - Player Settings - Version, input box at top.");
}
}
}
}