LoginSignup
5
3

More than 5 years have passed since last update.

Unityの自動ビルドでコミット番号を入れる(Windows)

Last updated at Posted at 2017-08-01

Unityの自動ビルドでコミット番号を入れる(Windows)

WindowsにJenkinsを入れて自動ビルド環境を構築してWindowsアプリを作っているのですが、ビルド時にgitのコミット番号を楽にアプリ内に入れれないかなぁと模索中

たぶん二度と書けなさそうなBuildClassなので覚え書きも兼ねて・・・

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

/// <summary>
/// 自動ビルドを行うクラス
/// </summary>
public class BuildClass
{

    public static void Build()
    {
        // gitのコミット番号の確認
        System.Diagnostics.Process p = new System.Diagnostics.Process();

        //git.exeのフルパス
        p.StartInfo.FileName = @"C:\Program Files\Git\cmd\git.exe";

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = false;
        p.StartInfo.CreateNoWindow = true;

        //gitで行うコマンドを設定
        p.StartInfo.Arguments = @"rev-parse --short HEAD";

        //起動
        p.Start();

        //出力を読み取る
        string results = p.StandardOutput.ReadToEnd();

        //改行を抜く
        results = results.Replace("\r", "").Replace("\n", "");

        //終了
        p.WaitForExit();
        p.Close();

        // プラットフォーム設定
        BuildTarget platform = BuildTarget.StandaloneWindows64;

        // 出力ファイル名及びパス
        string outputfile = "exe-file/"+  Application.productName +".exe";

        // ターゲットプラットフォーム
        EditorUserBuildSettings.SwitchActiveBuildTarget(platform);

        // ビルド対象シーンリスト
        List<string> allScene = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (scene.enabled)
            {
                allScene.Add(scene.path);
            }
        }

        //PlayerSettingsの変更
        PlayerSettings.SplashScreen.showUnityLogo = false;
        PlayerSettings.SplashScreen.show = false;
        PlayerSettings.bundleVersion = results;

        // 実行
        string errorMessage = BuildPipeline.BuildPlayer(
                allScene.ToArray(),
                outputfile,
                platform,
                BuildOptions.None
        );
    }
}

これで、Application.versionでgitのコミット番号が取得できる予定でしたが、7文字しか出ませんでした。
まぁ、あまり困らないからいいやっと

追記

Unity5.6.3f1だと、Obsoleteが出たのでSwitchActiveBuildTargetのところを修正

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

/// <summary>
/// 自動ビルドを行うクラス
/// </summary>
public class BuildClass
{

    public static void Build()
    {
        // gitのコミット番号の確認
        System.Diagnostics.Process p = new System.Diagnostics.Process();

        //git.exeのフルパス
        p.StartInfo.FileName = @"C:\Program Files\Git\cmd\git.exe";

        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.StartInfo.RedirectStandardInput = false;
        p.StartInfo.CreateNoWindow = true;

        //gitで行うコマンドを設定
        p.StartInfo.Arguments = @"rev-parse --short HEAD";

        //起動
        p.Start();

        //出力を読み取る
        string results = p.StandardOutput.ReadToEnd();

        //改行を抜く
        results = results.Replace("\r", "").Replace("\n", "");

        //終了
        p.WaitForExit();
        p.Close();

        // プラットフォーム設定
        BuildTarget platform = BuildTarget.StandaloneWindows64;

        // 出力ファイル名及びパス
        string outputfile = "exe-file/" + Application.productName + ".exe";

        // ターゲットプラットフォーム
        EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Standalone,platform);

        // ビルド対象シーンリスト
        List<string> allScene = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (scene.enabled)
            {
                allScene.Add(scene.path);
            }
        }

        //PlayerSettingsの変更
        PlayerSettings.SplashScreen.showUnityLogo = false;
        PlayerSettings.SplashScreen.show = false;
        PlayerSettings.bundleVersion = results;

        // 実行
        string errorMessage = BuildPipeline.BuildPlayer(
                allScene.ToArray(),
                outputfile,
                platform,
                BuildOptions.None
        );
    }
}

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3