unity编辑器扩展 SVN自动更新工具

来源:互联网 发布:手机淘宝怎么找相似 编辑:程序博客网 时间:2024/05/21 14:07

最近用到一个SVN功能:在本地一个文件夹路径下上传文件,本地其他多个路径下也可Update到前面上传的文件,由于路径不同手动打开多个文件夹更新比较麻烦,于是用Unity写了个一键更新的窗口工具。
这里写图片描述

以下是部分核心代码

窗口类:

    string SvnFolderName = "/.svn";     // 用于检测文件夹是否受SVN控制    string lastSourceFolderPath = "";    static string listFileName = "svnData.txt";    static Vector2 defaultWindowSize = new Vector2(800,650);    static bool isClose = false;    public static List<FolderPath> sourceFolderList = new List<FolderPath>();    // 窗口菜单路径  %Q 表示快捷键Ctrl+Q打开    [UnityEditor.MenuItem("Window/SvnManager %Q")]    // 实例化窗口    static void Init()    {        var curWindow = (SvnEdittorWindow)EditorWindow.GetWindow(typeof(SvnEdittorWindow));        if (isClose) {            curWindow.Show();            curWindow.minSize = defaultWindowSize;            Reload();        }        else        {            curWindow.Close();        }        isClose = !isClose;    }    void OnGUI()    {        GUILayout.BeginVertical();        GUILayout.BeginHorizontal();        GUILayout.Label("源文件夹列表",EditorStyles.boldLabel);        if (GUILayout.Button("添加")) {            // 添加一行目录            AddFolderPath();        }        if (GUILayout.Button("Reload")) {            this.Reload();        }        if (GUILayout.Button("存储")) {            // 将List序列化为字符串存储为txt            string str = GameUtils.GetEntityToString(sourceFolderList.ToArray());            // 自定义类 主要调用的是IO中文件读写功能            FileUtils.WriteFile(UnityEngine.Application.dataPath,listFileName,str);            Debug.Log("数据存储成功!");        }        GUILayout.EndHorizontal();        GUILayout.BeginScrollView(Vector2.zero,GUILayout.Width(800),GUILayout.Height(600));        for (int i = 0; i < sourceFolderList.Count; i++) {            // 设置源文件夹路径            GUILayout.BeginHorizontal();            GUILayout.Label("源:");            sourceFolderList[i].sourcePath = EditorGUILayout.TextField(sourceFolderList[i].sourcePath);            if (GUILayout.Button("设置源...")) {                var path = EditorUtility.OpenFolderPanel("选择源文件夹",lastSourceFolderPath,"");                if (!string.IsNullOrEmpty(path))                 {                    sourceFolderList[i].sourcePath = path;                    lastSourceFolderPath = path;                    //将盘符替换成SVN服务器地址                    sourceFolderList[i].SvnPath = SvnUtils.SvnFolderPR + (sourceFolderList[i].sourcePath.Remove(0,3));                }                GUILayout.Label(sourceFolderList[i].sourcePath,EditorStyles.boldLabel);            }            // Commit            GUI.backgroundColor = Color.yellow;            if (GUILayout.Button("Commit...")) {                if(sourceFolderList[i].sourcePath == null || sourceFolderList[i].sourcePath == "")                {                    Debug.LogError("请设置文件夹源路径!");                    return ;                }                SvnUtils.SVNCommit(sourceFolderList[i].sourcePath);            }            GUI.backgroundColor = Color.white;            GUILayout.EndHorizontal();            // 设置目标文件夹路径            GUILayout.BeginHorizontal();            GUILayout.Label("目标:");            sourceFolderList[i].targetPath = EditorGUILayout.TextField(sourceFolderList[i].targetPath);            if (GUILayout.Button("设置目标路径...")) {                var path = EditorUtility.OpenFolderPanel("选择目标文件夹","","");                if (!string.IsNullOrEmpty(path))                 {                    sourceFolderList[i].targetPath = path;                    sourceFolderList[i].lastTargetPath = path;                }                GUILayout.Label(sourceFolderList[i].targetPath,EditorStyles.boldLabel);            }            // Update            GUI.backgroundColor = Color.green;            if (GUILayout.Button("Update...")) {                if( !CheckFolderPathComplete(sourceFolderList[i].sourcePath,sourceFolderList[i].targetPath))                    return ;                // 若SVN为空或源文件夹路径发生改变 则重置SVN地址                if (string.IsNullOrEmpty (sourceFolderList[i].SvnPath) ||                     sourceFolderList[i].lastSourcePath != sourceFolderList[i].sourcePath)                {                    var tmpSvnPath = SvnUtils.SvnFolderPR + (sourceFolderList[i].sourcePath.Remove(0,3));                    sourceFolderList[i].SvnPath = tmpSvnPath;                }                // 检测文件夹是否受SVN控制                if (System.IO.Directory.Exists(sourceFolderList[i].targetPath + SvnFolderName)) {                    // 检测源路径是否发生改变                    if (sourceFolderList[i].lastSourcePath == sourceFolderList[i].sourcePath) {                        SvnUtils.SVNUpdate(sourceFolderList[i].targetPath);                    }else{                        FileUtil.DeleteFileOrDirectory(sourceFolderList[i].targetPath);                        AssetDatabase.Refresh();                        System.IO.Directory.CreateDirectory(sourceFolderList[i].targetPath);                        AssetDatabase.Refresh();                        //System.IO.DirectoryInfo folder = new System.IO.DirectoryInfo(sourceFolderList[i].targetPath);                        //folder.Create();                        SvnUtils.SVNCheckout(sourceFolderList[i].SvnPath,sourceFolderList[i].targetPath);                    }                }else {                    // 否 则Checkout                    SvnUtils.SVNCheckout(sourceFolderList[i].SvnPath,sourceFolderList[i].targetPath);                }                sourceFolderList[i].lastSourcePath = sourceFolderList[i].sourcePath;            }            GUI.backgroundColor = Color.white;            GUILayout.EndHorizontal();            GUILayout.BeginHorizontal();            if (GUILayout.Button("Copy")) {                var folderData = AddFolderPath();                folderData.sourcePath = sourceFolderList[i].sourcePath;                folderData.targetPath = sourceFolderList[i].targetPath;                folderData.SvnPath = sourceFolderList[i].SvnPath;            }            GUI.backgroundColor = Color.red;                if (GUILayout.Button("删除")) {                sourceFolderList.Remove(sourceFolderList[i]);            }            GUI.backgroundColor = Color.white;            GUILayout.EndHorizontal();            GUILayout.Space(20f);        }        GUILayout.EndScrollView();        GUILayout.EndVertical();    }    // 添加一条路径    FolderPath AddFolderPath()    {        var folderPath = new FolderPath();        sourceFolderList.Add(folderPath);        return folderPath;    }    static void Reload()    {        if (System.IO.File.Exists(UnityEngine.Application.dataPath + "/" + listFileName)) {            // 读取            var str = FileUtils.ReadFile(UnityEngine.Application.dataPath,listFileName);            var ieu = GameUtils.GetEntityByString<FolderPath[]>(str);            sourceFolderList = new List<FolderPath>(ieu);        }    }    bool CheckFolderPathComplete(string _sourcePath,string _targetPath)    {        if (string.IsNullOrEmpty(_sourcePath)) {            Debug.LogError("请设置文件夹源路径!");            return false;        }        if (string.IsNullOrEmpty(_targetPath)) {            Debug.LogError("请设置文件夹目标路径");            return false;        }        return true;    }

SVN工具类

    public static string SvnFolderPR = "https://engineer_server/svn/";    public static string UnityProjectName = "Desinty";    public static string SVNProjectPath{          get{              System.IO.DirectoryInfo parent = System.IO.Directory.GetParent(Application.dataPath);              return parent.ToString();          }      }     public static void SVNCommit(string folderPath){          if (folderPath == null || folderPath == "") return;        //List<string> pathList = new List<string> ();          //string basePath = SVNProjectPath + "/Assets";          //pathList.Add (folderPath);          //pathList.Add (SVNProjectPath+"/ProjectSettings");          //string commitPath = string.Join ("*", pathList.ToArray ());          ProcessCommand ("TortoiseProc.exe", "/command:commit /path:" + folderPath);      }      public static void SVNCheckout(string checkOutPath,string _svnPath)    {        if (string.IsNullOrEmpty(checkOutPath)) return;        if (string.IsNullOrEmpty(_svnPath)) return;        ProcessCommand ("TortoiseProc.exe", "/command:checkout /path:" + _svnPath + " /url:" + checkOutPath);      }    public static void SVNUpdate(string targetPath){          ProcessCommand ("TortoiseProc.exe", "/command:update /path:" + targetPath + " /closeonend:3");      }      public static void SVNCleanUp(){          ProcessCommand ("TortoiseProc.exe", "/command:cleanup /path:" + SVNProjectPath);      }      public static void SVNLog(){          ProcessCommand ("TortoiseProc.exe", "/command:log /path:" + SVNProjectPath);      }      // 执行命令行    public static void ProcessCommand(string command,string argument){        System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo(command);        info.Arguments = argument;        info.CreateNoWindow = false;        info.ErrorDialog = true;        info.UseShellExecute = true;        if(info.UseShellExecute){            info.RedirectStandardOutput = false;            info.RedirectStandardError = false;            info.RedirectStandardInput = false;        } else{            info.RedirectStandardOutput = true;            info.RedirectStandardError = true;            info.RedirectStandardInput = true;            info.StandardOutputEncoding = System.Text.UTF8Encoding.UTF8;            info.StandardErrorEncoding = System.Text.UTF8Encoding.UTF8;        }        System.Diagnostics.Process process = System.Diagnostics.Process.Start(info);        if(!info.UseShellExecute){            Debug.Log(process.StandardOutput);            Debug.Log(process.StandardError);        }        process.WaitForExit();        process.Close();    }
0 0
原创粉丝点击