unity自動化打AB包

using System.Collections;
using System.Collections.Generic;
using UnityEngine;//運(yùn)行期間用
using UnityEditor;//編輯狀態(tài)用
{

? ? public static Object[] Objs = new Object[] { };

? ? [MenuItem("Assets/AssetsBundle/BuildSelectObjects")]
? ? static void BuildSelect()
? ? {
? ? ? ? //獲取所有選中的對象
? ? ? ? Objs = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
? ? ? ? ? ? ?
? ? ? ? //彈出一個編輯窗口;
? ? ? ? AssetBundleWindow.ShowWindow();
? ? }

? ? /// <summary>
? ? /// 開始打包;
? ? /// </summary>
? ? public static void StartBuild()
? ? {
? ? ? ? Debug.Log("開始打包!");
? ? ? ? string path = AssetBundleWindow.AsbPath;
? ? ? ? Debug.Log("選擇路徑:" + path);

? ? ? ? //設(shè)置出asb[]
? ? ? ? AssetBundleBuild abb = new AssetBundleBuild();
? ? ? ? abb.assetNames = new string[Objs.Length];
? ? ? ? for (int i = 0; i < Objs.Length; i++)
? ? ? ? {
? ? ? ? ? ? abb.assetNames[i] = AssetDatabase.GetAssetPath(Objs[i]);
? ? ? ? }
? ? ? ? if (AssetBundleWindow.IsWindows)
? ? ? ? {
? ? ? ? ? ? //設(shè)置路徑;
? ? ? ? ? ? Debug.Log("將要打包到Windows");
? ? ? ? ? ? abb.assetBundleName = AssetBundleWindow.AssetBudleName + "_windows.UnityAsb";
? ? ? ? ? ? //開始打包;
? ? ? ? ? ? BuildPipeline.BuildAssetBundles(path, new AssetBundleBuild[] { abb }, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
? ? ? ? }
? ? ? ? if (AssetBundleWindow.IsAndorid)
? ? ? ? {
? ? ? ? ? ? //設(shè)置路徑;
? ? ? ? ? ? Debug.Log("將要打包到安卓");
? ? ? ? ? ? abb.assetBundleName = AssetBundleWindow.AssetBudleName + "_android.UnityAsb";
? ? ? ? ? ? //開始打包;
? ? ? ? ? ? BuildPipeline.BuildAssetBundles(path, new AssetBundleBuild[] { abb }, BuildAssetBundleOptions.None, BuildTarget.Android);
? ? ? ? }
? ? ? ? if (AssetBundleWindow.IsApple)
? ? ? ? {
? ? ? ? ? ? //設(shè)置路徑:
? ? ? ? ? ? Debug.Log("將要打包到蘋果");
? ? ? ? ? ? abb.assetBundleName = AssetBundleWindow.AssetBudleName + "_ios.UnityAsb";
? ? ? ? ? ? //開始打包;
? ? ? ? ? ? BuildPipeline.BuildAssetBundles(path, new AssetBundleBuild[] { abb }, BuildAssetBundleOptions.None, BuildTarget.iOS);
? ? ? ? }
? ? ? ? Debug.Log("打包完成!");
? ? }
}

/// <summary>
/// 彈出窗口;
/// </summary>
public class AssetBundleWindow : EditorWindow
{
? ? /// <summary>
? ? /// asb的名字;
? ? /// </summary>
? ? public static string AssetBudleName;
? ? /// <summary>
? ? /// asb包的路徑;
? ? /// </summary>
? ? public static string AsbPath;
? ? /// <summary>
? ? /// 是否在Windows下打包;
? ? /// </summary>
? ? public static bool IsWindows = false;
? ? /// <summary>
? ? /// 是否在安卓下打包;
? ? /// </summary>
? ? public static bool IsAndorid = false;
? ? /// <summary>
? ? /// 是否在蘋果下打包;
? ? /// </summary>
? ? public static bool IsApple = false;


? ? AssetBundleWindow()
? ? {
? ? ? ? titleContent = new GUIContent("資源打包");
? ? }


? ? public static void ShowWindow()
? ? {
? ? ? ? GetWindow(typeof(AssetBundleWindow));
? ? }

? ? private void OnGUI()
? ? {
? ? ? ? GUILayout.Space(10);
? ? ? ? GUI.skin.label.fontSize = 15;
? ? ? ? GUI.skin.label.alignment = TextAnchor.MiddleCenter;
? ? ? ? if (AssetBundle.Objs.Length > 0)
? ? ? ? {
? ? ? ? ? ? GUILayout.Label("當(dāng)前總共選擇:" + AssetBundle.Objs.Length + "個資源!");
? ? ? ? ? ?
? ? ? ? ? ? //繪制文件路徑選擇
? ? ? ? ? ? GUILayout.Space(10);
? ? ? ? ? ? if (GUILayout.Button("路徑選擇", GUILayout.Width(200)))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? AsbPath = EditorUtility.SaveFolderPanel("請選擇打包路徑", Application.streamingAssetsPath, AssetBudleName);
? ? ? ? ? ? ? ? //這里開啟窗口重繪制;
? ? ? ? ? ? ? ? Repaint();
? ? ? ? ? ? }

? ? ? ? ? ? GUI.skin.label.fontSize = 10;
? ? ? ? ? ? GUI.skin.label.alignment = TextAnchor.MiddleLeft;
? ? ? ? ? ? if (string.IsNullOrEmpty(AsbPath))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //這里繪制選擇結(jié)果;
? ? ? ? ? ? ? ? GUILayout.Label("沒有選擇打包路徑!" );
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //這里繪制選擇結(jié)果;
? ? ? ? ? ? ? ? GUILayout.Label("當(dāng)前選擇打包路徑:" + AsbPath);
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? //放3個togle
? ? ? ? ? ? ? ? IsWindows = GUI.Toggle(new Rect(10, 100, 600, 20), IsWindows, "打包到Windows平臺");

? ? ? ? ? ? ? ? IsAndorid = GUI.Toggle(new Rect(10, 120, 600, 20), IsAndorid, "打包到Android平臺");

? ? ? ? ? ? ? ? IsApple = GUI.Toggle(new Rect(10, 140, 600, 20), IsApple, "打包到IOS平臺");

? ? ? ? ? ? ? ? //繪制文件打包按鈕
? ? ? ? ? ? ? ? GUILayout.Space(100);
? ? ? ? ? ? ? ? GUILayout.Label("請輸入導(dǎo)出的包名:");
? ? ? ? ? ? ? ? //設(shè)置一個文字輸入框;
? ? ? ? ? ? ? ? AssetBudleName = EditorGUILayout.TextField(AssetBudleName);

? ? ? ? ? ? ? ? GUILayout.Space(10);

? ? ? ? ? ? ? ? if (GUILayout.Button("開始打包", GUILayout.Width(200)))
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? if (string.IsNullOrEmpty(AssetBudleName))
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? Debug.Log("請輸入打包文件名!");
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? AssetBundle.StartBuild();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }


? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? GUILayout.Label("當(dāng)前未選擇任何資源!");
? ? ? ? }
? ? }
}
? ? ? ? ? ? ? ? ? ? else
public class AssetBundle : Editor
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容