9.0自定义构建管线
2021-02-18 本文已影响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下的菜单进行打包。
打包菜单
- BuildAndroid打包android
- BuildWindow打包Window
- 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