一、前言
在日常开发中,常常会用到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插件,一个脚本就可以解决你的后顾之忧,非常好用。