【Unity】Unity编辑器拓展TortoiseGit菜单

  在开发过程中,版本控制是必不可少的强力工具。现在有很多版本控制软件,如SVN、Git、PlasticSCM等等。由于各种云仓库托管开源社区的加持,如GitHub、Gitee等,再加上分布式的特点使团队协作更加容易,使Git成为大家的首选。
  但是Git本身是没有可视化界面的,需要通过控制台输入命令的方式使用。对于大部分小伙伴来说,控制台相对繁琐,需要熟悉许多命令,加大了学习成本,提高了门槛。所以一般会搭配一个Git可视化软件,使Git更直观、操作更简洁。由于之前工作用的SVN,所以选择了TortoiseGit(小乌龟)可视化软件。
  使用一段时间后发现,每次提交代码、更新代码或者查看日志的时候总是要先打开资源管理器,在工程目录右键启用TortoiseGit,感觉很繁琐。所以思考了一下,我把TortoiseGit的常用功能拓展到Unity编辑器菜单中不就行了吗?
  所以整理了部分TortoiseGit命令,拓展到了Unity编辑器菜单,代码如下:

using UnityEditor;

internal static class TortoiseGitMenu
{
    private const string MENU_NAME = "Tortoise Git";

    private static string ProjectPath => System.Environment.CurrentDirectory;

    private static string SelectionObjPath => $"{ProjectPath}/{AssetDatabase.GetAssetPath(Selection.activeInstanceID)}";

    #region Menu

    [MenuItem(MENU_NAME + "/提交", priority = 0)]
    internal static void ProjectCommit()
    {
        AssetDatabase.SaveAssets();
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:commit /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/日志", priority = 1)]
    internal static void ProjectLog()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:log /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/还原", priority = 2)]
    internal static void ProjectRevert()
    {
        AssetDatabase.SaveAssets();
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:revert /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/推送", priority = 100)]
    internal static void ProjectPush()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:push /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/获取", priority = 101)]
    internal static void ProjectFetch()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:fetch /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/切换", priority = 200)]
    internal static void ProjectSwitch()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:switch /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/合并", priority = 201)]
    internal static void ProjectMerge()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:merge /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/贮藏更改", priority = 300)]
    internal static void ProjectStashSave()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:stashsave /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/弹出贮藏", priority = 301)]
    internal static void ProjectStashPop()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:stashpop /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/同步", priority = 400)]
    internal static void ProjectSync()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:sync /path:{ProjectPath}");
    }

    [MenuItem(MENU_NAME + "/变基", priority = 401)]
    internal static void ProjectRebase()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:rebase");
    }

    [MenuItem(MENU_NAME + "/查看版本分支图", priority = 500)]
    internal static void ProjectRevisionGraph()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:revisiongraph");
    }

    [MenuItem(MENU_NAME + "/设置", priority = 600)]
    internal static void ProjectSettings()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:settings /path:{ProjectPath}");
    }

    #endregion Menu

    #region Assets

    [MenuItem("Assets/" + MENU_NAME + "/提交", priority = 1000)]
    internal static void SelectionCommit()
    {
        AssetDatabase.SaveAssets();
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:commit /path:{SelectionObjPath}");
    }

    [MenuItem("Assets/" + MENU_NAME + "/日志", priority = 1001)]
    internal static void SelectionLog()
    {
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:log /path:{SelectionObjPath}");
    }

    [MenuItem("Assets/" + MENU_NAME + "/还原", priority = 1002)]
    internal static void SelectionRevert()
    {
        AssetDatabase.SaveAssets();
        System.Diagnostics.Process.Start("TortoiseGitProc.exe", $"/command:revert /path:{SelectionObjPath}");
    }

    #endregion Assets
}

支持总菜单操作整个项目目录。

【Unity】Unity编辑器拓展TortoiseGit菜单_第1张图片

也支持Asset资源单独操作。

【Unity】Unity编辑器拓展TortoiseGit菜单_第2张图片

将代码直接复制到项目中就可以使用了,记得放在Editor目录中,因为Unity编辑器代码是不能打包的,会报错!

如果内容对你有所帮助,帮忙点个赞加个关注,互相学习探讨!!

你可能感兴趣的:(Unity小知识,unity,编辑器,git)