生成AssetBundle

本文介绍了一个Unity脚本,该脚本用于从选中的Prefab资源生成AssetBundle,并提供了针对不同平台(Android、iOS、Windows)的导出功能。同时,还展示了如何使用IEnumerator加载这些AssetBundle。

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

选中prefabs,生成AssetBundle

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using UObject = UnityEngine.Object;

public class CreateAseetBundle {

    [MenuItem("CreateAssetBundle/AssetBundle_Android")]
    static void ExportRoom_Android()
    {
        CreatAssetBundle(BuildTarget.Android);
    }
    [MenuItem("CreateAssetBundle/AssetBundle_IOS")]
    static void ExportRoom_IOS()
    {
        CreatAssetBundle(BuildTarget.iOS);
    }
    [MenuItem("CreateAssetBundle/AssetBundle_Win")]
    static void ExportRoom_Win()
    {
        CreatAssetBundle(BuildTarget.StandaloneWindows);
    }
    /// <summary>
    /// 创建AssetBundle 包括材质等 -->prefabs
    /// </summary>
    static void CreatAssetBundle(BuildTarget target)
    {
        UObject[] selection = Selection.GetFiltered(typeof(UObject), SelectionMode.DeepAssets);
        foreach (UObject item in selection)
        {
            string prefabPath = AssetDatabase.GetAssetPath(item);
            string[] resourcesName = new string[1];
            resourcesName[0] = prefabPath;

            AssetBundleBuild[] buildMap = new AssetBundleBuild[1];
            buildMap[0].assetBundleName = item.name + "_assetbundle";
            Debug.Log(resourcesName[0]);
            buildMap[0].assetNames = resourcesName;
            string exportPath = GetBundleUrl(target);
            BuildPipeline.BuildAssetBundles(exportPath, buildMap, BuildAssetBundleOptions.None, target);
       //  BuildPipeline.BuildAssetBundles(dirFile, BuildAssetBundleOptions.None, BuildTarget.StandaloneWindows64);
        }
        AssetDatabase.Refresh();
    }

    static string GetBundleUrl(BuildTarget target)
    {
        if (target == BuildTarget.Android)
            return Application.streamingAssetsPath + "/Android/";
        else if (target == BuildTarget.iOS)
            return Application.streamingAssetsPath + "/IOS/";
        else if (target == BuildTarget.StandaloneWindows)
            return Application.streamingAssetsPath + "/Win/";
        return Application.streamingAssetsPath + "/WinEditor/";
    }
}

加载

    /// <summary>
    /// 初始化加载
    /// </summary>
    IEnumerator GetStartLoadAsset(string assetName)
    {
        WWW www = new WWW("file://" + Application.streamingAssetsPath + @"/Win/" + assetName + "_assetbundle");
        yield return www;
        AssetBundle ab = www.assetBundle;
        GameObject obj = ab.LoadAsset(assetName) as GameObject;
        GameObject genObj = Instantiate(obj);
        genObj.name = assetName + "_child";
        genObj.transform.parent = GameObject.Find(assetName).transform;
        genObj.transform.localPosition = new Vector3(0, 0, 0);
        genObj.transform.localScale = new Vector3(1f, 1f, 1f);
        genObj.transform.localEulerAngles = new Vector3(0, 0, 0);
        www.assetBundle.Unload(false);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值