Unity3D日常开发Unity3D工具之UnityForSVN

Posted 恬静的小魔龙

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity3D日常开发Unity3D工具之UnityForSVN相关的知识,希望对你有一定的参考价值。

推荐阅读

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新Unity开发技巧,觉得有用记得一键三连哦。

一、前言

在日常开发中,常常会用到SVN或者Git作为项目版本协同管理的工具,可是在Unity中没有集成的SVN的相关工具,每当需要更新代码或者上传代码的时候需要在项目的文件中操作。

所以写了一个工具来实现在Unity中直接使用SVN的相关功能。

二、正文

首先,来说明一下原理。

原理:

在Windows操作系统中,我们可以通过cmd命令来启动各种其他应用程序,所以就可以在Unity中使用cmd命令去执行这些命令。

比如:

"TortoiseProc.exe", "/command:update /path:xxxx"
TortoiseProc.exe:SVN应用程序
command:后为操作的类型,有update/commit/revert
path:后面为项目的路径

代码如下所示:

using UnityEngine;
using UnityEditor;
using System.Diagnostics;

/// <summary>
/// SVN
/// </summary>
public class SvnForUnity

    //项目路径
    static string SVN_BASE = Application.dataPath.Replace("/", "\\\\").Remove(Application.dataPath.Replace("/", "\\\\").Length - 6, 6);

    /// <summary>
    /// SVN更新 快捷键Ctrl+G
    /// </summary>
    [MenuItem("SVN/Update %g", false, 1)]
    public static void SvnUpdate()
    
        ProcessCommand("TortoiseProc.exe", "/command:update /path:\\"" + SVN_BASE + "Assets" + "\\"");
    
    /// <summary>
    /// SVN提交
    /// </summary>
    [MenuItem("SVN/Commit", false, 2)]
    public static void SvnCommit()
    
        ProcessCommand("TortoiseProc.exe", "/command:commit /path:\\"" + SVN_BASE + "Assets" + "\\"");
    
    /// <summary>
    /// SVN选择并提交
    /// </summary>
    [MenuItem("SVN/CommitSelect", false, 3)]
    public static void SvnCommitSelect()
    
        if (Selection.GetFiltered(typeof(object), SelectionMode.Assets).Length > 0)
        
            string selectionPath = string.Empty;
            for (int i = 0; i < Selection.GetFiltered(typeof(object), SelectionMode.Assets).Length; i++)
            
                if (i > 0)
                
                    selectionPath = selectionPath + "*" + SVN_BASE + AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i]).Replace("/", "\\\\");
                    selectionPath = selectionPath + "*" + SVN_BASE + MetaFile(AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i])).Replace("/", "\\\\");
                
                else
                
                    selectionPath = SVN_BASE + AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i]).Replace("/", "\\\\");
                    selectionPath = selectionPath + "*" + SVN_BASE + MetaFile(AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i])).Replace("/", "\\\\");
                
            
            ProcessCommand("TortoiseProc.exe", "/command:commit /path:\\"" + selectionPath + "\\"");
        
    
    /// <summary>
    /// SVN显示信息
    /// </summary>
    [MenuItem("SVN/TortoiseSVN/Logs", false, 4)]
    public static void SvnLog()
    
        ProcessCommand("TortoiseProc.exe", "/command:log /path:\\"" + SVN_BASE + "\\"");
    
    /// <summary>
    /// SVN设置
    /// </summary>
    [MenuItem("SVN/TortoiseSVN/Settings", false, 5)]
    public static void SvnSetting()
    
        ProcessCommand("TortoiseProc.exe", "/command:settings \\"\\"");
    
    /// <summary>
    /// SVN重命名文件夹
    /// </summary>
    [MenuItem("SVN/TortoiseSVN/Rename", false, 6)]
    public static void SvnRename()
    
        if (Selection.GetFiltered(typeof(object), SelectionMode.Assets).Length == 1)
        
            string selectionPath = SVN_BASE + AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[0]).Replace("/", "\\\\");
            ProcessCommand("TortoiseProc.exe", "/command:rename /path:\\"" + selectionPath + "\\"");
        
    
    /// <summary>
    /// SVN删除文件夹
    /// </summary>
    [MenuItem("SVN/TortoiseSVN/Remove", false, 7)]
    public static void SvnRemove()
    
        if (Selection.GetFiltered(typeof(object), SelectionMode.Assets).Length > 0)
        
            string selectionPath = string.Empty;
            for (int i = 0; i < Selection.GetFiltered(typeof(object), SelectionMode.Assets).Length; i++)
            
                if (i > 0)
                
                    selectionPath = selectionPath + "*" + SVN_BASE + AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i]).Replace("/", "\\\\");
                    selectionPath = selectionPath + "*" + SVN_BASE + MetaFile(AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i])).Replace("/", "\\\\");
                
                else
                
                    selectionPath = SVN_BASE + AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i]).Replace("/", "\\\\");
                    selectionPath = selectionPath + "*" + SVN_BASE + MetaFile(AssetDatabase.GetAssetPath(Selection.GetFiltered(typeof(object), SelectionMode.Assets)[i])).Replace("/", "\\\\");
                
            
            ProcessCommand("TortoiseProc.exe", "/command:remove /path:\\"" + selectionPath + "\\"");
        
    
    /// <summary>
    /// SVN合并
    /// </summary>
    [MenuItem("SVN/TortoiseSVN/Merge", false, 8)]
    public static void SvnMerge()
    
        ProcessCommand("TortoiseProc.exe", "/command:log /path:\\"" + SVN_BASE + "\\"");
    
    /// <summary>
    /// SVN帮助
    /// </summary>
    [MenuItem("SVN/TortoiseSVN/Help", false, 9)]
    public static void SvnHelp()
    
        ProcessCommand("TortoiseProc.exe", "/command:help \\"\\"");
    
    /// <summary>
    /// SVN项目设置→更新
    /// </summary>
    [MenuItem("SVN/ProjectSettings/Update", false, 10)]
    public static void ProjectSettingsUpdate()
    
        ProcessCommand("TortoiseProc.exe", "/command:update /path:\\"" + SVN_BASE + "ProjectSettings" + "\\"");
    
    /// <summary>
    /// SVN项目设置→提交
    /// </summary>
    [MenuItem("SVN/ProjectSettings/Commit", false, 11)]
    public static void ProjectSettingsCommit()
    
        ProcessCommand("TortoiseProc.exe", "/command:commit /path:\\"" + SVN_BASE + "ProjectSettings" + "\\"");
    
    /// <summary>
    /// 启动VisualSVN服务器
    /// </summary>
    [MenuItem("SVN/VisualSVN Server", false, 12)]
    public static void SvnServer()
    
        ProcessCommand("VisualSVN Server.msc", string.Empty);
    
    /// <summary>
    /// 浏览器打开项目路径
    /// </summary>
    [MenuItem("SVN/AssetsFile", false, 10)]
    public static void AssetsFile()
    
        ProcessCommand("explorer.exe", SVN_BASE + "Assets");
    
    /// <summary>
    /// SVN切换地址
    /// </summary>
    [MenuItem("SVN/Relocate", false, 13)]
    public static void Relocate()
    
        ProcessCommand("TortoiseProc.exe", "/command:relocate /path:\\"" + SVN_BASE + "\\"");
    
    /// <summary>
    /// SVN首次设置
    /// </summary>
    [MenuItem("SVN/FirstSetting", false, 14)]
    public static void SetEditor()
    
        if (EditorSettings.serializationMode!= SerializationMode.ForceText)
        
            EditorSettings.serializationMode = SerializationMode.ForceText;
            UnityEngine.Debug.Log("SerializationMode"+ "=>ForceText");
        
        if (EditorSettings.externalVersionControl != "Visible Meta Files")
        
            EditorSettings.externalVersionControl = "Visible Meta Files";
            UnityEngine.Debug.Log("externalVersionControl" + "=>Visible Meta Files");
        
        UnityEngine.Debug.Log("SVN for Unity is OK");
    

    /// <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";
    

重要代码都注释了,欢迎食用。

三、后记

UnityForSVN插件,一个脚本就可以解决你的后顾之忧,非常好用。


你的点赞就是对博主的支持,有问题记得留言:

博主主页有联系方式。

博主还有跟多宝藏文章等待你的发掘哦:

专栏方向简介
Unity3D开发小游戏小游戏开发教程分享一些使用Unity3D引擎开发的小游戏,分享一些制作小游戏的教程。
Unity3D从入门到进阶入门从自学Unity中获取灵感,总结从零开始学习Unity的路线,有C#和Unity的知识。
Unity3D之UGUIUGUIUnity的UI系统UGUI全解析,从UGUI的基础控件开始讲起,然后将UGUI的原理,UGUI的使用全面教学。
Unity3D之读取数据文件读取使用Unity3D读取txt文档、json文档、xml文档、csv文档、Excel文档。
Unity3D之数据集合数据集合数组集合:数组、List、字典、堆栈、链表等数据集合知识分享。
Unity3D之VR/AR(虚拟仿真)开发虚拟仿真总结博主工作常见的虚拟仿真需求进行案例讲解。
Unity3D之插件插件主要分享在Unity开发中用到的一些插件使用方法,插件介绍等
Unity3D之日常开发日常记录主要是博主日常开发中用到的,用到的方法技巧,开发思路,代码分享等
Unity3D之日常BUG日常记录记录在使用Unity3D编辑器开发项目过程中,遇到的BUG和坑,让后来人可以有些参考。

以上是关于Unity3D日常开发Unity3D工具之UnityForSVN的主要内容,如果未能解决你的问题,请参考以下文章

Unity3D日常开发提高开发效率之定时器系统实现

Unity3D日常开发提高开发效率之定时器系统实现

Unity3D日常开发Unity3D中实现计时器工具类-正计时倒计时暂停计时加速计时

Unity3D日常开发Unity3D中实现计时器工具类-正计时倒计时暂停计时加速计时

Unity3D日常开发Unity3D中实现计时器工具类-正计时倒计时暂停计时加速计时

Unity3D日常开发实现角色移动行走之CharacterController组件