Unity 一键导出exe

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

/// <summary>
/// 批量导出exe
/// ====================================================================放在 Editor  文件夹里面
/// </summary>
public class BatchExportExe : MonoBehaviour
{
    public static int num = 0;
    public static int numMax;
    public static string scenesName;
    /// <summary>
    /// 场景目录
    /// </summary>
    private static readonly string scenePath = "Scenes/";
    /// <summary>
    /// 文件目录
    /// </summary>
    static string pathX = @"E:\UnityWorkPlace\打包文件\";

    [MenuItem("Tools/一键导出")]
    public static void SettingPlayer()
    {
        RefreshAllScene(true);
        string[] scenesNamseList = scenesName.Split('\\');
        string[] sceneNamesList_Right = scenesNamseList[scenesNamseList.Length - 1].Split('/');
        string sceneNamesLis_Right_Left = sceneNamesList_Right[sceneNamesList_Right.Length - 1].Replace(".unity", "");
        string folderName = sceneNamesList_Right[sceneNamesList_Right.Length - 1].Replace(".unity", "");//文件夹的名字
        int num = sceneNamesLis_Right_Left.IndexOf(".") + 1;
        sceneNamesLis_Right_Left = sceneNamesLis_Right_Left.Substring(num);
        PlayerSettings.productName = sceneNamesLis_Right_Left;
        string path = pathX + (folderName) + @"\" + (sceneNamesLis_Right_Left) + ".exe";                         //每个场景都有一个单独的文件夹
                                                                                                                 //string path = pathX + (sceneNamesLis_Right_Left) + ".exe";                                                           //所有场景都在一个文件夹
        BuildReport report = BuildPipeline.BuildPlayer(GetBuildScenes(), path, BuildTarget.StandaloneWindows, BuildOptions.None);
        BuildSummary summary = report.summary;
        if (summary.result == BuildResult.Succeeded)
        {
            Debug.Log("完成");
           SettingStop();
        }
        path = "";
    }
    // 添加菜单选项
    //[MenuItem("Tool/RefreshScene")]
    public static void RefreshAllScene(bool value)
    {
        // 设置场景 *.unity 路径
        string pathUnity = Path.Combine(Application.dataPath, scenePath);
        // 遍历获取目录下所有 .unity 文件
        string[] files = Directory.GetFiles(pathUnity, "*.unity", SearchOption.AllDirectories);
        // 定义 场景数组
        EditorBuildSettingsScene[] scenes = new EditorBuildSettingsScene[1];
        //将即将发布的场景赋值
        string scenePathFiles = files[num];
        // 通过scene路径初始化
        scenes[0] = new EditorBuildSettingsScene(scenePathFiles, value);
        numMax = files.Length;
        // 设置 scene 数组
        EditorBuildSettings.scenes = scenes;
        //导出路径      *****路径+场景名
        scenesName = files[num].ToString();
    }
    static string[] GetBuildScenes()
    {
        List<string> names = new List<string>();
        foreach (EditorBuildSettingsScene item in EditorBuildSettings.scenes)
        {
            if (item == null)
                continue;
            if (item.enabled)
            {
                names.Add(item.path);
            }
        }
        return names.ToArray();
    }
    public static void SettingStop()
    {
        if (num < numMax - 1)
        {
            num++;
            RefreshAllScene(false);
            SettingPlayer();
        }
    }
}

 

posted on 2024-11-06 16:55  zqiang0803  阅读(17)  评论(0编辑  收藏  举报

导航