Unity编辑器扩展——调用Git,在Unity中拿到Git上每次提交Push的Log等信息

这篇博客介绍了如何在Unity编辑器中扩展Git功能,包括获取Git上的信息如版本更新、提交历史等。作者提供了三个核心脚本,利用C#的Process类调用Git命令,并提醒读者注意配置Git相关工具的路径,以及Unity项目的Git初始化状态。文章还指出,通过这种方式获取的日志为本地提交信息,可能不包括已推送的Merge记录。
摘要由CSDN通过智能技术生成

Unity编辑器扩展——Git笔记

本人之前做的一个项目中因为根据公司要求APP的版本需要调用Git上每次提交自动生成的那串字符,然后为了方便自动更新版本号,因此弄了两个功能的编辑器扩展,一个是之前已经写过笔记的自定义打包的https://blog.csdn.net/weixin_43872129/article/details/128372980,另一个就是本篇下面会介绍的Unity如何获取Git上的信息。本篇内容主要功能是下图中的这些内容,当然代码并没有做特别多的优化,因此有一些设置需要手动去设置,设置完才能正常使用,否则会报错,这些设置会在每个代码前后说明。
在这里插入图片描述
下面先上代码,一共有三个脚本,可以都放到Editor文件夹下。(这边顺便提醒一下初学者,unity中编辑扩展的脚本都需要放到Editor文件夹下,没有这个文件夹就自己创建一个这是有特殊意义的,当然除了放到Editor下,还可以在脚本中通过预编译让他在Editor下运行,例如下面这段代码创建一个asset文件,在最前面添加#if UNITY_DEITOR以及在最后添加#endif)

#if UNITY_EDITOR
using UnityEditor;

public class TestEditor : EditorWindow
{
   
    [MenuItem("Create GameSetting/test", priority = 0)]
    static void Create()
    {
   
        GameSettingData_Test config = CreateInstance<GameSettingData_Test>();
        AssetDatabase.CreateAsset(config, "Assets/Resources/Game/GameSetting/test.asset");
        AssetDatabase.SaveAssets();
        AssetDatabase.Refresh();
    }
}
#endif

回到本文主题,先放上第一个核心的脚本,这是利用C#自带的Process类去调用Git的一些"xxx.exe"文件,下面通过下面这个脚本就已经可以拿到git的一些信息甚至一些操作了。

using System.Diagnostics;
using System.IO;
using Debug = UnityEngine.Debug;

public enum GitType
{
   
    Log,
    Pull,
    Commit,
    Push,
    StashSave,
    StashPop
}

public static class TortoiseGit
{
   
    private const string quota = "\"";

    public const string COMMAND_TORTOISE_LOG = @"/command:log /path:{0} /findtype:0 /closeonend:0";
    public const string COMMAND_TORTOISE_PULL = @"/command:pull /path:{0} /closeonend:0";
    public const string COMMAND_TORTOISE_COMMIT = @"/command:commit /path:{0} /closeonend:0";
    public const string COMMAND_TORTOISE_PUSH = @"/command:push /path:{0} /closeonend:0";
    public const string COMMAND_TORTOISE_STASHSAVE = @"/command:stashsave /path:{0} /closeonend:0";
    public const string COMMAND_TORTOISE_STASHPOP = @"/command:stashpop /path:{0} /closeonend:0";

    public static Process CreateProcess(string path, string arguments)
    {
   
        ProcessStartInfo startInfo = new ProcessStartInfo(path, arguments)
        {
   
            WindowStyle = ProcessWindowStyle.Hidden,
            UseShellExecute = false,
            ErrorDialog = false,
            CreateNoWindow = true,
            RedirectStandardError = true,
            RedirectStandardInput = true,
            RedirectStandardOutput = true,
            LoadUserProfile = true
        };
        return new Process {
   StartInfo = startInfo};
    }

    public static void GitCommand(GitType gitType, string path, string tortoiseGitPath)
    {
   
        if (!File.Exists(tortoiseGitPath))
        {
   
            Debug.LogError("TortoiseGitPath can't find.");
            return;
        }

        switch (gitType)
        {
   
            case GitType.Log:
                GitLog(path, tortoiseGitPath);
                break;
            case GitType.Commit:
                GitCommmit(path, tortoiseGitPath);
                break;
            case GitType.Pull:
                
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TenderRain。

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值