---
title: "Unity3D一键导出Windows程序"
author: "wgcode"
date: "2015-05-13T14:37:13Z"
tags: []
categories: ["移动开发"]
slug: "unity3d-windows"
draft: false
lastmod: "2015-05-13T14:37:13Z"
id: 2210790
url: "https://tts.iteye.com/blog/2210790"
---

01. using UnityEngine;
02. using System.Collections;
03. using UnityEditor;
04. using System.Collections.Generic;
05. using System.IO;
06. public class Tools : EditorWindow
07. {
08. \[MenuItem("Tools/Build for Windows")]
09. public static void ShowWindow()
10. {
11. EditorWindow.GetWindow(typeof(Tools), true, "导出Windows应用程序");
12. }
13. static string exePath = "";
14. static List&lt;string&gt; allSceneName = new List&lt;string&gt;();
15. void OnGUI()
16. {
17. GUILayout.Label("导出路径：", EditorStyles.boldLabel);
18. GUILayout.Label(exePath, EditorStyles.boldLabel);
19. if (GUILayout.Button("选择导出路径"))
20. {
21. exePath = EditorUtility.SaveFolderPanel("导出路径", "", "");
22. }
23. if (GUILayout.Button("扫描场景"))
24. {
25. string\[] sl = Directory.GetFiles(Application.dataPath + "/Scene/");
26. foreach (string file in sl)
27. {
28. if (!file.Contains(".meta"))
29. {
30. string temp = file;
31. if (!allSceneName.Contains(temp))
32. {
33. allSceneName.Add(temp);
34. }
35. }
36. }
37. }
38. if (GUILayout.Button("导出程序"))
39. {
40. string\[] levels = new string\[1];
41. Debug.Log("allSceneName.Count:" + allSceneName.Count);
42. if (allSceneName.Count &gt; 0)
43. {
44. Debug.Log("allSceneName.Count:" + allSceneName.Count);
45. for (int i = 0; i &lt; allSceneName.Count; i++)
46. {
47. levels\[0] = allSceneName\[i];
48. string temp = allSceneName\[i].Replace(Application.dataPath + "/Scene/", "");
49. temp = temp.Replace(".unity", "");
50. string exeName = exePath + "/" + temp + ".exe";
51. if (File.Exists(exeName))
52. {
53. File.Delete(exeName);
54. Directory.Delete(exePath + "/" + temp + "\_Data",true);
55. }
56. BuildPipeline.BuildPlayer(levels, exeName, BuildTarget.StandaloneWindows, BuildOptions.None);
57. }
58. }
59. }
60. }
61. }

 

效果如图

![](http://img.blog.csdn.net/20140912180425718?watermark%2F2%2Ftext%2FaHR0cDovL2Jsb2cuY3Nkbi5uZXQvQVdOVVhDVkJO%2Ffont%2F5a6L5L2T%2Ffontsize%2F400%2Ffill%2FI0JBQkFCMA=%3D%2Fdissolve%2F70%2Fgravity%2FSouthEast)