通过最近的学习扩展一下自动打包的功能,重写了之前的代码,支持Windows和Mac打包Android和IOS(Xcode工程包),简直perfect~~
这个自动打包只是一个Demo,简单的实现了出包功能,直接拿来用与手动也没多大区别,不过是少点几下鼠标,实际使用还要与开发流程相结合,设置条件,自动调用打包方法,自动出包~
//自动打包部分:
#if UNITY_EDITOR
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.IO;
//Assets/Editor文件夹下
namespace SimpleFrame.Tool
{
public class PackageTool
{
//打包操作
public static void Build(string apkPath, string apkName, BuildTarget buildTarget)
{
if (!Directory.Exists(apkPath) || string.IsNullOrEmpty(apkName))
return;
if (BuildPipeline.isBuildingPlayer)
return;
//打包场景路径
List<string> sceneNames = new List<string>();
foreach (EditorBuildSettingsScene scene in EditorBuildSettings.scenes)
{
if (scene != null && scene.enabled)
{
sceneNames.Add(scene.path);
//Debug.Log(scene.path);
}
}
if(sceneNames.Count == 0)
{
Debug.Log("Build Scene Is None");
return;
}
BuildTargetGroup buildTargetGroup = GetBuildTargetGroupByBuildTarget(buildTarget);
//切换平台
EditorUserBuildSettings.SwitchActiveBuildTarget(buildTargetGroup, buildTarget);
//设置打包平台、标签
PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, null);
string path = apkPath + "/" + apkName;
if (buildTarget == BuildTarget.Android)
path += ".apk";
else if(buildTarget == BuildTarget.iOS)
path += "_IOS";
Debug.Log("Start Build Package");
//开始打包
BuildPipeline.BuildPlayer(sceneNames.ToArray(), path, buildTarget, BuildOptions.None);
}
回调 场景运行前操作
//[PostProcessScene(1)]
//public static void BeforeBuild()
//{
// Debug.Log("Build Start");
//}
//回调 打包后操作
[PostProcessBuild(1)]
public static void AfterBuild(BuildTarget target, string pathToBuiltProject)
{
Debug.Log("Build Success " + target + " " + pathToBuiltProject);
//打开文件或文件夹
System.Diagnostics.Process.Start(pathToBuiltProject);
}
static BuildTargetGroup GetBuildTargetGroupByBuildTarget(BuildTarget buildTarget)
{
switch(buildTarget)
{
case BuildTarget.Android:
return BuildTargetGroup.Android;
case BuildTarget.iOS:
return BuildTargetGroup.iOS;
case BuildTarget.StandaloneOSX:
case BuildTarget.StandaloneWindows:
case BuildTarget.StandaloneWindows64:
return BuildTargetGroup.Standalone;
//······
default:
return BuildTargetGroup.Standalone;
}
}
}
}
#endif
//接下来这一段代码,自定义了一个弹窗,不涉及自动打包,可忽略 ······
//确认窗口, 可用快捷键启动
public class BuildPackageWindow : EditorWindow
{
[MenuItem("MyTools/Package/Build Android Package #&b")]
public static void ShowPackageWindow()
{
//弹出确认窗口
EditorWindow.GetWindow(typeof(BuildPackageWindow), false, "Build Package Window");
}
const string buildPathPlayerPrefsStr = "BuildPathPlayerPrefsStr";
string showStr;
Texture2D defaultIcon;
//
string apkPath, apkName;
void OnEnable()
{
//确认信息
showStr = "Package Info :";
showStr += "\nCompany Name : " + PlayerSettings.companyName;
showStr += "\nProduct Name : " + PlayerSettings.productName;
showStr += "\nIdentifier : " + PlayerSettings.applicationIdentifier;
showStr += "\nBundle Version : " + PlayerSettings.bundleVersion;
showStr += "\nBundle Version Code : " + PlayerSettings.Android.bundleVersionCode;
//图标Iocn
Texture2D[] texture2Ds = PlayerSettings.GetIconsForTargetGroup(BuildTargetGroup.Android);
defaultIcon = texture2Ds.Length > 0 ? texture2Ds[0] : null;
//打包默认路径及默认文件名
apkPath = PlayerPrefs.GetString(buildPathPlayerPrefsStr);
apkName = PlayerSettings.productName;
}
void OnGUI()
{
GUILayout.Space(10);
GUILayout.Label("Please Make Sure");
defaultIcon = EditorGUILayout.ObjectField(defaultIcon, typeof(Texture2D), true) as Texture2D;
EditorGUILayout.TextArea(showStr);
GUILayout.Space(5);
apkPath = EditorGUILayout.TextField("Package Path : ", apkPath);
if (!Directory.Exists(apkPath))
EditorGUILayout.TextArea("Package Path Is Error");
apkName = EditorGUILayout.TextField("Package Name : ", apkName);
if (string.IsNullOrEmpty(apkName))
EditorGUILayout.TextArea("Package Name Is Empty");
//调用系统窗口,选择保存路径
if (GUILayout.Button("Choose Path"))
{
//EditorUtility.OpenFolderPanel("Choose Output Path", "", "");
string tmpPath = EditorUtility.SaveFolderPanel("Choose Package Output Path", "", "");
if (!string.IsNullOrEmpty(tmpPath))
{
apkPath = tmpPath;
PlayerPrefs.SetString(buildPathPlayerPrefsStr, apkPath);
}
}
GUILayout.Space(10);
if (GUILayout.Button("Cancel"))
this.Close();
GUILayout.Space(5);
if (Directory.Exists(apkPath) && !string.IsNullOrEmpty(apkName))
{
if (GUILayout.Button("Build Android"))
{
this.Close();
PackageTool.Build(apkPath, apkName, BuildTarget.Android);
}
GUILayout.Space(5);
if (GUILayout.Button("Build IOS"))
{
this.Close();
PackageTool.Build(apkPath, apkName, BuildTarget.iOS);
}
}
this.Repaint();
}
void OnDisable()
{
showStr = null;
defaultIcon = null;
}
}