当前位置:   article > 正文

UnityEditor写工具编译项目

UnityEditor写工具编译项目

前提:项目中有需求,在项目里有一个主项目,一个模块项目,需要将模块项目编译成dll给主项目调用。

设计:

  • 寻找Microsoft visual studio目录
  • 调用LaunchDevCmd.bat.
  • Msbuild
  • copy target dll
  • refresh

用到的知识:

  • unity中开启cmd
  • 字符串编码格式转换
  • https://docs.microsoft.com/zh-cn/visualstudio/ide/reference/command-prompt-powershell?view=vs-2019
  • https://docs.microsoft.com/zh-cn/visualstudio/msbuild/msbuild-command-line-reference?view=vs-2019

GUI:在这里插入图片描述

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;


public class CMD : EditorWindow
{
    [MenuItem("SDK/编译sdk %w")]
    private static void Msbuild()
    {
        var window = GetWindow<CMD>();
        window.Show();
        //window.Close();
        microsoftVisualPath = EditorPrefs.GetString(nameof(microsoftVisualPath));
    }

    private void Check()
    {
        //if()
        var batFile = Path.Combine(microsoftVisualPath, launchDevCMDPath);
        
        //entreprise
        if(File.Exists(batFile))
        {

            Rebuild(batFile);
        }
        //profession
        else if(File.Exists(batFile = batFile.Replace(enterprise, professional)))
        {
            Rebuild(batFile);
        }
        //community
        else if (File.Exists(batFile = batFile.Replace(professional, community)))
        {
            Rebuild(batFile);
        }
        else
        {
            this.ShowNotification(new GUIContent() { image = EditorGUIUtility.IconContent("d_Collab.FolderConflict").image, text = "请检查MicrosoftVisualStudio路径!" });
        }
        Debug.Log($"batFile:{batFile}");
    }

    private string output;
    private void Rebuild(string batFile)
    {
        output = string.Empty;

        Process proc = null;

        try
        {
            proc = new Process();
            proc.StartInfo.FileName = batFile;
            //proc.StartInfo.Arguments = string.Format("10");//this is argument
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            
            proc.Start(); 

            string strInput = "chcp 65001" + @"\" + "echo 输入编译命令!" + @"\";
            proc.StandardInput.WriteLine(strInput);
            proc.StandardInput.AutoFlush = true;
            strInput = "chcp 65001" + "&msbuild E:\\sw\\Unity_project\\AFEvent\\AstralSDK\\AstralSDK.csproj /t:ResolveReferences;Compile  /p:Configuration=Release" + @"\" + "&exit";
            proc.StandardInput.WriteLine(strInput);
            proc.StandardInput.AutoFlush = true;
            output = proc.StandardOutput.ReadToEnd();

            Encoding gbkencoding = Encoding.GetEncoding(936);
            byte[] buf2 = Encoding.Convert(gbkencoding, Encoding.UTF8, System.Text.Encoding.Default.GetBytes(output));
            string atext = Encoding.UTF8.GetString(buf2);

            proc.WaitForExit();

            

        }
        catch (Exception ex)
        {
            Debug.LogErrorFormat("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace);
        }
        Debug.Log($"batFile:执行完成!");

        //move dll
        var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "AstralSDK", "obj", "Release", "net472", "AstralSDK.dll");
        var destPath = Path.Combine(Application.dataPath, "SDK", "AstralSDK.dll");
        if(File.Exists(destPath))
        {
            FileUtil.DeleteFileOrDirectory(destPath);
            FileUtil.DeleteFileOrDirectory(destPath + ".meta");
        }
        AssetDatabase.Refresh();
        FileUtil.CopyFileOrDirectory(filePath, destPath);
        Debug.Log($"batFile:移动完成!");
        AssetDatabase.Refresh();
    }
 

    private static string microsoftVisualPath;
    private string launchDevCMDPath = "2019\\Enterprise\\Common7\\Tools\\LaunchDevCmd.bat";
    private string professional = "Professional";
    private string enterprise = "Enterprise";
    private string community = "Community";
    //private string astralSDKPath = "";
    private  Vector2 posScroll= Vector2.zero;
    private void OnGUI()
    {

        EditorGUILayout.LabelField("MicrosoftVisualStudio路径:" ,microsoftVisualPath,new GUIStyle(EditorStyles.boldLabel) {  alignment = TextAnchor.MiddleRight});

        if(GUILayout.Button("microsoftVisualStudioPath"))
        {
            microsoftVisualPath = EditorUtility.OpenFolderPanel("选择MicrosoftVisualStudio路径", microsoftVisualPath, "");
            EditorPrefs.SetString(nameof(microsoftVisualPath), microsoftVisualPath);
            //Debug.Log(new DirectoryInfo(Path.Combine(Application.dataPath, "..")).FullName);
            //Debug.Log();
        }

        if(GUILayout.Button("编译"))
        {
            if(string.IsNullOrWhiteSpace(microsoftVisualPath) || !microsoftVisualPath.Contains("Microsoft Visual Studio") || !Directory.Exists(microsoftVisualPath))
            {
                this.ShowNotification(new GUIContent() { image = EditorGUIUtility.IconContent("d_Collab.FolderConflict").image, text = "请检查MicrosoftVisualStudio路径!" });
                return;
            }
            Check();
        }

        posScroll = GUILayout.BeginScrollView(posScroll);
        GUILayout.TextArea(output);
        GUILayout.EndScrollView();
        
        if(GUILayout.Button("清理"))
        {
            output = string.Empty;
        }
    }



    
}
#endif

  • 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

#编译.sln

#if UNITY_EDITOR
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
using Debug = UnityEngine.Debug;


public class CMD : EditorWindow
{
    [MenuItem("SDK/编译sdk %w")]
    private static void Msbuild()
    {
        var window = GetWindow<CMD>();
        window.Show();
        //window.Close();
        microsoftVisualPath = EditorPrefs.GetString(nameof(microsoftVisualPath));
    }

    private void Check()
    {
        //if()
        var batFile = Path.Combine(microsoftVisualPath, launchDevCMDPath);
        
        //entreprise
        if(File.Exists(batFile))
        {

            Rebuild(batFile);
        }
        //profession
        else if(File.Exists(batFile = batFile.Replace(enterprise, professional)))
        {
            Rebuild(batFile);
        }
        //community
        else if (File.Exists(batFile = batFile.Replace(professional, community)))
        {
            Rebuild(batFile);
        }
        else
        {
            this.ShowNotification(new GUIContent() { image = EditorGUIUtility.IconContent("d_Collab.FolderConflict").image, text = "请检查MicrosoftVisualStudio路径!" });
        }
        Debug.Log($"batFile:{batFile}");
    }

    private string output;
    private void Rebuild(string batFile)
    {
        output = string.Empty;

        Process proc = null;

        try
        {
            proc = new Process();
            proc.StartInfo.FileName = batFile;
            proc.StartInfo.Arguments = "/cipconfig";//this is argument
            proc.StartInfo.CreateNoWindow = false;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.RedirectStandardInput = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.RedirectStandardError = true;
            
            proc.Start();
            var path =Path.Combine(System.IO.Directory.GetCurrentDirectory(), "SDK", "SDK.sln");
            string strInput = "chcp 65001" + @"\" + "echo 输入编译命令!";
            proc.StandardInput.WriteLine(strInput);
            proc.StandardInput.AutoFlush = true;
            //strInput = "cd " + Path.Combine(System.IO.Directory.GetCurrentDirectory(), "SDK");
            //proc.StandardInput.WriteLine(strInput);
            //proc.StandardInput.AutoFlush = true;
            strInput = "chcp 65001" + "&msbuild " + path + " /t:Rebuild&exit";
            proc.StandardInput.WriteLine(strInput);
            proc.StandardInput.AutoFlush = true;
            output = proc.StandardOutput.ReadToEnd();

            Encoding gbkencoding = Encoding.GetEncoding(936);
            byte[] buf2 = Encoding.Convert(gbkencoding, Encoding.UTF8, System.Text.Encoding.Default.GetBytes(output));
            string atext = Encoding.UTF8.GetString(buf2);

            proc.WaitForExit();
            proc.Close();


        }
        catch (Exception ex)
        {
            Debug.LogErrorFormat("Exception Occurred :{0},{1}", ex.Message, ex.StackTrace);
        }
        Debug.Log($"batFile:执行完成!");

        //move dll
        var filePath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "SDK", "bin", "Release", "net471", "SDK.dll");
        var destPath = Path.Combine(Application.dataPath, "Plugins", "AstralSDK", "SDK.dll");
        var destSDKEditorPath = Path.Combine(Application.dataPath, "Plugins", "AstralSDK", "SDKEditor.dll");
        var fileSDKEditorPath = Path.Combine(System.IO.Directory.GetCurrentDirectory(), "SDK", "bin", "Release", "net471", "SDKEditor.dll");
        if (File.Exists(destPath))
        {
            FileUtil.DeleteFileOrDirectory(destPath);
            //FileUtil.DeleteFileOrDirectory(destPath + ".meta");
        }

        if(File.Exists(destSDKEditorPath))
        {
            FileUtil.DeleteFileOrDirectory(destSDKEditorPath);
            //FileUtil.DeleteFileOrDirectory(destSDKEditorPath + ".meta");
        }

        AssetDatabase.Refresh();
        FileUtil.CopyFileOrDirectory(filePath, destPath);
        FileUtil.CopyFileOrDirectory(fileSDKEditorPath, destSDKEditorPath);
        Debug.Log($"batFile:移动完成!");
        AssetDatabase.Refresh();
    }
 

    private static string microsoftVisualPath;
    private string launchDevCMDPath = "2019\\Enterprise\\Common7\\Tools\\LaunchDevCmd.bat";
    private string professional = "Professional";
    private string enterprise = "Enterprise";
    private string community = "Community";
    //private string astralSDKPath = "";
    private  Vector2 posScroll= Vector2.zero;
    private void OnGUI()
    {

        EditorGUILayout.LabelField("MicrosoftVisualStudio路径:" ,microsoftVisualPath,new GUIStyle(EditorStyles.boldLabel) {  alignment = TextAnchor.MiddleRight});

        if(GUILayout.Button("microsoftVisualStudioPath"))
        {
            microsoftVisualPath = EditorUtility.OpenFolderPanel("选择MicrosoftVisualStudio路径", microsoftVisualPath, "");
            EditorPrefs.SetString(nameof(microsoftVisualPath), microsoftVisualPath);
            //Debug.Log(new DirectoryInfo(Path.Combine(Application.dataPath, "..")).FullName);
            //Debug.Log(System.IO.Directory.GetCurrentDirectory());
        }

        if(GUILayout.Button("编译"))
        {
            if(string.IsNullOrWhiteSpace(microsoftVisualPath) || !microsoftVisualPath.Contains("Microsoft Visual Studio") || !Directory.Exists(microsoftVisualPath))
            {
                this.ShowNotification(new GUIContent() { image = EditorGUIUtility.IconContent("d_Collab.FolderConflict").image, text = "请检查MicrosoftVisualStudio路径!" });
                return;
            }
            Check();
        }

        posScroll = GUILayout.BeginScrollView(posScroll);
        GUILayout.TextArea(output);
        GUILayout.EndScrollView();
        
        if(GUILayout.Button("清理"))
        {
            output = string.Empty;
        }
    }



    
}
#endif

  • 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
声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号