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;
        }
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值