欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Unity实现批量Build打包详解

程序员文章站 2023-12-29 12:48:19
一般来讲如果项目是pc或android、ios端不会有批量build打包这样的需求,但如果项目是webgl端可能会遇到这样的需求:不同场景打包成不同的包体,入口是前端在页面中布局的,点击链接打开相应的...

一般来讲如果项目是pc或android、ios端不会有批量build打包这样的需求,但如果项目是webgl端可能会遇到这样的需求:不同场景打包成不同的包体,入口是前端在页面中布局的,点击链接打开相应的程序。依次手动打包比较繁琐而且需要等待很长时间,因此写了批量build这样的功能,下班时点击build经历漫长的夜晚,第二天上班时包体已经都打好了。

Unity实现批量Build打包详解

核心api是unityeditor.buildpipeline类中的buildplayer,调用该方法传入相应参数即可实现打包,我们要做的是做一个配置文件,在其中配置打包不同包体对应的数据,包含打包的场景、名称和平台等。首先构建可序列化类:

/// <summary>
/// 打包任务
/// </summary>
[serializable]
public sealed class buildtask
{
    /// <summary>
    /// 名称
    /// </summary>
    public string productname;
    /// <summary>
    /// 目标平台
    /// </summary>
    public buildtarget buildtarget;
    /// <summary>
    /// 打包路径
    /// </summary>
    public string buildpath;
    /// <summary>
    /// 打包场景
    /// </summary>
    public list<sceneasset> sceneassets = new list<sceneasset>(0);
}

使用scriptableobject构建配置:

/// <summary>
/// 打包配置表
/// </summary>
[createassetmenu(filename = "new build profile", menuname = "build profile")]
public sealed class buildprofile : scriptableobject
{
    /// <summary>
    /// 打包任务列表
    /// </summary>
    public list<buildtask> buildtasks = new list<buildtask>(0);
}

Unity实现批量Build打包详解

有了buildprofile后,配置打包列表,批量打包要做的就是遍历该列表依次调用buildpipeline中的buildplayer方法。创建editor类,重写buildprofile的inspector面板,编写打包功能,以及添加、移除打包项等菜单。

[customeditor(typeof(buildprofile))]
public sealed class buildprofileinspector : editor
{
    private readonly dictionary<buildtask, bool> foldoutmap = new dictionary<buildtask, bool>();
    private vector2 scroll = vector2.zero;
    private buildprofile profile;
 
    private void onenable()
    {
        profile = target as buildprofile;
    }
    public override void oninspectorgui()
    {
        guilayout.beginhorizontal();
        {
            if (guilayout.button("新建", "buttonleft"))
            {
                undo.recordobject(profile, "create");
                var task = new buildtask()
                {
                    productname = "product name",
                    buildtarget = buildtarget.standalonewindows64,
                    buildpath = directory.getparent(application.datapath).fullname
                };
                profile.buildtasks.add(task);
            }
            if (guilayout.button("展开", "buttonmid"))
            {
                for (int i = 0; i < profile.buildtasks.count; i++)
                {
                    foldoutmap[profile.buildtasks[i]] = true;
                }
            }
            if (guilayout.button("收缩", "buttonmid"))
            {
                for (int i = 0; i < profile.buildtasks.count; i++)
                {
                    foldoutmap[profile.buildtasks[i]] = false;
                }
            }
            gui.color = color.yellow;
            if (guilayout.button("清空", "buttonmid"))
            {
                undo.recordobject(profile, "clear");
                if (editorutility.displaydialog("提醒", "是否确定清空列表?", "确定", "取消"))
                {
                    profile.buildtasks.clear();
                }
            }
            gui.color = color.cyan;
            if (guilayout.button("打包", "buttonright"))
            {
                if (editorutility.displaydialog("提醒", "打包需要耗费一定时间,是否确定开始?", "确定", "取消"))
                {
                    stringbuilder sb = new stringbuilder();
                    sb.append("打包报告:\r\n");
                    for (int i = 0; i < profile.buildtasks.count; i++)
                    {
                        editorutility.displayprogressbar("build", "building...", i + 1 / profile.buildtasks.count);
                        var task = profile.buildtasks[i];
                        list<editorbuildsettingsscene> buildscenes = new list<editorbuildsettingsscene>();
                        for (int j = 0; j < task.sceneassets.count; j++)
                        {
                            var scenepath = assetdatabase.getassetpath(task.sceneassets[j]);
                            if (!string.isnullorempty(scenepath))
                            {
                                buildscenes.add(new editorbuildsettingsscene(scenepath, true));
                            }
                        }
                        string locationpathname = $"{task.buildpath}/{task.productname}";
                        var report = buildpipeline.buildplayer(buildscenes.toarray(), locationpathname, task.buildtarget, buildoptions.none);
                        sb.append($"[{task.productname}] 打包结果: {report.summary.result}\r\n");
                    }
                    editorutility.clearprogressbar();
                    debug.log(sb.tostring());
                }
                return;
            }
            gui.color = color.white;
        }
        guilayout.endhorizontal();
        scroll = guilayout.beginscrollview(scroll);
        {
            for (int i = 0; i < profile.buildtasks.count; i++)
            {
                var task = profile.buildtasks[i];
                if (!foldoutmap.containskey(task)) foldoutmap.add(task, true);
                guilayout.beginhorizontal("badge");
                guilayout.space(12);
                foldoutmap[task] = editorguilayout.foldout(foldoutmap[task], $"{task.productname}", true);
                guilayout.label(string.empty);
                if (guilayout.button(editorguiutility.iconcontent("treeeditor.trash"), "iconbutton", guilayout.width(20)))
                {
                    undo.recordobject(profile, "delete task");
                    foldoutmap.remove(task);
                    profile.buildtasks.remove(task);
                    break;
                }
                guilayout.endhorizontal();
                if (foldoutmap[task])
                {
                    guilayout.beginvertical("box");
                    guilayout.beginhorizontal();
                    guilayout.label("打包场景:", guilayout.width(70));
                    if (guilayout.button(editorguiutility.iconcontent("toolbar plus more"), guilayout.width(28)))
                    {
                        task.sceneassets.add(null);
                    }
                    guilayout.endhorizontal();
                    if (task.sceneassets.count > 0)
                    {
                        guilayout.beginhorizontal();
                        guilayout.space(75);
                        guilayout.beginvertical("badge");
                        for (int j = 0; j < task.sceneassets.count; j++)
                        {
                            var sceneasset = task.sceneassets[j];
                            guilayout.beginhorizontal();
                            guilayout.label($"{j + 1}.", guilayout.width(20));
                            task.sceneassets[j] = editorguilayout.objectfield(sceneasset, typeof(sceneasset), false) as sceneasset;
                            if (guilayout.button("↑", "minibuttonleft", guilayout.width(20)))
                            {
                                if (j > 0)
                                {
                                    undo.recordobject(profile, "move up scene assets");
                                    var temp = task.sceneassets[j - 1];
                                    task.sceneassets[j - 1] = sceneasset;
                                    task.sceneassets[j] = temp;
                                }
                            }
                            if (guilayout.button("↓", "minibuttonmid", guilayout.width(20)))
                            {
                                if (j < task.sceneassets.count - 1)
                                {
                                    undo.recordobject(profile, "move down scene assets");
                                    var temp = task.sceneassets[j + 1];
                                    task.sceneassets[j + 1] = sceneasset;
                                    task.sceneassets[j] = temp;
                                }
                            }
                            if (guilayout.button(editorguiutility.iconcontent("toolbar plus"), "minibuttonmid", guilayout.width(20)))
                            {
                                undo.recordobject(profile, "add scene assets");
                                task.sceneassets.insert(j + 1, null);
                                break;
                            }
                            if (guilayout.button(editorguiutility.iconcontent("toolbar minus"), "minibuttonmid", guilayout.width(20)))
                            {
                                undo.recordobject(profile, "delete scene assets");
                                task.sceneassets.removeat(j);
                                break;
                            }
                            guilayout.endhorizontal();
                        }
                        guilayout.endvertical();
                        guilayout.endhorizontal();
                    }
                    guilayout.beginhorizontal();
                    guilayout.label("产品名称:", guilayout.width(70));
                    var newpn = guilayout.textfield(task.productname);
                    if (task.productname != newpn)
                    {
                        undo.recordobject(profile, "product name");
                        task.productname = newpn;
                    }
                    guilayout.endhorizontal();
                    guilayout.beginhorizontal();
                    guilayout.label("打包平台:", guilayout.width(70));
                    var newbt = (buildtarget)editorguilayout.enumpopup(task.buildtarget);
                    if (task.buildtarget != newbt)
                    {
                        undo.recordobject(profile, "build target");
                        task.buildtarget = newbt;
                    }
                    guilayout.endhorizontal();
                    guilayout.beginhorizontal();
                    guilayout.label("打包路径:", guilayout.width(70));
                    guilayout.textfield(task.buildpath);
                    if (guilayout.button("browse", guilayout.width(60)))
                    {
                        task.buildpath = editorutility.savefolderpanel("build path", task.buildpath, "");
                    }
                    guilayout.endhorizontal();
                    guilayout.endvertical();
                }
            }
        }
        guilayout.endscrollview();
        serializedobject.applymodifiedproperties();
        if (gui.changed) editorutility.setdirty(profile);
    }
}

Unity实现批量Build打包详解

到此这篇关于unity实现批量build打包详解的文章就介绍到这了,更多相关unity批量build打包内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

上一篇:

下一篇: