using UnityEngine;
using System.Collections;
using UnityEditor;
public class ExportAssetBundles {
[MenuItem("Custom Editor/Create comm asset")]
//依赖关系打包
static void CreateCommAssetBunldes()
{
//公用资源 需要依赖的公用资源login.png
//进入资源依赖栈 A
BuildPipeline.PushAssetDependencies();
string path = Application.streamingAssetsPath;
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Resources/login.png"), null,
path + "/login.assetbundle",
BuildAssetBundleOptions.CollectDependencies);
//进入资源依赖栈 B: 这里B依赖A
BuildPipeline.PushAssetDependencies();
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Prefab1.prefab"), null,
path + "/Prefab1.assetbundle",
BuildAssetBundleOptions.CollectDependencies);
//跳出资源依赖栈
BuildPipeline.PopAssetDependencies();
BuildPipeline.PopAssetDependencies();
//这里C没有依赖关系
BuildPipeline.BuildAssetBundle(AssetDatabase.LoadMainAssetAtPath("Assets/Resources/Prefab2.prefab"), null,
path + "/Prefab2.assetbundle",
BuildAssetBundleOptions.CollectDependencies);
EditorUtility.DisplayDialog("", "Completed", "OK");
}
[@MenuItem("Custom Editor/Create AssetBunldes Main")]
//单个打包
static void CreateAssetBunldesMain()
{
Caching.CleanCache();
//获取在Project视图中选择的所有游戏对象
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
int i = 0;
//遍历所有的游戏对象
foreach (Object obj in SelectedAsset)
{
//string sourcePath = AssetDatabase.GetAssetPath(obj);
string targetPath = Application.streamingAssetsPath + "/" + obj.name + ".assetbundle";
if (BuildPipeline.BuildAssetBundle(obj, null, targetPath, BuildAssetBundleOptions.CollectDependencies))
{
Debug.Log(obj.name + "资源打包成功");
}
else
{
Debug.Log(obj.name + "资源打包失败");
}
}
//刷新编辑器
EditorUtility.DisplayDialog("", "Completed", "OK");
AssetDatabase.Refresh();
}
[@MenuItem("Custom Editor/Create AssetBunldes ALL")]
//整体打包
static void CreateAssetBunldesALL()
{
Caching.CleanCache();
string Path = Application.streamingAssetsPath + "/ALL.assetbundle";
Object[] SelectedAsset = Selection.GetFiltered(typeof(Object), SelectionMode.DeepAssets);
foreach (Object obj in SelectedAsset)
{
Debug.Log("Create AssetBunldes name :" + obj);
}
//这里注意第二个参数就行
if (BuildPipeline.BuildAssetBundle(null, SelectedAsset, Path, BuildAssetBundleOptions.CollectDependencies))
{
AssetDatabase.Refresh();
}
}
}
Unity之Assetbundle打包记录一
最新推荐文章于 2025-07-02 13:27:55 发布