Unity编辑器扩展——Git笔记
本人之前做的一个项目中因为根据公司要求APP的版本需要调用Git上每次提交自动生成的那串字符,然后为了方便自动更新版本号,因此弄了两个功能的编辑器扩展,一个是之前已经写过笔记的自定义打包的https://blog.csdn.net/weixin_43872129/article/details/128372980,另一个就是本篇下面会介绍的Unity如何获取Git上的信息。本篇内容主要功能是下图中的这些内容,当然代码并没有做特别多的优化,因此有一些设置需要手动去设置,设置完才能正常使用,否则会报错,这些设置会在每个代码前后说明。
下面先上代码,一共有三个脚本,可以都放到Editor文件夹下。(这边顺便提醒一下初学者,unity中编辑扩展的脚本都需要放到Editor文件夹下,没有这个文件夹就自己创建一个这是有特殊意义的,当然除了放到Editor下,还可以在脚本中通过预编译让他在Editor下运行,例如下面这段代码创建一个asset文件,在最前面添加#if UNITY_DEITOR以及在最后添加#endif)
#if UNITY_EDITOR
using UnityEditor;
public class TestEditor : EditorWindow
{
[MenuItem("Create GameSetting/test", priority = 0)]
static void Create()
{
GameSettingData_Test config = CreateInstance<GameSettingData_Test>();
AssetDatabase.CreateAsset(config, "Assets/Resources/Game/GameSetting/test.asset");
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
}
}
#endif
回到本文主题,先放上第一个核心的脚本,这是利用C#自带的Process类去调用Git的一些"xxx.exe"文件,下面通过下面这个脚本就已经可以拿到git的一些信息甚至一些操作了。
using System.Diagnostics;
using System.IO;
using Debug = UnityEngine.Debug;
public enum GitType
{
Log,
Pull,
Commit,
Push,
StashSave,
StashPop
}
public static class TortoiseGit
{
private const string quota = "\"";
public const string COMMAND_TORTOISE_LOG = @"/command:log /path:{0} /findtype:0 /closeonend:0";
public const string COMMAND_TORTOISE_PULL = @"/command:pull /path:{0} /closeonend:0";
public const string COMMAND_TORTOISE_COMMIT = @"/command:commit /path:{0} /closeonend:0";
public const string COMMAND_TORTOISE_PUSH = @"/command:push /path:{0} /closeonend:0";
public const string COMMAND_TORTOISE_STASHSAVE = @"/command:stashsave /path:{0} /closeonend:0";
public const string COMMAND_TORTOISE_STASHPOP = @"/command:stashpop /path:{0} /closeonend:0";
public static Process CreateProcess(string path, string arguments)
{
ProcessStartInfo startInfo = new ProcessStartInfo(path, arguments)
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
ErrorDialog = false,
CreateNoWindow = true,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
LoadUserProfile = true
};
return new Process {
StartInfo = startInfo};
}
public static void GitCommand(GitType gitType, string path, string tortoiseGitPath)
{
if (!File.Exists(tortoiseGitPath))
{
Debug.LogError("TortoiseGitPath can't find.");
return;
}
switch (gitType)
{
case GitType.Log:
GitLog(path, tortoiseGitPath);
break;
case GitType.Commit:
GitCommmit(path, tortoiseGitPath);
break;
case GitType.Pull: