Unity之Assetbundle打包记录一

本文介绍如何使用Unity通过脚本方式创建AssetBundles资源包,并详细解释了三种不同的打包方式:依赖关系打包、单个资源打包及整体打包。适用于游戏开发人员和技术人员了解和掌握Unity资源管理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值