[Unity] Unity editor expands TortoiseGit menu

  Version control is an essential and powerful tool in the development process. Now there are many version control software, such as SVN, Git, PlasticSCM and so on. Due to the support of various cloud warehouse hosting open source communities, such as GitHub, Gitee, etc., coupled with the distributed characteristics that make team collaboration easier, Git has become everyone's first choice.
  But Git itself has no visual interface and needs to be used by entering commands through the console. For most small partners, the console is relatively cumbersome and needs to be familiar with many commands, which increases the learning cost and raises the threshold. Therefore, a Git visualization software is generally used to make Git more intuitive and simpler to operate. Because I used SVN for my previous work, I chose TortoiseGit (little turtle) visualization software.
  After using it for a period of time, I found that every time I submit the code, update the code or view the log, I always have to open the resource manager first, and right click on the project directory to enable TortoiseGit, which feels very cumbersome. So after thinking about it, can I extend the common functions of TortoiseGit to the Unity editor menu ?
  So I sorted out some TortoiseGit commands and extended them to the Unity editor menu. The code is as follows:

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
}

Support the general menu to operate the entire project directory.

insert image description here

It also supports separate operation of Asset resources.

insert image description here

Copy the code directly into the project and you can use it. Remember to put it in the Editor directory, because the Unity editor code cannot be packaged, and an error will be reported!

If the content is helpful to you, please give me a like and a follow, and learn from each other! !

Guess you like

Origin blog.csdn.net/qq_39955460/article/details/131088189
Recommended