Unity3D集成TortoiseSVN

        不管是我的開發仍是團隊開發,養成好的版本管理習慣能夠大大提升開發效率。經常使用的版本管理軟件通常用SVN或者Git。儘管2者都在用,可是更加偏好SVN一些。安裝TortoiseSVN就能夠直接將SVN管理軟件集成到Windows的Explorer中了。點擊鼠標右鍵就會有SVN的相關菜單,能夠很是方便地CheckOut項目以及Commit和Update。html

20140415001

        在使用Unity3D開發時,目前主流的代碼編輯器(IDE)都會選擇Virsual Studio(VS)。做爲宇宙第一強大的IDE,VS天然也有SVN插件——Virsual SVN。安裝後就能夠在VS裏面進行SVN操做了。以下圖所示,一個項目若是已經歸入了版本管理,在文件前面會有標記。綠色表示同步,黃色表示不一樣步,紅色則表示衝突。可使用右鍵進行SVN操做編輯器

20170415091803

image

        可是美中不足是Unity3D裏面無法快速地進行SVN操做。通常都是回到Explorer中而後進行提交,不夠便捷。查看了Unity Asset Store上的SVN插件,發現大多過於複雜,或者一些操做不符合原有的操做習慣。因此對於以極簡高效爲原則的我來講,還不如本身寫一個足夠用的小工具。svn

        在unity插件開發——一個例子:簡單的svn集成中,已經說明了相關方法是,經過Process.Start方法開啓一個TortoiseProc.exe進程,並將參數(點擊TortoiseSVN 操做查看全部參數)傳給TortoiseProc,使之執行相關的命令。工具

using System;
using System.Diagnostics;
using System.IO;
using UnityEditor;
using UnityEngine;

public class SvnEditor : EditorWindow
{
    #region Setup

    private const string Svnkey = "key_svnroot";
    private const string SvnName = "TortoiseProc.exe";
    private static string s_svnpath = SvnName;

    [MenuItem("SVN/Setup", priority = 0)]
    public static void Setup()
    {
        GetWindow<SvnEditor>();
    }

    #endregion

    #region Log
    [MenuItem("Assets/SVN/", priority = 10)]
    [MenuItem("Assets/SVN/LogAll", priority = 11)]
    [MenuItem("SVN/", priority = 10)]
    [MenuItem("SVN/LogAll", priority = 11)]
    public static void SVNLogAll()
    {
        SvnCmdProject(EmSVNCmd.Log);
    }

    [MenuItem("Assets/SVN/Log", priority = 1)]
    public static void SVNLog()
    {
        SvnCmdSelect(EmSVNCmd.Log);
    }

    #endregion

    #region Update

    [MenuItem("Assets/SVN/Update", priority = 2)]
    public static void SVNUpdate()
    {
        SvnCmdSelect(EmSVNCmd.Update);
    }


    [MenuItem("Assets/SVN/UpdateAll", priority = 12)]
    [MenuItem("SVN/UpdateAll", priority = 12)]
    public static void SVNUpdateAll()
    {
        SvnCmdProject(EmSVNCmd.Update);
    }

    #endregion

    #region Commit

    [MenuItem("Assets/SVN/CommitAll", priority = 13)]
    [MenuItem("SVN/CommitAll", priority = 13)]
    public static void SVNCommitAll()
    {
        SvnCmdProject(EmSVNCmd.Commit);
    }

    [MenuItem("Assets/SVN/Commit", priority = 3)]
    public static void SVNCommit()
    {
        SvnCmdSelect(EmSVNCmd.Commit);
    }

    #endregion

    #region Revert

    [MenuItem("Assets/SVN/Revert", priority = 4)]
    public static void SVNRevert()
    {
        SvnCmdSelect(EmSVNCmd.Revert);
    }

    #endregion

    #region Clearup

    [MenuItem("Assets/SVN/ClearUpAll", priority = 5)]
    [MenuItem("SVN/ClearUpAll", priority = 15)]
    public static void SVNClearUpAll()
    {
        SvnCmdProject(EmSVNCmd.Clearup);
    }

    #endregion

    #region core

    private static void SvnCmdProject(EmSVNCmd cmd)
    {
        var path = Application.dataPath;
        var project = Directory.GetParent(path);
        ProcessStart(cmd, project.FullName);
    }

    private static void SvnCmdSelect(EmSVNCmd cmd)
    {
        var select = Selection.activeObject;
        var path = AssetDatabase.GetAssetPath(select);
        ProcessStart(cmd, path);
    }

    private static void ProcessStart(EmSVNCmd cmd, string path)
    {
#if UNITY_STANDALONE_WIN||UNITY_EDITOR_WIN
        try
        {
            var info = new ProcessStartInfo(s_svnpath)
            {
                Arguments = string.Format("/command:{0} /path:{1} /closeonend:0", cmd.ToString().ToLower(), path)
            };
            Process.Start(info);
        }
        catch (Exception)
        {
            Setup();
        }
#endif
    }

    #endregion

    internal void OnEnable()
    {
        s_svnpath = EditorPrefs.GetString(Svnkey);
    }

    internal void OnGUI()
    {
        EditorGUILayout.LabelField("設置SVN");
        EditorGUILayout.Space();

        EditorGUILayout.BeginHorizontal();
        EditorGUILayout.LabelField("SVN程序", GUILayout.Width(50));
        EditorGUILayout.SelectableLabel(s_svnpath, GUILayout.Width(200));
        if (GUILayout.Button("select"))
        {
            var path = EditorUtility.OpenFilePanel("請選擇TortoiseProc.exe存儲位置", "", "exe");
            if (string.IsNullOrEmpty(path))
                return;
            s_svnpath = path;
            EditorPrefs.SetString(Svnkey, s_svnpath);
        }
        if (GUILayout.Button("Clear"))
        {
            s_svnpath = SvnName;
            EditorPrefs.SetString(Svnkey, s_svnpath);
        }

        EditorGUILayout.EndHorizontal();
    }

    private enum EmSVNCmd
    {
        Log,
        Update,
        Commit,
        Revert,
        Clearup
    }
}
相關文章
相關標籤/搜索