自学内容网 自学内容网

Unity扩展SVN命令

可以直接在unity里右键文件提交和查看提交记录
在这里插入图片描述
顶部菜单栏上回退和更新整个unity工程
在这里插入图片描述
SvnForUnity.CS
记得要放在Editor文件夹下

using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;

public class SvnForUnity
{
    static string SVN_BASE = Application.dataPath.Replace("/", "\\").Remove(Application.dataPath.Replace("/", "\\").Length - 6, 6);
    
    /// <summary>
    /// 调用cmd命令
    /// </summary>
    /// <param name="command">cmd命令</param>
    /// <param name="argument">命令参数</param>
    private static void ProcessCommand(string command, string argument)
    {
        ProcessStartInfo start = new ProcessStartInfo(command)
        {
            Arguments = argument,
            CreateNoWindow = false,
            ErrorDialog = true,
            UseShellExecute = true//明确传入的执行文件类型
        };

        if (start.UseShellExecute)
        {
            start.RedirectStandardOutput = false;
            start.RedirectStandardError = false;
            start.RedirectStandardInput = false;
        }
        else
        {
            start.RedirectStandardOutput = true;
            start.RedirectStandardError = true;
            start.RedirectStandardInput = true;
            start.StandardOutputEncoding = System.Text.Encoding.UTF8;
            start.StandardErrorEncoding = System.Text.Encoding.UTF8;
        }

        Process p = Process.Start(start);
        
        p.WaitForExit();
        p.Close();
    }
    
    static string MetaFile(string str)
    {
        return str + ".meta";
    }

    static string GetSelectFilePath()
    {
        var selectAssets = Selection.GetFiltered<Object>(SelectionMode.Assets);
        if (selectAssets.Length > 0)
        {
            string selectionPath = string.Empty;
            for (int i = 0; i < selectAssets.Length; i++)
            {
                // Debug.Log(AssetDatabase.GetAssetPath(selectAssets[i]));
                if (AssetDatabase.GetAssetPath(selectAssets[i]) == "Assets")
                {
                    return SVN_BASE + "Assets" + "\"";
                }
                if (i > 0)
                {
                    selectionPath = selectionPath + "*" + SVN_BASE + AssetDatabase.GetAssetPath(selectAssets[i]).Replace("/", "\\");
                    selectionPath = selectionPath + "*" + SVN_BASE + MetaFile(AssetDatabase.GetAssetPath(selectAssets[i])).Replace("/", "\\");
                }
                else
                {
                    selectionPath = SVN_BASE + AssetDatabase.GetAssetPath(selectAssets[i]).Replace("/", "\\");
                    selectionPath = selectionPath + "*" + SVN_BASE + MetaFile(AssetDatabase.GetAssetPath(selectAssets[i])).Replace("/", "\\");
                }
            }

            return selectionPath  + "\"";
        }

        return SVN_BASE + "Assets" + "\"";
    }
    
    /// <summary>
    /// SVN更新
    /// </summary>
    [MenuItem("SVN/Update", false, 1)]
    public static void SvnUpdate()
    {
        ProcessCommand("TortoiseProc.exe", "/command:update /path:\"" + SVN_BASE + "Assets" + "\"");
    }
    
    /// <summary>
    /// SVN提交
    /// </summary>
    [MenuItem("Assets/SVN/Commit", false, 2)]
    public static void SvnCommit()
    {
        var selectPath = GetSelectFilePath();
        ProcessCommand("TortoiseProc.exe", "/command:commit /path:\"" + selectPath);
    }
    
    /// <summary>
    /// SVN回退
    /// </summary>
    [MenuItem("SVN/Revert", false, 3)]
    public static void SvnRevert()
    {
        ProcessCommand("TortoiseProc.exe", "/command:revert /path:\"" + SVN_BASE + "Assets" + "\"");
    }
    
    /// <summary>
    /// SVN显示信息
    /// </summary>
    [MenuItem("Assets/SVN/ShowLog", false, 4)]
    public static void SvnLog()
    {
        var selectPath = GetSelectFilePath();
        ProcessCommand("TortoiseProc.exe", "/command:log /path:\"" + selectPath);
    }
}

参考
https://blog.csdn.net/egostudio/article/details/51074814


原文地址:https://blog.csdn.net/m0_38141357/article/details/140543363

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!