美文网首页
9.0自定义构建管线

9.0自定义构建管线

作者: 莫忘初心_倒霉熊 | 来源:发表于2021-02-18 13:12 被阅读0次

设置各个平台数据

using UnityEditor;
public class BuildSettings
{
    public static void UpdateSettings()
    {
        //通用设置
        PlayerSettings.companyName = "TaikrJeff";
        PlayerSettings.productName = "Jump And Run";
        PlayerSettings.bundleVersion = "1.0";

        //安卓平台
        PlayerSettings.applicationIdentifier = "com.Taikr.Jeff.RunAndJump";
    }
}

打包脚本

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

public class Builder
{
    //打包路径
    private static string buildFolderPath = Application.dataPath + "/../Build";
    //打包
    public static void Build(BuildTarget target, string buildName)
    {
        BuildSettings.UpdateSettings();
        string[] scenes = GetEnabledScenes();
        string buildFullPath = buildFolderPath + "/" + target + "/" + buildName;
        BuildPipeline.BuildPlayer(scenes, buildFullPath, target, BuildOptions.None);
    }
    //获取需要打包的场景
    private static string[] GetEnabledScenes()
    {
        List<string> scenes = new List<string>();
        foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
        {
            if (scene.enabled)
                scenes.Add(scene.path);
        }

        return scenes.ToArray();
    }
    //创建打包路径文件夹
    public static void CreateBuildFolder()
    {
        if (System.IO.Directory.Exists(buildFolderPath))
        {
            System.IO.Directory.Delete(buildFolderPath, true);
        }

        System.IO.Directory.CreateDirectory(buildFolderPath);
    }
}

打包工具

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

public class SettingsWindow : EditorWindow
{
    private Dictionary<BuildTarget, string> _targets;
    private const string Prefix = "AppBuilder_";

    public static SettingsWindow instance;
    
    //通过菜单打包android
    [MenuItem("Tools/AppBuilder/BuildAndroid")]
    public static void BuildAndroid()
    {
        Builder.Build(BuildTarget.Android, "RunAndJump.apk");
    }
    //通过菜单打包win
    [MenuItem("Tools/AppBuilder/BuildWindow")]
    public static void BuildWindow()
    {
        Builder.Build(BuildTarget.StandaloneWindows, "RunAndJump.exe");
    }
    //打开打包设置面板
    [MenuItem("Tools/AppBuilder/Show Settings")]
    public static void ShowSettings()
    {
        instance = (SettingsWindow) EditorWindow.GetWindow(typeof(SettingsWindow));
        instance.titleContent = new GUIContent("AppBuilder");
    }

    private void OnEnable()
    {
        InitTargets();
    }

    private void OnGUI()
    {
        DrawPlatformsGUI();
        DrawButtonsGUI();
    }
    //初始化平台
    private void InitTargets()
    {
        _targets = new Dictionary<BuildTarget, string>();
        _targets.Add(BuildTarget.StandaloneWindows, "Windows");
        _targets.Add(BuildTarget.Android, "Android");
    }
    //平台选择Toggle
    private void DrawPlatformsGUI()
    {
        EditorGUILayout.LabelField("Platforms", EditorStyles.boldLabel);
        EditorGUILayout.BeginVertical("box");
        foreach (KeyValuePair<BuildTarget, string> target in _targets)
        {
            DrawPlatformToggle(target.Key, target.Value);
        }

        EditorGUILayout.EndVertical();
    }
    //打包按钮
    private void DrawButtonsGUI()
    {
        if (GUILayout.Button("Build", GUILayout.Height(30)))
            StartBuildProcess();
    }
    //开始打包
    private void StartBuildProcess()
    {
        Builder.CreateBuildFolder();
        foreach (KeyValuePair<BuildTarget, string> target in _targets)
        {
            if (GetPlatformToggleValue(target.Key))
            {
                switch (target.Key)
                {
                    case BuildTarget.Android:
                        Builder.Build(target.Key, "RunAndJump.apk");
                        break;
                    case BuildTarget.StandaloneWindows:
                        Builder.Build(target.Key, "RunAndJump.exe");
                        break;
                }
            }
        }

        EditorUtility.DisplayDialog("AppBuilder", "Build process has finished!", "Ok");
    }
    private void DrawPlatformToggle(BuildTarget target, string label)
    {
        string key = Prefix + target.ToString();
        bool currentValue = EditorPrefs.GetBool(key, false);
        bool newValue = GUILayout.Toggle(currentValue, label);
        EditorPrefs.SetBool(key, newValue);
    }
    private bool GetPlatformToggleValue(BuildTarget target)
    {
        string key = Prefix + target.ToString();
        return EditorPrefs.GetBool(key, false);
    }
}

如图,我们可以通过Tools/AppBuilder下的菜单进行打包。


打包菜单
  1. BuildAndroid打包android
  2. BuildWindow打包Window
  3. ShowSettings打开设置面板

点击ShowSetting按钮,如图:可以一次打包多个平台。


ShowSetting

使用.bat文件在Unity外部打包

注意,需要关闭Unity

这里我把.bat文件放到Assets同级目录下,如图:


bat文件

BuildWindow.bat专为window打包的.bat文件,BuildLog.txt是打包的日志文件。
BuildWindow.bat如下:

"C:\Program Files\Unity\Hub\Editor\2020.2.3f1c1\Editor\Unity.exe" -quit -batchmode -projectPath "C:\Users\17190\Desktop\TestUMP" -executeMethod SettingsWindow.BuildWindow -logFile "BuildLog.txt"

更多的详情请看这里https://docs.unity3d.com/Manual/CommandLineArguments.html

相关文章

  • 9.0自定义构建管线

    设置各个平台数据 打包脚本 打包工具 如图,我们可以通过Tools/AppBuilder下的菜单进行打包。 Bui...

  • URP RenderObject

    RenderObject RenderObject是什么 前面讲到过在URP渲染管线下,可以对自定义管线的渲染配置...

  • Unity3D CustomSRP[译].1.自定义渲染管线[C

    Custom Render Pipeline(自定义渲染管线) ——控制渲染 本节内容 创建一个渲染管线资源和实例...

  • 鸿业市政管线9.0破解版软件免费下载附安装教程

    鸿业市政管线9.0是国内最早研制并推出的专业市政管线类软件,由给排水专业人员和计算机专业人员共同开发而成,软件基本...

  • Unity3D CustomSRP[译]

    本系列翻译自Catlike Coding[https://catlikecoding.com/]的自定义渲染管线[...

  • 四、OpenGL的存储着色器

    OpenGL提供了一些能够实现某些渲染功能的固定管线,即存储着色器。这些管线只能拿来直接使用,不支持自定义编程,接...

  • Docker操作相关命令

    docker build -t 自定义名称 . //构建镜像 docker build -t 自定义名称 ...

  • 115.脚手架工作流程

    脚手架工作流程 通过yeoman来构建 自定义构建脚手架 自定义构建脚手架 通过命令行工具启动一个程序,获取用户输...

  • Swift

    1.Swift构建自定义控件2.Swift开发集锦3.Swift构建自定义控件4.Cocoa的Swift开发专题5...

  • 6.1、Android Studio配置构建

    文章摘要1、 Gradle 构建流程2、自定义构建配置3、构建配置文件4、Gradle 设置文件5、顶级构建文件6...

网友评论

      本文标题:9.0自定义构建管线

      本文链接:https://www.haomeiwen.com/subject/vbbgxltx.html