• 【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:后面为项目的路径
    
    • 1
    • 2
    • 3
    • 4

    代码如下所示:

    using UnityEngine;
    using UnityEditor;
    using System.Diagnostics;
    
    /// 
    /// SVN
    /// 
    public class SvnForUnity
    {
        //项目路径
        static string SVN_BASE = Application.dataPath.Replace("/", "\\").Remove(Application.dataPath.Replace("/", "\\").Length - 6, 6);
    
        /// 
        /// SVN更新 快捷键Ctrl+G
        /// 
        [MenuItem("SVN/Update %g", false, 1)]
        public static void SvnUpdate()
        {
            ProcessCommand("TortoiseProc.exe", "/command:update /path:\"" + SVN_BASE + "Assets" + "\"");
        }
        /// 
        /// SVN提交
        /// 
        [MenuItem("SVN/Commit", false, 2)]
        public static void SvnCommit()
        {
            ProcessCommand("TortoiseProc.exe", "/command:commit /path:\"" + SVN_BASE + "Assets" + "\"");
        }
        /// 
        /// SVN选择并提交
        /// 
        [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 + "\"");
            }
        }
        /// 
        /// SVN显示信息
        /// 
        [MenuItem("SVN/TortoiseSVN/Logs", false, 4)]
        public static void SvnLog()
        {
            ProcessCommand("TortoiseProc.exe", "/command:log /path:\"" + SVN_BASE + "\"");
        }
        /// 
        /// SVN设置
        /// 
        [MenuItem("SVN/TortoiseSVN/Settings", false, 5)]
        public static void SvnSetting()
        {
            ProcessCommand("TortoiseProc.exe", "/command:settings \"\"");
        }
        /// 
        /// SVN重命名文件夹
        /// 
        [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 + "\"");
            }
        }
        /// 
        /// SVN删除文件夹
        /// 
        [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 + "\"");
            }
        }
        /// 
        /// SVN合并
        /// 
        [MenuItem("SVN/TortoiseSVN/Merge", false, 8)]
        public static void SvnMerge()
        {
            ProcessCommand("TortoiseProc.exe", "/command:log /path:\"" + SVN_BASE + "\"");
        }
        /// 
        /// SVN帮助
        /// 
        [MenuItem("SVN/TortoiseSVN/Help", false, 9)]
        public static void SvnHelp()
        {
            ProcessCommand("TortoiseProc.exe", "/command:help \"\"");
        }
        /// 
        /// SVN项目设置→更新
        /// 
        [MenuItem("SVN/ProjectSettings/Update", false, 10)]
        public static void ProjectSettingsUpdate()
        {
            ProcessCommand("TortoiseProc.exe", "/command:update /path:\"" + SVN_BASE + "ProjectSettings" + "\"");
        }
        /// 
        /// SVN项目设置→提交
        /// 
        [MenuItem("SVN/ProjectSettings/Commit", false, 11)]
        public static void ProjectSettingsCommit()
        {
            ProcessCommand("TortoiseProc.exe", "/command:commit /path:\"" + SVN_BASE + "ProjectSettings" + "\"");
        }
        /// 
        /// 启动VisualSVN服务器
        /// 
        [MenuItem("SVN/VisualSVN Server", false, 12)]
        public static void SvnServer()
        {
            ProcessCommand("VisualSVN Server.msc", string.Empty);
        }
        /// 
        /// 浏览器打开项目路径
        /// 
        [MenuItem("SVN/AssetsFile", false, 10)]
        public static void AssetsFile()
        {
            ProcessCommand("explorer.exe", SVN_BASE + "Assets");
        }
        /// 
        /// SVN切换地址
        /// 
        [MenuItem("SVN/Relocate", false, 13)]
        public static void Relocate()
        {
            ProcessCommand("TortoiseProc.exe", "/command:relocate /path:\"" + SVN_BASE + "\"");
        }
        /// 
        /// SVN首次设置
        /// 
        [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");
        }
    
        /// 
        /// 调用cmd命令
        /// 
        /// cmd命令
        /// 命令参数
        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";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222

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

    三、后记

    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和坑,让后来人可以有些参考。
  • 相关阅读:
    vector用法
    python每日一题【剑指 Offer 63. 股票的最大利润】
    Android MVVM LiveData postValue 吞消息
    ICMP协议
    Hadoop--03---HDFS_01----概述
    Java项目如何实现限流?
    Mac搭建vue环境
    后端项目-菩提阁
    基于MATLAB开发AUTOSAR软件应用层模块-part8.AUTOSAR工具箱的功能介绍
    Docker部署私有仓库Harbor
  • 原文地址:https://blog.csdn.net/q764424567/article/details/127475196