@小白菜 提出每次出包都需要手动修改版本号,加上公司每个项目都需要统计出包次数就临时写了这个脚本,后续会优化并拓展功能,欢迎各位提出思路或者建议。
目前更新到2.0版本,最新代码在评论区
#if UNITY_EDITOR
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using System.IO;
using UnityEngine;
using System;
using UnityEditor;
public class PackageCounter : MonoBehaviour, IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
private static string counterFilePath;
private static int buildCount = 0;
public int callbackOrder => 0;
public void OnPreprocessBuild(BuildReport report)
{
AutoChangeVersion();
if (!Directory.Exists(Application.streamingAssetsPath))
Directory.CreateDirectory(Application.streamingAssetsPath);
if (string.IsNullOrEmpty(counterFilePath))
{
counterFilePath = Application.streamingAssetsPath + "/出包次数.txt";
File.Create(counterFilePath).Dispose();
}
if(!File.Exists(counterFilePath))
File.Create(counterFilePath).Dispose();
WriteTextToAbsolutePath(++buildCount, counterFilePath);
}
public void OnPostprocessBuild(BuildReport report)
{
}
private static void AutoChangeVersion()
{
string newVersion = DateTime.Now.ToString("yyyyMMdd");
PlayerSettings.bundleVersion = newVersion;
}
private void WriteTextToRelativePath(string text, string relativeFilePath)
{
string filePath = Path.Combine(Application.persistentDataPath, relativeFilePath);
File.WriteAllText(filePath, text);
}
private void WriteTextToRelativePath(int intText, string relativeFilePath)
{
string filePath = Path.Combine(Application.persistentDataPath, relativeFilePath);
string text = intText.ToString();
File.WriteAllText(filePath, text);
}
private void WriteTextToAbsolutePath(string text, string absoluteFilePath)
{
File.WriteAllText(absoluteFilePath, text);
}
private void WriteTextToAbsolutePath(int text, string absoluteFilePath)
{
File.WriteAllText(absoluteFilePath, text.ToString());
}
}
#endif