在统一独立构建中获取构建日期

时间:2020-02-19 09:18:46

标签: c# unity3d

我正在寻找一种获取构建DateTime的方法,以便用户/质量检查人员可以获取可执行文件的发布日期。因此,可以在构建可执行文件时在其中更新DateTime的地方创建一个变量或文本文件。然后,稍后我从文件或变量中检索日期。问题是,对于每个版本,我都必须更新文件或变量中的DateTime。 Unity或Application class中是否有任何内置方法可以做到这一点?我没有发现与此相关的任何东西。

我在Stack表单上找到了此代码段,但未返回任何内容:

string GetPlayerBuildDate()
{
    var version = Assembly.GetEntryAssembly().GetName().Version;
    DateTime buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(
    TimeSpan.TicksPerDay * version.Build + // days since 1 January 2000
    TimeSpan.TicksPerSecond * 2 * version.Revision));
    Debug.Log("GetPlayerDate Assembly" + buildDateTime);
    return buildDateTime.Year.ToString();
}

1 个答案:

答案 0 :(得分:0)

如我之前的较早评论中所述,您可以使用Build Player PipeLine来完全自动地将.txt文件与包含构建日期/时间的构建文件一起使用。无需使用UI创建自己的构建系统(除非我误解了,并且您拥有自己的构建系统。但是即使如此,它也可以并入其中)。

重要提示:后处理器脚本必须位于一个名为Editor的文件夹中,如果不是,则在构建时会引发错误。在我的示例中,脚本位于/Assets/Scripts/Editor/PostBuildScript.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System;
using System.IO;

public class BuildScript
{
    //Script is called after the building process, BuildTarget and string pathToBuiltProject are mandatory arguments
    [PostProcessBuildAttribute(1)]
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject)
    {
        //Give the user a file folder pop-up asking for the location you wish to save the file to
        string path = EditorUtility.SaveFolderPanel("Save location", "", "");
        //Alternative you can also just hardcode the path..
        //string path = "C:/Dev/Unity/MyProject/MyBuilds/

        //Get the current datetime and convert it to a string with some explanatory text
        string date = string.Format("Build date: {0}", DateTime.Now.ToString());

        //Write the date to a text file called "BuildDate.txt" at the selected location
        File.WriteAllText(path + "BuildDate.txt", date);
    }
}

现在,构建“ BuildDate.txt”文件后,文件将被包含在所选位置,其中包含构建的DateTime(例如Build date: 19/02/2020 20:29:30