PackUtilack(打包工具)

using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;

public class PackUtil : EditorWindow
{
    readonly string [] BuildType = { "APK" , "EXE" };
    private static int buildTypeIndex = 0;



    [MenuItem("Window/发布 _F4")]

    static void PackagerWindow() {
        PackUtil packagerEditor = (PackUtil)EditorWindow.GetWindow(typeof(PackUtil) , false , "发布" , true);
        packagerEditor.minSize = new Vector2(300 , 220);
        packagerEditor.maxSize = new Vector2(300 , 220);
        packagerEditor.Show();
    }

    public void OnGUI() {
        buildTypeIndex = GUILayout.Toolbar(buildTypeIndex , BuildType);
        if (GUI.Button(new Rect(80 , 90 , 150 , 20) , "开始打包")) {
            Build();
        }
    }

    static string fabuPath = string.Empty;
    private static BuildTarget buildTarget;
    public static void Build() {
        string Name = DateTime.Now.ToString("yyyyMMdd_HHmmss");
        if (buildTypeIndex == 0) {
            buildTarget = BuildTarget.Android;
            fabuPath = Application.dataPath.Replace("/Assets" , "") + "/APK/" + projectName + "_" + Name + ".apk";
        }
        else if (buildTypeIndex == 1) {
            buildTarget = BuildTarget.StandaloneWindows;
            fabuPath = Application.dataPath.Replace("/Assets" , "") + "/EXE/" + projectName + "_" + Name + ".exe";
        }
        Debug.Log("fabuPath:" + fabuPath);
        if (!string.IsNullOrEmpty(fabuPath)) {
            EditorBuildSettingsScene [] sceneArray = EditorBuildSettings.scenes;
            if (sceneArray != null && sceneArray.Length > 0) {
                List<string> outScenes = new List<string>();
                for (int i = 0 ; i < sceneArray.Length ; i++) {
                    if (sceneArray [i].enabled)
                        outScenes.Add(sceneArray [i].path);
                }
                if (outScenes.Count > 0) {
                    AssetDatabase.Refresh();
                    BuildPipeline.BuildPlayer(outScenes.ToArray() , fabuPath , buildTarget , BuildOptions.None);
                    AssetDatabase.Refresh();
                    System.Diagnostics.Process.Start(Path.GetDirectoryName(fabuPath));
                }
            }
        }
    }

    public static string projectName {
        get
        {
            string tmp = "";
            foreach (string arg in System.Environment.GetCommandLineArgs()) {
                //Debug.Log("arg:" + arg);
                //if (arg.StartsWith("project")) {
                //    return arg.Split("-" [0]) [1];
                //}
                tmp += arg;
                return tmp;
            }
            return Application.productName;
        }
    }


}

你可能感兴趣的:(unity)