首页 > 其他分享 >【Unity3D日常开发】Unity3D工具之UnityForSVN

【Unity3D日常开发】Unity3D工具之UnityForSVN

时间:2022-11-03 23:07:43浏览次数:98  
标签:SVN Unity3D false Assets UnityForSVN BASE static 日常 selectionPath

大家好,我是佛系工程师☆恬静的小魔龙☆,不定时更新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之UGUI​

UGUI

Unity的UI系统UGUI全解析,从UGUI的基础控件开始讲起,然后将UGUI的原理,UGUI的使用全面教学。

​​Unity3D之读取数据​

文件读取

使用Unity3D读取txt文档、json文档、xml文档、csv文档、Excel文档。

​​Unity3D之数据集合​

数据集合

数组集合:数组、List、字典、堆栈、链表等数据集合知识分享。

​​Unity3D之VR/AR(虚拟仿真)开发​

虚拟仿真

总结博主工作常见的虚拟仿真需求进行案例讲解。

​​Unity3D之插件​

插件

主要分享在Unity开发中用到的一些插件使用方法,插件介绍等

​​Unity3D之日常开发​

日常记录

主要是博主日常开发中用到的,用到的方法技巧,开发思路,代码分享等

​​Unity3D之日常BUG​

日常记录

记录在使用Unity3D编辑器开发项目过程中,遇到的BUG和坑,让后来人可以有些参考。


标签:SVN,Unity3D,false,Assets,UnityForSVN,BASE,static,日常,selectionPath
From: https://blog.51cto.com/u_15296123/5821298

相关文章

  • 06_管理数据_回收空间和分析_日常重建索引_管理GPDB日志文件
    一、回收空间和分析 1、事务ID管理在每一个数据库每2百万哥事务的时候,对每张表执行VACUUM是很有必要的; 2、系统目录维护大量的CREATE和DROP命令会导致系统表的迅速......
  • 日常开发记录-粘性定位
    需求:随着页面高度变化,中间区域的头部固定,不随页面高度的变化而跟随滚动 解决方案:粘性定位,记得设置z-index属性。固定定位不可行,会随着页面高度的变化超出中间区域,不......
  • 日常笔记
    1.openresty+redis+lua缓存使用openresty+lua脚本实现多级缓存:用户访问openresty中的Nginx,若null则访问redis,若null则访问数据库,数据库返回信息并存储在redis,redis在存......
  • unity3d:最简单的服务器,把收到消息发回客户端
    usingUnityEngine;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Net.Sockets;usingSystem.Net;usingSystem.Threading;usingSystem;usin......
  • unity3d:ui跟着gameobject移动
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassUiFollowObj:MonoBehaviour{Transformm_trans;publicTransform......
  • unity3d:protobuf .java转.cs
    服务器端定义好protobuf结构,放unity编辑器中生成.cs的结构usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;usingSystem......
  • unity3d:复制选中物体transform信息到剪切板
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEditor;publicclassSaveTransInfo:EditorWindow{[MenuItem("SaveTrans......
  • unity3d:ugui 每个字间隔间距
    usingUnityEngine;usingSystem.Collections;usingUnityEngine.UI;usingSystem;usingSystem.Collections.Generic;publicclassLine{privateint_startVertexInde......
  • Unity3D :Mob SMSSDK 运行崩溃
    报错信息android.content.ActivityNotFoundException:Unabletofindexplicitactivityclass{com.shuiying.smsm09061/com.mob.tools.MobUIShell};haveyoudeclaredt......
  • unity3d:显示FPS
    usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;publicclassFPSShow:MonoBehaviour{privatevoidOnGUI(){stringte......